【问题标题】:Elasticsearch: merge terms query and match queryElasticsearch:合并术语查询和匹配查询
【发布时间】:2022-04-08 23:51:46
【问题描述】:

假设,我的表有 2 个字段:用户名和年龄 我想检查列表用户名:[john, mary, jim] 并且该用户的“年龄”为 20。我该怎么办? 我想我将合并以下 2 个查询:

  • 词条查询: { “条款”:{ “用户”:[“约翰”、“玛丽”、“吉姆”] } }

  • 并匹配查询:

    { “询问”: { “匹配”: { “年龄”:20 } } }

但我不确定。请帮我。谢谢。

【问题讨论】:

    标签: elasticsearch match


    【解决方案1】:

    terms 查询似乎适合该名称。但是,我建议为“年龄”使用term 过滤器,因为它是一个数值。通常,match 查询是针对您计划对其进行全文搜索的analyzed 字段。

    例如,您可以使用 filtered 查询将两个搜索条件链接在一起,如下所示:

    {
      "query": {
        "filtered": {
          "query": {
            "terms": {
              "user_name": ["john","mary","jim"]
            }
          },
          "filter": {
            "term": {
              "age": 20
            }
          }
        }
      }
    }
    

    另一种可能性是使用bool 查询组合您的搜索条件。例如。

    {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {
                "user_name": ["john","mary","jim"]
              }
            },
            {
              "term": {
                "age": 20
              }
            }
          ]
        }
      }
    }
    

    在这里查看documentation for Bool Query

    【讨论】:

    • 非常感谢。我明白了:)
    • @TriPham 很高兴我能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2019-12-06
    • 2021-11-11
    • 1970-01-01
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多