从sql转换为弹性搜索查询

时间:2015-09-19 18:45:45

标签: mysql sql elasticsearch aggregation

我想要传达这一点。 sql查询到弹性json查询

select count(distinct(fk_id)),city_id from table 
where status1 != "xyz" and satus2 = "abc" and 
cr_date >="date1" and cr_date<="date2" group by city_id

还有任何方法可以用弹性方式编写嵌套查询。

select * from table where status in (select status from table2)

1 个答案:

答案 0 :(得分:0)

使用Sql API在Elastic搜索中,我们可以编写查询,也可以将它们转换为Elastic查询

 POST /_sql/translate
{
    "query": "SELECT * FROM customer where address.Street='JanaChaitanya Layout' and Name='Pavan Kumar'"
}

对此的答复是

{
  "size" : 1000,
  "query" : {
    "bool" : {
      "must" : [
        {
          "term" : {
            "address.Street.keyword" : {
              "value" : "JanaChaitanya Layout",
              "boost" : 1.0
            }
          }
        },
        {
          "term" : {
            "Name.keyword" : {
              "value" : "Pavan Kumar",
              "boost" : 1.0
            }
          }
        }
      ],
      "adjust_pure_negative" : true,
      "boost" : 1.0
    }
  },
  "_source" : {
    "includes" : [
      "Name",
      "address.Area",
      "address.Street"
    ],
    "excludes" : [ ]
  },
  "docvalue_fields" : [
    {
      "field" : "Age"
    }
  ],
  "sort" : [
    {
      "_doc" : {
        "order" : "asc"
      }
    }
  ]
}

现在我们可以使用此结果查询弹性搜索 有关更多详细信息,请阅读本文

https://xyzcoder.github.io/elasticsearch/2019/06/25/making-use-of-sql-rest-api-in-elastic-search-to-write-queries-easily.html