【问题标题】:Converting SQL into Elasticsearch DSL for AND case将 SQL 转换为 Elasticsearch DSL for AND case
【发布时间】:2020-08-06 14:46:50
【问题描述】:

我有一个这样的 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

【问题讨论】:

    标签: elasticsearch elastic-stack elasticsearch-query


    【解决方案1】:

    对于“OR”,您可以将“should”与 minimum_should_match:1 一起使用

    对于“AND”,您可以使用“must”

    如果您不想计算搜索结果的分数,则使用过滤器。 constant_score - 返回每个匹配的文档,其相关性得分等于 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
                    }
                  }
                }
              ]
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-11-22
      • 2018-10-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-23
      • 2021-08-18
      • 2020-01-24
      • 2018-04-07
      • 1970-01-01
      相关资源
      最近更新 更多