elasticsearch实现类似于sql的like,or,and,in,大于等复杂查询
首页->学习资料->微服务治理->elasticsearch 关键词: 发布时间:2020-04-05 06:11:57 浏览次数:1041

基于之前的数据,我们要实现一个类似这样的查询:

select * from users 
where (name like '%张三%' or name like '%李四%') 
	and sex=1 and city_id in (1,2)
	and (address like '%广州%' or address like '%深圳%') 
	and create_time>'2020-04-04 12:30:30'
order by position desc
limit 10,2
post 

 match用于text字段查询,如果有多个条件中间用空格隔开
 term:如果是等于时用
 terms:如果是in操作时用


{
	"from":0,
	"size":10,
    "query": {
        "bool": {
            "must": [
                {
                    "match": {
                        "name": "张三 李四"
                    }
                },
                {
                	"terms":{
                    	"sex":[1,2]
                    }
                },
                {
                	"terms":{
                    	"city_id":[1,2]
                    }
                },
                {
                	"match":{
                    	"city_name":"广州 深圳"
                    }
                },
                {
                	"range":{
                    	"create_time":{
                    		"gt":"2020-04-04 15:03:11"
                    	}
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "_geo_distance": {
                "location": {
                    "lat": 23.265706,
                    "lon": 113.261805
                },
                "order": "asc",
                "unit": "km"
            }
        }
    ],
    "highlight": {
        "pre_tags": [
            "<font color=red>",
            "<tag2>"
        ],
        "post_tags": [
            "</font>",
            "</tag2>"
        ],
        "fields": {
            "description": {}
        }
    }
}


查询结果:

查询结果中的sort为距离

{
    "took": 4,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": null,
        "hits": [
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "B_PTRHEBHFJU5U-vVzVn",
                "_score": null,
                "_source": {
                    "id": "3",
                    "tel": "13533592803",
                    "name": "张三",
                    "sex": "1",
                    "city_id": "2",
                    "city_name": "广州市",
                    "location": {
                        "lat": 23.265706,
                        "lon": 113.261805
                    },
                    "create_time": "2020-04-04 16:03:12",
                    "address": "广东省广州市天河客运站"
                },
                "sort": [
                    0.0
                ]
            },
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "BvPTRHEBHFJU5U-vVzVn",
                "_score": null,
                "_source": {
                    "id": "2",
                    "tel": "13533592802",
                    "name": "张三花",
                    "sex": "2",
                    "city_id": "1",
                    "city_name": "深圳市",
                    "location": {
                        "lat": 22.632069,
                        "lon": 114.062088
                    },
                    "create_time": "2020-04-04 15:58:52",
                    "address": "广东省深圳市龙岗区坂田站"
                },
                "sort": [
                    108.06943106175012
                ]
            },
            {
                "_index": "student",
                "_type": "_doc",
                "_id": "BfPTRHEBHFJU5U-vVzVn",
                "_score": null,
                "_source": {
                    "id": "1",
                    "tel": "13533592801",
                    "name": "张三丰",
                    "sex": "1",
                    "city_id": "1",
                    "city_name": "深圳市",
                    "location": {
                        "lat": 22.719571,
                        "lon": 114.364493
                    },
                    "create_time": "2020-04-04 15:57:20",
                    "address": "广东省深圳市龙岗区坪山站"
                },
                "sort": [
                    128.17103318459294
                ]
            }
        ]
    }
}



再来一个例子:

{
	"from":0,
	"size":10,
    "query": {
			"bool":{
                "must":[
                    {"match":{"vcc_id":"2000820"}},
                    {"match":{"flow_id":"460"}},
                    {"range":{"create_time":{"gte":"1547801073"}}},
                    {"range":{"create_time":{"lte":"1642495473"}}},
                    {
                        "bool":{
                            "should":[
                                    {"match":{"ticket_no":"460210118000001"}},
                                    {"match":{"client_phone":"13760139234"}},
                                    {"match":{"node_1715_form.nvPun11548055909540":"鄂ley981"}},
                                    {"match":{"node_1715_form.bD8i5R1563328936844":"fuwukahao"}}
                                ]
                            }
                    }
                ]
            }
		},
    "highlight": {
        "pre_tags": [
            "<font color=red>",
            "<tag2>"
        ],
        "post_tags": [
            "</font>",
            "</tag2>"
        ],
        "fields": {
            "description": {}
        }
    },
    "sort": [{
			"create_time": {
				"order": "desc",
				"unmapped_type": "date"
			}
		}]
}


赞:(0)
踩:(0)
相关文章
elasticsearch插入时报错:invalid
无法访问虚拟机上的elasticsearch
elasticsearch scroll方式查询时sc
elasticsearch批量更新数据
centos7.4下安装elasticsearch
php中elasticsearch scroll方式查
Elasticsearch 实时分布式搜索分析
Elasticsearch 权威指南(中文版)
elasticsearch查询实例-模拟数据
elasticsearch-建立indice(相当于s
热门文章
win7中将文件拷贝到虚拟机linux下
phpexcel设置行高及列宽,背景颜色,
rabbitmq无法启动
intellij idea不显示git push按钮
php7中使用mongodb的aggregate进行
centos7.4 64位下swoole安装及配置
laravel页面静态化的方法
navicate连接mycat报1184错误
单点登录sso原理及php实现方式及de
devops-jenkins容器为pending状态
好评文章
phpexcel设置行高及列宽,背景颜色,
php7中使用mongodb的aggregate进行
intellij idea打开文件所在文件夹
windows下使用MongoDB Compass Com
win7中将文件拷贝到虚拟机linux下
laravel 中悲观锁 & 乐观锁的使用
单点登录sso原理及php实现方式及de
navicate连接mycat报1184错误
rabbitmq无法启动
laravel整合dingo/api方法步骤:jwt
标签
rabbitmq mysql备份 elasticsearch golang swoole
我的项目
【github】www.github.com/hurong241
【码云】gitee.com/hu_rong/projects
【docker hub】hub.docker.com/repositories/hurong241
【packagist】packagist.org/users/hurong241/packages
站点信息
建站时间:2011年
文章数:607篇
浏览数:940840
粤ICP备18028092号-1  微信:hurong241