Elasticsearch (九)搜索排序和高亮
程序员文章站
2022-07-05 14:25:53
...
准备测试案例:
DELETE blog
PUT blog
GET blog/_mapping
PUT blog/article/_mapping
{
"dynamic":"strict",
"properties": {
"id":{
"type": "long"
},
"title":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"content":{
"type": "text",
"analyzer": "ik_max_word",
"search_analyzer": "ik_max_word"
},
"postdate":{
"type": "date",
"format": "yyyy-MM-dd HH:mm || yyyy-MM-dd HH:mm:ss || yyyy-MM-dd || epoch_millis"
}
}
}
PUT blog/article/1
{
"id":1,
"title":"git简介",
"content":"Svn与Git的最主要区别。",
"postdate":"2016-06-09"
}
PUT blog/article/2
{
"id":2,
"title":"Java中泛型的介绍与简单应用",
"content":"学习目标 掌握泛型的产生意义。",
"postdate":"2016-08-06"
}
PUT blog/article/3
{
"id":3,
"title":"SQL的基本操作",
"content":"基本操作 CRUD。",
"postdate":"2017-05-06"
}
PUT blog/article/4
{
"id":4,
"title":"Hibernate框架基础",
"content":"Hibernate框架基础。",
"postdate":"2017-08-03"
}
PUT blog/article/5
{
"id":5,
"title":"Shell基本知识",
"content":"Shell是什么。",
"postdate":"2017-11-03"
}
1.match_all 查询
查询一个索引中的全部文档
GET blog/_search 与
GET blog/_search
{
"query":{
"match_all":{}
}
}
查询结果相同
查询结果:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 1,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "5",
"_score": 1,
"_source": {
"id": 5,
"title": "Shell基本知识",
"content": "Shell是什么。",
"postdate": "2017-11-03"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "4",
"_score": 1,
"_source": {
"id": 4,
"title": "Hibernate框架基础",
"content": "Hibernate框架基础。",
"postdate": "2017-08-03"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "git简介",
"content": "Svn与Git的最主要区别。",
"postdate": "2016-06-09"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "3",
"_score": 1,
"_source": {
"id": 3,
"title": "SQL的基本操作",
"content": "基本操作 CRUD。",
"postdate": "2017-05-06"
}
}
]
}
}
2.term 查询
词项查询,需要精确匹配
GET blog/_search
{
"query": {
"term": {
"title": {
"value": "java"
}
}
}
}
查询结果:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.561078,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 0.561078,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
}
}
]
}
}
3.terms 查询
多词项查询,精确匹配,字段中包含其中一个即可term即可匹配
GET blog/_search
{
"query": {
"terms": {
"title": [
"java",
"shell"
]
}
}
}
查询结果:
{
"took": 7,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.561078,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 0.561078,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "5",
"_score": 0.2876821,
"_source": {
"id": 5,
"title": "Shell基本知识",
"content": "Shell是什么。",
"postdate": "2017-11-03"
}
}
]
}
}
4.查询高亮
词项查询,精确匹配,并高亮
GET blog/_search
{
"query": {
"term": {
"title": {
"value": "java"
}
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
查询结果:
{
"took": 98,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.561078,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 0.561078,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
},
"highlight": {
"title": [
"<em>Java</em>中泛型的介绍与简单应用"
]
}
}
]
}
}
5.match 查询
对查询语句分词,再term查询
GET blog/_search
{
"query": {
"match": {
"title": "java框架"
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
查询结果:
{
"took": 32,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.5072953,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "4",
"_score": 1.5072953,
"_source": {
"id": 4,
"title": "Hibernate框架基础",
"content": "Hibernate框架基础。",
"postdate": "2017-08-03"
},
"highlight": {
"title": [
"Hibernate<em>框架</em>基础"
]
}
},
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 0.561078,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
},
"highlight": {
"title": [
"<em>Java</em>中泛型的介绍与简单应用"
]
}
}
]
}
}
6.multi_match查询
对查询语句分词,再term查询,fields中任何一个字段包含分词后的term都可以被匹配
GET blog/_search
{
"query": {
"multi_match": {
"query": "java svn",
"fields": ["title","content"]
}
},
"highlight": {
"fields": {
"title": {}
}
}
}
查询结果:
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.561078,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 0.561078,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
},
"highlight": {
"title": [
"<em>Java</em>中泛型的介绍与简单应用"
]
}
},
{
"_index": "blog",
"_type": "article",
"_id": "1",
"_score": 0.26742277,
"_source": {
"id": 1,
"title": "git简介",
"content": "Svn与Git的最主要区别。",
"postdate": "2016-06-09"
}
}
]
}
}
7.range 查询
范围查询 gte 大于或等于,gt 大于,lte 小于或等于,lt 小于
GET blog/_search
{
"query": {
"range": {
"postdate": {
"gte": "2016-01-01",
"lte": "2016-12-31"
}
}
}
}
查询结果:
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": 1,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
}
},
{
"_index": "blog",
"_type": "article",
"_id": "1",
"_score": 1,
"_source": {
"id": 1,
"title": "git简介",
"content": "Svn与Git的最主要区别。",
"postdate": "2016-06-09"
}
}
]
}
}
8.搜索排序
按发布日期降序排序
GET blog/_search
GET blog/_search
{
"query":{
"match_all":{}
},
"sort": [
{
"postdate": {
"order": "desc"
}
}
]
}
查询结果:
{
"took": 22,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": null,
"hits": [
{
"_index": "blog",
"_type": "article",
"_id": "5",
"_score": null,
"_source": {
"id": 5,
"title": "Shell基本知识",
"content": "Shell是什么。",
"postdate": "2017-11-03"
},
"sort": [
1509667200000
]
},
{
"_index": "blog",
"_type": "article",
"_id": "4",
"_score": null,
"_source": {
"id": 4,
"title": "Hibernate框架基础",
"content": "Hibernate框架基础。",
"postdate": "2017-08-03"
},
"sort": [
1501718400000
]
},
{
"_index": "blog",
"_type": "article",
"_id": "3",
"_score": null,
"_source": {
"id": 3,
"title": "SQL的基本操作",
"content": "基本操作 CRUD。",
"postdate": "2017-05-06"
},
"sort": [
1494028800000
]
},
{
"_index": "blog",
"_type": "article",
"_id": "2",
"_score": null,
"_source": {
"id": 2,
"title": "Java中泛型的介绍与简单应用",
"content": "学习目标 掌握泛型的产生意义。",
"postdate": "2016-08-06"
},
"sort": [
1470441600000
]
},
{
"_index": "blog",
"_type": "article",
"_id": "1",
"_score": null,
"_source": {
"id": 1,
"title": "git简介",
"content": "Svn与Git的最主要区别。",
"postdate": "2016-06-09"
},
"sort": [
1465430400000
]
}
]
}
}
上一篇: 弟弟的安慰
下一篇: CDR怎么设计规则图案的底纹效果?
推荐阅读
-
Android实现ListView的A-Z字母排序和过滤搜索功能 实现汉字转成拼音
-
[TextMatch框架] 基于召回和排序的文本搜索
-
JavaScript对JSON数据进行排序和搜索
-
【C++ STL学习笔记】C++ STL常用算法(排序、合并、搜索和分区)
-
8、搜索排序和搜索过滤(lucene笔记)
-
Android实现ListView的A-Z字母排序和过滤搜索功能,实现汉字转成拼音
-
Django实现drf搜索过滤和排序过滤
-
ElasticSearch 6.x 学习笔记:19.搜索高亮
-
【ElasticSearch】高亮搜索
-
ElasticSearch 搜索引擎入门到实战 22-- 查询出的结果高亮显示