首页 > 解决方案 > im not able to convert my mysql query into elasticsearch query

问题描述

I'm a beginner in Elasticsearch. I recently changed my search db from MySQL to Elasticsearch but I am not able to convert one of my queries. The query is given below.

select * from table 
where (col1 = "a" and col2 = "b") 
or (col1 = "c" and col2 = "d") 
and col3 = "z";

标签: spring-bootelasticsearchelasticsearch-5spring-data-elasticsearch

解决方案


这一切都是关于必须和应该一起过滤来锻炼这样的查询。

以下查询是您的 SQL 语句的弹性搜索查询。

GET _search
{
  "query": {
    "bool": {
      "must": [
        {
          "term": {
            "col3": {
              "value": "z"
            }
          }
        },
        {
          "bool": {
            "should": [
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "col1": {
                          "value": "a"
                        }
                      }
                    },
                    {
                      "term": {
                        "col2": {
                          "value": "b"
                        }
                      }
                    }
                  ]
                }
              },
              {
                "bool": {
                  "must": [
                    {
                      "term": {
                        "col1": {
                          "value": "c"
                        }
                      }
                    },
                    {
                      "term": {
                        "col2": {
                          "value": "d"
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

推荐阅读