将 SQL 转换为 Elasticsearch DSL for AND case

Converting SQL into Elasticsearch DSL for AND case

我有一个 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" 与 minimum_should_match:1

一起使用

对于 "AND" 你可以使用 "must"

如果您不想计算搜索结果的分数,则使用过滤器。 constant_score -returns 每个匹配文档的相关性得分等于 boost 参数值。

在你的情况下,单独的过滤器可能就足够了,如果你想使用 constant_score 然后用 constant_score 包装过滤器查询并使用 boost

查询 1:

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