既然是说Elasticsearch全文搜索引擎,那么笔者就在Elasticsearch中的一些简单的搜索来入门Elasticsearch,借用官网的一句话“你知道的,为了搜索...”
进行搜索前,先下载安装Sense,可参考该篇文章https://www.cnblogs.com/cnjavahome/p/9124584.html
当然!记得启动Elasticsearch
其次在来看一张图,理解什么是ES集群、索引、文档、属性,方便后文的理解
PUT /myindex/mydoc/1
{
"name":"qianran",
"age":20,
"work":"java",
"about":"I love to go rock climbing"
}
PUT /myindex/mydoc/2
{
"name":"linjie",
"age":29,
"work":"java",
"about":"I want to climbing"
}
PUT /myindex/mydoc/3
{
"name":"jay",
"age":23,
"work":"C++",
"about":"happy.."
}
PUT /myindex/mydoc/4
{
"name":"lucy",
"age":22,
"work":"java",
"about":"xixi.."
}
若返回结果如下,即代表添加数据成功
{
"_index": "myindex",
"_type": "mydoc",
"_id": "4",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
GET /myindex/mydoc/1
即可返回相应的文档
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
}
}
GET /myindex/mydoc/_search
即可返回所有文档
{
"took": 168,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 4,
"max_score": 1,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 1,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "4",
"_score": 1,
"_source": {
"name": "lucy",
"age": 22,
"work": "java",
"about": "xixi.."
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_score": 1,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "3",
"_score": 1,
"_source": {
"name": "jay",
"age": 23,
"work": "C++",
"about": "happy.."
}
}
]
}
}
使用_search端点,并将查询本身赋值给参数q
GET /myindex/mydoc/_search?q=work:java
即可返回work是java的文档
{
"took": 61,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.2876821,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 0.18232156,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "4",
"_score": 0.18232156,
"_source": {
"name": "lucy",
"age": 22,
"work": "java",
"about": "xixi.."
}
}
]
}
}
它支持构建更加复杂和健壮的查询
GET /myindex/mydoc/_search
{
"query": {
"match": {
"work":"java"
}
}
}
即可返回work是java的文档
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 3,
"max_score": 0.2876821,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_score": 0.2876821,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 0.18232156,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "4",
"_score": 0.18232156,
"_source": {
"name": "lucy",
"age": 22,
"work": "java",
"about": "xixi.."
}
}
]
}
}
work:java age大于25
gt:_great than 表示大于
当然还有许多,如下:
eq相等 ne、neq不相等, gt大于, lt小于 gte、ge大于等于 lte、le 小于等于 not非 mod求模 is [not] div by是否能被某数整除 is [not] even是否为偶数 is [not] even by $b即($a / $b) % 2 == 0 is [not] odd是否为奇 is not odd by $b即($a / $b) % 2 != 0
GET /myindex/mydoc/_search
{
"query" : {
"bool": {
"must": {
"match" : {
"work" : "java"
}
},
"filter": {
"range" : {
"age" : { "gt" : 25 }
}
}
}
}
}
即可返回age大于25,并且work是java的文档
{
"took": 70,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.18232156,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 0.18232156,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
}
]
}
}
在全文中搜索某句,得到文档结果 结果会有_score属性,即相关性得分,得到分数越高,匹配程度越高
GET /myindex/mydoc/_search
{
"query": {
"match": {
"about":"climbing"
}
}
}
即可返回about属性中有climbing的文档,并且有_score属性来显示相关性得分
{
"took": 23,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 0.5565415,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 0.5565415,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_score": 0.26742277,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
}
}
]
}
}
精确搜索某短语,需要将match改成match_phrase
GET /myindex/mydoc/_search
{
"query": {
"match_phrase": {
"about":"I want"
}
}
}
即可返回有about中有I want,如果是全文搜索,只要有I的也会被搜索出来,这就是短语搜索和全文搜素的区别
{
"took": 28,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.113083,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 1.113083,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
}
}
]
}
}
让用户知道为何该文档符合查询结果
GET /myindex/mydoc/_search
{
"query": {
"match": {
"about":"I want"
}
},
"highlight": {
"fields": {
"about":{}
}
}
}
即可返回有I 或者有want 的文档,并且通过标签进行高亮
{
"took": 56,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.113083,
"hits": [
{
"_index": "myindex",
"_type": "mydoc",
"_id": "2",
"_score": 1.113083,
"_source": {
"name": "linjie",
"age": 29,
"work": "java",
"about": "I want to climbing"
},
"highlight": {
"about": [
"I want to climbing"
]
}
},
{
"_index": "myindex",
"_type": "mydoc",
"_id": "1",
"_score": 0.26742277,
"_source": {
"name": "qianran",
"age": 20,
"work": "java",
"about": "I love to go rock climbing"
},
"highlight": {
"about": [
"I love to go rock climbing"
]
}
}
]
}
}
以上就是ES的简单搜索