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

时间:2018-06-13 09:15:58

标签: java elasticsearch kibana elastic-stack

尝试使用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]

 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"
        }

2 个答案:

答案 0 :(得分:2)

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

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

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

答案 1 :(得分:1)

对于那些希望在多个字段上进行AND查询的人应该可以使用。下面将搜索代码“ TE-7000-8002-W”,语言环境仅搜索en_US

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