使用弹性搜索搜索多个术语

时间:2016-08-05 23:14:37

标签: json elasticsearch

我是ES的新手,想要设置查询以搜索多个字词。

我的SQL看起来像:

"SELECT field4 FROM tablename WHERE (field1 = 'A' OR field1 = 'AA') AND (field2 = 'B' OR fields2 = 'BB') AND (field3 = 'C' OR field3 = 'CC')"

在ES中我尝试

{
"query": {
"bool": {
  "must": [
    {
      "term": {
        "field1": {"A","AA"}
      }
    },
    {
      "term": {
        "field2": {"B","BB"}
      }
    },
    {
    "term": {
      "field3": {"C","CC"}
    }
   }
  }
 }
}

1 个答案:

答案 0 :(得分:2)

您最想要的是terms查询。您可以在此处查看相关文档:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-terms-query.html

该网站的一个例子是:

{
    "terms" : { "user" : ["kimchy", "elasticsearch"]}
}

此外,您的查询现在不是有效的json,您需要更正它。