本章学习ElasticSearch全文检索的相关知识
1.概念
我们知道,ES在全文检索、高亮这方面做的十分出色,让我们一起揭开它的神秘面纱!
我们首先构造下数据(附带上ik中文分词器哦)!
创建新的索引jwang01并构造数据:
PUT http://localhost:9200/jwang01
{
"settings": {
"index": {
"number_of_shards": 1,
"number_of_replicas": 0
}
},
"mappings": {
"properties": {
"name": {
"type": "text"
},
"age": {
"type": "integer"
},
"email": {
"type": "keyword"
},
"hobby": {
"type": "text",
"analyzer":"ik_max_word"
}
}
}
}
接下来导入相关的数据:
POST http://localhost:9200/jwang01/_bulk
{"create":{"_index":"jwang01","_type":"_doc","_id":"1001"}}
{"name":"张三","age":30,"email":"[email protected]","hobby":"打篮球、踢足球、听音乐"}
{"create":{"_index":"jwang01","_type":"_doc","_id":"1002"}}
{"name":"李四","age":33,"email":"[email protected]","hobby":"踢足球、下象棋、追剧"}
{"create":{"_index":"jwang01","_type":"_doc","_id":"1003"}}
{"name":"王五","age":26,"email":"[email protected]","hobby":"打网球、踢毽子、听音乐"}
{"create":{"_index":"jwang01","_type":"_doc","_id":"1004"}}
{"name":"赵六","age":26,"email":"[email protected]","hobby":"打篮球、玩游戏、追剧"}
注意:ES默认的文档类型为_doc
查看数据是否创建成功:
2.单词查询
当我们仅仅查询一个单词的时候,我们可以这样做:
POST http://localhost:9200/jwang01/_search
{
"query": {
"match": {
"hobby": "篮球"
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
查询结果如下所示:
3.多词查询
有些情况下我们需要匹配多个单词,那么就是多词查询。
POST http://localhost:9200/jwang01/_search
{
"query": {
"match": {
"hobby": "篮球 足球"
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
但有些时候我们并不是这样想的,我们想要既有篮球又有足球的数据!
POST http://localhost:9200/jwang01/_search
{
"query": {
"match": {
"hobby": {
"query":"篮球 足球",
"operator":"and"
}
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
那么我们就仅仅查出了这一条符合条件的数据啦
or和and是两种极端的查询结果。有些时候我们需要一定的匹配度就行了,就是我们之前的元数据_source的分数,分数越高匹配度越大。
范例:设置匹配度40%进行查询
POST http://localhost:9200/jwang01/_search
{
"query": {
"match": {
"hobby": {
"query":"篮球 足球",
"minimum_should_match":"40%"
}
}
},
"highlight": {
"fields": {
"hobby": {}
}
}
}
查询结果如下:
匹配度的设置需要根据生产环境灵活的进行选择。