6

我是 Elasticsearch 的新手。我不认为我完全理解查询和过滤器的概念。就我而言,我只想使用过滤器,因为我不想使用评分等高级功能。

如何将以下 SQL 语句转换为 elasticsearch 查询?

SELECT * FROM advertiser 
WHERE company like '%com%' 
AND sales_rep IN (1,2) 

到目前为止我所拥有的:

curl -XGET 'localhost:9200/advertisers/advertiser/_search?pretty=true' -d ' 
 { 
     "query" : { 
         "bool" : { 
             "must" : { 
                 "wildcard" : { "company" : "*com*" } 
             } 
         } 
     }, 
     "size":1000000 

}' 

如何在 sales_rep 字段上添加 OR 过滤器?

谢谢

4

2 回答 2

2

Add a "should" clause after your must clause. In a bool query, one or more should clauses must match by default. Actually, you can set the "minimum_number_should_match" to be any number, Check out the bool query docs.

For your case, this should work.

    "should" : [
        {
            "term" : { "sales_rep_id" : "1" }
        },
        {
            "term" : { "sales_rep_id" : "2" }
        }
    ],

The same concept works for bool filters. Just change "query" to "filter". The bool filter docs are here.

于 2011-12-19T19:47:40.333 回答
0

晚了4年才看到这个帖子。。。

无论如何,也许下面的代码可能有用......

{
  "query": {
    "filtered": {
      "query": {
        "wildcard": {
          "company": "*com*"
        }
      },
      "filter": {
        "bool": {
          "should": [
            {
              "terms": {
                "sales_rep_id": [ "1", "2" ]
              }
            }
          ]
        }
      }
    }
  }
}
于 2016-03-01T10:40:54.253 回答