这里总结一下es完全映射mysql的搜索语句,希望能够帮到学习es的同学们。
实例
这里我们假设mysql表有5个字段: birthday, name, sex, height, address
实例1: mysql 子查询+or
select * from
(select * from table where birthday > '2018-05-17' and birthday < '2018-05-17' and name = 'xx')
where sex = 1 or height = 183
es的搜索语句是:
{
"query": {
"bool": {
"filter": [
{
"range": {
"birthday": {
"from": "2018-05-17",
"include_lower": true,
"include_upper": true,
"to": null
}
}
},
{
"range": {
"birthday": {
"from": null,
"include_lower": true,
"include_upper": true,
"to": "2018-05-24"
}
}
},
{
"match": {
"name ": {
"query": "xx" // 这里不应该对应mysql的=, term应该是=. match用到的是es里面的分词,但mysql找不到与之匹配的.
}
}
}
],
"should": [
{
"term": {
"sex": 1
}
},
{
"term": {
"height ": 183
}
}
]
}
}
}