繁体   English   中英

Elasticsearch 匹配多个字段,AND 运算符不起作用

[英]Elasticsearch match multiple fields with AND operator not working

我正在尝试使用 AND 运算符从具有多个字段的 elasticsearch 中获取文档

对于以下查询,我期待以下结果

AB-7000-8002-W

但我收到此错误消息Unrecognized token 'get': was expecting ('true', 'false' or 'null')\\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@40d2a7e8; line: 1, column: 5] Unrecognized token 'get': was expecting ('true', 'false' or 'null')\\n at [Source: org.elasticsearch.transport.netty4.ByteBufStreamInput@40d2a7e8; line: 1, column: 5]

 get my_index12/_search {
    "query" : {
        "bool": {
            "should": [
                {
                    "match": {
                        "code": {
                         "query": "AB-5000-6002-AK",
                         "operator": "and"
                        }
                    }
                },
                {
                    "match": {
                        "locale": {
                         "query": "en_US",
                         "operator": "and"
                        }
                    }
                }
            ]
        }
    }
 }

请在下面找到我的索引文件

 {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "2",
        "_score": 1,
        "_source": {
          "code": "AB-7000-8002-W",
          "locale": "en_US"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "4",
        "_score": 1,
        "_source": {
          "code": "AB-7000-8002-W",
          "locale": "en_EU"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "1",
        "_score": 1,
        "_source": {
          "code": "sG66tsdF",
          "locale": "en_US"
        }
      },
      {
        "_index": "my_index12",
        "_type": "doc",
        "_id": "3",
        "_score": 1,
        "_source": {
          "code": "AB-7000-6002-WK",
          "locale": "en_EU"
        }

只需将get my_index12/_search {行中的花括号移动到下一行。 它应该工作。

为了获得同时满足这两个条件的结果,您必须使用must子句而不是should match查询中的“AND”运算符不适用于您想要实现的用例。 使用以下查询。

{
"query": {
"bool": {
  "must": [
    {
      "match": {
        "code": {
          "query": "TE-7000-8002-W",
          "operator": "and"
        }
      }
    },
    {
      "match": {
        "locale": {
          "query": "en_US",
          "operator": "and"
          }
        }
      }
     ]
    }
  }
}

对于那些在多个字段上寻找 AND 之类的查询应该可以工作。 下面将搜索代码为“TE-7000-8002-W”,语言环境仅搜索为 en_US

    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                       "code": "TE-7000-8002-W"
                    }
                },
                {
                    "match": {
                       "locale": "en_US"
                    }
                }
            ]
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM