ElasticSearch13:高亮查询

ElasticSearch13:高亮查询

高亮查询

高亮的前提时进行分词,type为text。这里description就是text
# 查看索引映射信息 GET /products/_mapping
notion image
# 高亮。高亮的前提时进行分词,type为text GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "fields": { "*":{} } } }
notion image
也可以自定义高亮
#自定义高亮 GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "pre_tags": ["<span style='color:red;'>"], "post_tags": ["</span>"], "require_field_match": "false", "fields": { "*":{} } } }
notion image

返回指定条数[size]

指定查询结果中返回指定条数。默认是10条
# 指定条数 GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "fields": { "*":{} } }, "size": 1 }

分页查询[form]

(page-1)*size
# 指定条数 GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "fields": { "*":{} } }, "from": 0, "size": 10, }

指定字段排序[sort]

des降序,asc升序
# 排序 GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "fields": { "*":{} } }, "from": 0, "size": 10, "sort": [ { "price": { "order": "desc" } } ] }

返回指定字段[_source]

_source是一个数组,在数组中用来指定展示哪些字段
# 指定字段查询 GET /products/_search { "query": { "query_string": { "default_field": "description", "query": "雪融融" } }, "highlight": { "fields": { "*":{} } }, "from": 0, "size": 10, "sort": [ { "price": { "order": "desc" } } ], "_source": ["id","title","description"]