ElasticSearch入门(三)
为了方便接下来的例子演示,我们新创建一个索引名为compony,type为person的文档,里面添加四个人,还是使用postman进行请求
{"name": "zhangsan",
"age":24,
"address":"beijing",
"word":"i like computer music",
"interests":["football","dance"]
}
{"name": "lisi",
"age":25,
"address":"shanghai",
"word":"i like computer game",
"interests":["sing","dance"]
}
{"name": "wangwu",
"age":26,
"address":"guangzhou",
"word":"i like read book",
"interests":["poker","game"]
}
{"name": "zhaoliu",
"age":27,
"address":"chengdu",
"word":"i like game",
"interests":["forestry","game"]
}
一、全文搜索
查找所有喜欢computer game的人:
localhost:9200/compony/person/_search
{
"query" : {
"match" : {
"word" : "computer game"
}
}
}
得到的结果:
{
"took": 225,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 3,
"relation": "eq"
},
"max_score": 1.3494902,
"hits": [
{
"_index": "compony",
"_type": "person",
"_id": "2",
"_score": 1.3494902,
"_source": {
"name": "lisi",
"age": 25,
"address": "shanghai",
"word": "i like computer game",
"interests": [
"sing",
"dance"
]
}
},
{
"_index": "compony",
"_type": "person",
"_id": "4",
"_score": 0.7549127,
"_source": {
"name": "zhaoliu",
"age": 27,
"address": "chengdu",
"word": "i like game",
"interests": [
"forestry",
"game"
]
}
},
{
"_index": "compony",
"_type": "person",
"_id": "1",
"_score": 0.6747451,
"_source": {
"name": "zhangsan",
"age": 24,
"address": "beijing",
"word": "i like computer music",
"interests": [
"football",
"dance"
]
}
}
]
}
}
其中的_score代表相关性得分,Elasticsearch 默认按照相关性得分排序,即每个文档跟查询的匹配程度。我们这里看到即使是zhaoliu的爱好里面没有computer,但是结果也返回了,因为它里面有game,所以也返回了,只是相关性变低了。这也看出来区别于关系型数据库的概念,数据库中的一条记录要么匹配要么不匹配。
二、短语搜索
找出一个属性中的独立单词是没有问题的,但有时候想要精确匹配一系列单词或者短语 。 比如, 我们想执行这样一个查询,仅匹配同时包含 “computer” 和 “game” ,并且 二者以短语 “computer game” 的形式紧挨着的人员记录。
localhost:9200/compony/person/_search
{
"query" : {
"match_phrase" : {
"word" : "computer game"
}
}
}
注意这里与全文搜索变化的地方只有match变为了match_phrase,返回结果:
{
"took": 4,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3494902,
"hits": [
{
"_index": "compony",
"_type": "person",
"_id": "2",
"_score": 1.3494902,
"_source": {
"name": "lisi",
"age": 25,
"address": "shanghai",
"word": "i like computer game",
"interests": [
"sing",
"dance"
]
}
}
]
}
}
这样的查询,只匹配到了lisi这个人
三、高亮搜索
许多应用都倾向于在每个搜索结果中 高亮 部分文本片段,以便让用户知道为何该文档符合查询条件。在 Elasticsearch 中检索出高亮片段也很容易。
再次执行前面的查询,并增加一个新的 highlight
参数:
{
"query" : {
"match_phrase" : {
"word" : "computer game"
}
},
"highlight":{
"fields":{
"word":{}
}
}
}
查询结果:
{
"took": 31,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 1.3494902,
"hits": [
{
"_index": "compony",
"_type": "person",
"_id": "2",
"_score": 1.3494902,
"_source": {
"name": "lisi",
"age": 25,
"address": "shanghai",
"word": "i like computer game",
"interests": [
"sing",
"dance"
]
},
"highlight": {
"word": [
"i like <em>computer</em> <em>game</em>"
]
}
}
]
}
}
这样的效果可能与百度搜索那种类似,这只是我的猜测:
官网给出的基本入门也就这么多了,其它的后续看情况要不要做几个demo,现目前计划后续整合springboot在代码层面来展示