我有一个SQL查询,像这样
sql_1 = "SELECT * FROM dd_s3data WHERE (yelp_address = '370 Barren Rd' OR yelp_businessname ILIKE '%Computer%') AND (yelp_state = 'CT' OR yelp_category ILIKE '%flooring%');"
我正试图将其转换为Elasticsearch查询。这是我尝试的查询。它给出的是 OR
结果 AND
es_query1 = {
"query": {
"constant_score": {
"filter": {
"bool": {
"should": [
{"match_phrase": {"yelp_address": "370 Barren Rd"}},
{"match": {"yelp_businessname": "Computer"}}
],
"should": [
{"match": {"yelp_state": "CT"}},
{"match_phrase": {"yelp_category": "flooring"}}
]
}
}
}
}
}
我有一个偶大的查询,我的第一个查询正确后,我必须转换。
sql_2 = "SELECT * FROM dd_s3data WHERE (yelp_address = '370 Barren Rd' OR yelp_businessname ILIKE '%Computer%') AND (yelp_state = 'CT' OR yelp_category ILIKE '%flooring%') AND yelp_noofreviews < 3.0 AND yelp_noofrating > 3.0;"
如何转换我的SQL查询,使我得到一个新的查询。AND
结果 OR
?
对于 "OR",你可以使用 "should",最小_should_match:1。
对于 "和",你可以使用 "必须"
如果你不想计算搜索结果的分数,那么就使用filter.constant_score -返回每个匹配的文档,其相关性分数等于boost参数值。
在你的例子中,单单使用filter就足够了,如果你想使用constant_score,那么就用constant_score包装filter查询,然后使用boost。
Query1.查询1:查询2:查询3:查询4:查询5
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match_phrase": {
"yelp_address": "370 Barren Rd"
}
},
{
"match": {
"yelp_businessname": "Computer"
}
}
],
"minimum_should_match":1
}
},
{
"bool": {
"should": [
{
"match": {
"yelp_state": "CT"
}
},
{
"match_phrase": {
"yelp_category": "flooring"
}
}
],
"minimum_should_match":1
}
}
]
}
}
}
}
}
查询2:
{
"query": {
"bool": {
"filter": {
"bool": {
"must": [
{
"bool": {
"should": [
{
"match_phrase": {
"yelp_address": "370 Barren Rd"
}
},
{
"match": {
"yelp_businessname": "Computer"
}
}
]
}
},
{
"bool": {
"should": [
{
"match": {
"yelp_state": "CT"
}
},
{
"match_phrase": {
"yelp_category": "flooring"
}
}
],
"minimum_should_match":1
}
},
{
"range": {
"yelp_noofreviews": {
"lt": 3
}
}
},
{
"range": {
"yelp_noofrating": {
"gt": 3
}
}
}
]
}
}
}
}
}