赞
踩
1.git clone后,easy-es-core中的pom中需要引入:
- <dependency>
- <groupId>org.apache.httpcomponents</groupId>
- <artifactId>httpcore</artifactId>
- <version>4.4.12</version>
- </dependency>
2.easy-es-sample 中提供了基本案例,可以用来解析源码。
3.easy-es-common中的pom里可以看到,它是基于elasticsearch-rest-high-level-client的。
如果不熟悉elasticsearch-rest-high-level-client,建议先熟悉一下。
查询所有:match_all (一般也就是测试用用)
全文检索:利用分词器对用户输入的内容进行分词后进行匹配查询
match_query,multi_match_query
精确查询:根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等
ids,range,term
地理(geo)查询:根据经纬度查询。
geo_distance,geo_bounding_box
复合查询:将上述查询条件组合起来,合并查询条件
bool,function_score
对用户输入的内容进行分词后进行匹配查询
- GET /easyes_document/_search
- {
- "query": {
- "match_all": {}
- }
- }
-
- GET /easyes_document/_search
- {
- "query": {
- "match": {
- "title": "老王"
- }
- }
- }
-
- GET /easyes_document/_search
- {
- "query": {
- "multi_match": {
- "query": "老王",
- "fields": ["content","creator"]
- }
- }
- }

根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等,不会对用户输入的内容进行分词
term:根据词条精确值查询
range:根据值的范围查询
- #term 的使用
-
- GET /easyes_document/_search
- {
- "query": {
- "term": {
- "_id": {
- "value": "3"
- }
- }
- }
- }
-
- GET /easyes_document/_search
- {
- "query": {
- "range": {
- "star_num": {
- "gte": 20
- }
- }
- }
- }
-
- GET /easyes_document/_search
- {
- "query": {
- "range": {
- "star_num": {
- "lte": 5
- }
- }
- }
- }
-
- GET /easyes_document/_search
- {
- "query": {
- "range": {
- "gmt_create": {
- "lte": "2023-06-18 07:38:43"
- }
- }
- }
- }

如果没有提示需要硬写:
- GET /easyes_document/_search
- {
- "query": {
- "geo_distance": {
- "location": "40.13,116.63",
- "distance": "2km"
- }
- }
- }
term默认对keyword中文无效,需要field.keyword才可以。
- #function score query
- GET /easyes_document/_search
- {
- "query": {
- "function_score": {
- "query": {
- "match": {
- "title": "老王"
- }
- },
- "functions": [
- {
- "filter": {
- "term": {
- "sub_title.keyword": "小毛子"
- }
- },
- "weight":100
- }
- ]
- }
- }
- }

布尔查询是一个或多个查询子句的组合。子查询的组合方式有:
找汉子,把地理位置写在must中和放在filter中得分是不一样的
- #bool query
- GET /easyes_document/_search
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {"title" : "测试"}
- },
- {
- "match": {"creator" : "老汉"}
- },
- {
- "geo_distance": {
- "geo_location": "POINT (34.400544 73.53028599999999)",
- "distance": "500km"
- }
- }
- ],
- "must_not": [
- {
- "range": {
- "star_num": {
- "gte": 10,
- "lte": 20
- }
- }
- }
- ]
- }
- }
- }

- #bool query
- GET /easyes_document/_search
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {"title" : "测试"}
- },
- {
- "match": {"creator" : "老汉"}
- }
- ],
- "must_not": [
- {
- "range": {
- "star_num": {
- "gte": 10,
- "lte": 20
- }
- }
- }
- ],
- "filter": [
- {
- "geo_distance": {
- "geo_location": "POINT (34.400544 73.53028599999999)",
- "distance": "500km"
- }
- }
- ]
- }
- }
- }

sort集合,写在前面的起主要作用
- #sort
- GET /easyes_document/_search
- {
- "query": {
- "function_score": {
- "query": {
- "match": {
- "title": "老王"
- }
- },
- "functions": [
- {
- "filter": {
- "term": {
- "sub_title.keyword": "小毛子"
- }
- },
- "weight":100
- }
- ]
- }
- },
- "sort": [
-
- {"star_num": "asc"},
- {"_score" : "desc"}
- ]
- }

- GET /easyes_document/_search
- {
- "query": {
- "match": {
- "title": "测试"
- }
- },
- "sort": [
- {
- "star_num": {
- "order": "desc"
- }
- }
- ],
- "from": 10,
- "size": 10
- }

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。