1. 常见的DSL
(1) 查询所有: 查询出所有数据,一般测试的时候使用,例如: match_all .但是受分页限制,一般返回10条数据
(2) 全文检索(full text)查询:利用分词器对用户输入内容分词,然后去倒排索引中匹配,例如:match_query,multi_match_query
(3) 精确查询:不需要分词的词条,根据精确词条查找数据,一般是查找keyword,数值,日期,boolean等类型字段。例如: ids,range,term
(4) 地理查询:根据经纬度查询,例如:geo_distance,geo_bounding_box
(5) 复合查询:将上述查询条件进行组合,合并查询条件。例如:bool,function_score
2 DSL基本语法
get /hotel/_search
{
"query":{
"查询类型":{
"查询条件":"值"
}
}
}
(1) 查询所有
get /hotel/_search
{
"query":{
"match_all":{}
}
}
(2) 全文检索查询: 会对用户输入内容分词,常用于搜索框搜索
match查询:全文检索查询的一种,会对用户输入的内容进行分词,然后去倒排索引库检索,语法:
get /hotel/_search
{
"query":{
"match":{
"FIELD":"TEXT"
}
}
}
(2.1)重新导入数据,整合地理坐标
@GetMapping("batchInsert")
public void batchInsert() throws IOException {
List<Hotel> hotels = hotelMapper.selectList(null);
BulkRequest request = new BulkRequest();
for (int i = 0; i < hotels.size(); i++) {
HotelDoc doc = new HotelDoc(hotels.get(i));
request.add(new IndexRequest("hotel")
.id(doc.getId().toString())
.source(JSON.toJSONString(doc),XContentType.JSON));
}
BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
System.out.println(bulk.status());
System.out.println(bulk.getTook());
}
分词检索,将搜索词进行分词,然后查询出2条数据
之前在定义mappings的时候,定义了多个字段检索的条件all,因此使用brand检索,也是可以检索出数据的
multi_match
get /hotel/_search
{
"query":{
"multi_match":{
"query":"四季",
"fields": ["brand","name","business"]
}
}
}
搜出结果,建议使用all的方式,不建议使用multi_match的方式
{
"took" : 4,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 2.00488,
"hits" : [
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "3",
"_score" : 2.00488,
"_source" : {
"address" : "洪武北路1号",
"brand" : "四季",
"business" : "新街口商圈",
"city" : "南京市玄武区",
"id" : 3,
"location" : "33.35,131.36",
"name" : "四季",
"pic" : "http://www.bai.com/images/7.png",
"price" : 489,
"score" : 8,
"starName" : "3星"
}
},
{
"_index" : "hotel",
"_type" : "_doc",
"_id" : "4",
"_score" : 1.5579718,
"_source" : {
"address" : "是单独发2号",
"brand" : "四季如春",
"business" : "新街口商圈",
"city" : "南京市鼓楼区",
"id" : 4,
"location" : "66.66,133.36",
"name" : "四季如春",
"pic" : "http://www.qiniu.com/images/xxx.png",
"price" : 999,
"score" : 9,
"starName" : "五星"
}
}
]
}
}