【问题标题】:Query an array with must match查询必须匹配的数组
【发布时间】:2019-10-25 02:10:01
【问题描述】:

我有一个文档索引。索引包含文档正文和文档类型,例如 pdf、jpeg、png 等。我可以用一个单词和一个文档类型查询索引,使用 must 就可以了。

                $params = [
                'index' => 'trial2',
                'type' => '_doc',
                'body' => [
                  'query' => [
                    'bool' => [
                      'must' => [
                        [ 'match' => [ 'file.extension' => "png" ] ],
                        [ 'match' => [ 'content' => "abc" ] ],
                      ]
                    ]
                  ]
                ]
              ];

screenshot 挑战是,我想查询仍然使用 must 但文档类型数组(png 但 jpeg、gif、svg、tiff)的索引,以便将其分类为图像。如何将 png 替换为数组,以便至少一个为真。

【问题讨论】:

    标签: php elasticsearch


    【解决方案1】:

    如果file.extension 属于text 类型:

    只需在Match Query 旁边添加更多令牌。确保您通过this (Analysis)this (Analyzer) 了解它在内部是如何工作的。

    POST my_png/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "file.extension": "jpg jpeg png"        <---- Note this.
              }
            },
            {
              "match": {
                "content": "abc"
              }
            }
          ]
        }
      }
    }
    

    如果file.extensionkeyword 类型(推荐)

    或者如果你在file.extension.keyword 中有一个keyword 兄弟,你可以使用Terms Query

    POST my_png/_search
    {
      "query": {
        "bool": {
          "must": [
            {
              "terms": {                         <---- Terms Query
                "file.extension.keyword": [      <---- Or 'file.extension' field, whichever must of be type `keyword`
                  "jpg",
                  "jpeg",
                  "png"
                ]
              }
            },
            {
              "match": {
                "content": "abc"
              }
            }
          ]
        }
      }
    }
    

    根据您的要求,我认为您必须使用第二个选项来进行精确匹配,您需要在 keyword 字段上使用 Terms Query

    希望有帮助!

    【讨论】:

    • @Denn 您能否检查一下我在您提到的链接中发布的答案。如果您需要任何进一步的帮助,请告诉我!
    • 非常感谢.. 你澄清了一个我没有得到的概念。我很感激..也许对我的回复发表评论?
    【解决方案2】:

    您可以使用“术语”查询:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

    terms 查询返回在提供的字段中包含一个或多个确切术语的文档。

    【讨论】:

      猜你喜欢
      • 2017-07-29
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 1970-01-01
      • 2021-12-08
      • 2019-07-10
      • 2019-05-14
      • 2020-03-15
      相关资源
      最近更新 更多