Mysql 从sql到弹性搜索查询的转换
我想换福勒。sql查询到弹性json查询Mysql 从sql到弹性搜索查询的转换,mysql,sql,elasticsearch,aggregation,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 在Elasticsearch查询DSL中,第一个查询可以这样翻译: curl -XPOST localhost:9200/table/_s
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
在Elasticsearch查询DSL中,第一个查询可以这样翻译:
curl -XPOST localhost:9200/table/_search -d '{
"size": 0,
"query": {
"filtered": {
"filter": {
"bool": {
"must": [
{
"term": {
"status2": "abc"
}
},
{
"range": {
"cr_date": {
"gt": "date1", <--- don't forget to change the date
"lt": "date2" <--- don't forget to change the date
}
}
}
],
"must_not": [
{
"term": {
"status1": "xyz"
}
}
]
}
}
}
},
"aggs": {
"by_cities": {
"terms": {
"field": "city_id"
},
"aggs": {
"fk_count": {
"cardinality": {
"field": "fk_id"
}
}
}
}
}
}'
curl-XPOST localhost:9200/table/\u search-d'{
“大小”:0,
“查询”:{
“过滤”:{
“过滤器”:{
“布尔”:{
“必须”:[
{
“期限”:{
“状态2”:“abc”
}
},
{
“范围”:{
“确认日期”:{
“gt”:“date1”,在弹性搜索中使用Sql API,我们可以编写查询,也可以将它们转换为弹性查询
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"
}
}
]
}
现在我们可以使用此结果查询弹性搜索
有关更多详细信息,请浏览本文
{
"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"
}
}
]
}