2011-12-19 66 views
6

我是Elasticsearch的新手。我不认为我完全理解查询和过滤器的概念。在我的情况下,我只想使用过滤器,因为我不想使用评分等高级功能。Elasticsearch来自SQL语句的DSL查询

如何将以下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场?

谢谢

回答

2

在你的must子句后添加一个“should”子句。在一个bool查询中,一个或多个should子句必须默认匹配。其实,你可以设置“minimum_number_should_match”为任意数字,查看bool query docs

对于你的情况,这应该工作。

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

相同的概念适用于布尔过滤器。只需将“查询”更改为“过滤器”即可。布尔筛选器文档是here

0

我碰到过这样的杆柱4年太久已晚...

不管怎么说,也许下面的代码可能是有用的...

{ 
    "query": { 
    "filtered": { 
     "query": { 
     "wildcard": { 
      "company": "*com*" 
     } 
     }, 
     "filter": { 
     "bool": { 
      "should": [ 
      { 
       "terms": { 
       "sales_rep_id": [ "1", "2" ] 
       } 
      } 
      ] 
     } 
     } 
    } 
    } 
}