从best-fields
换成most-fields
策略best-fields
策略,主要是说将某一个field匹配尽可能多的关键词的doc优先返回回来most-fields
策略,主要是说尽可能返回更多field匹配到某个关键词的doc,优先返回回来
POST /forum/_mapping
{
"properties": {
"sub_title": {
"type": "string",
"analyzer": "english",
"fields": {
"std": {
"type": "string",
"analyzer": "standard"
}
}
}
}
}
POST /forum/_mapping
{
"properties": {
"sub_title": {
"type": "text",
"analyzer": "english",
"fields": {
"std": {
"type": "text",
"analyzer": "standard"
}
}
}
}
}
POST /forum/_bulk
{ "update": { "_id": "1"} }
{ "doc" : {"sub_title" : "learning more courses"} }
{ "update": { "_id": "2"} }
{ "doc" : {"sub_title" : "learned a lot of course"} }
{ "update": { "_id": "3"} }
{ "doc" : {"sub_title" : "we have a lot of fun"} }
{ "update": { "_id": "4"} }
{ "doc" : {"sub_title" : "both of them are good"} }
{ "update": { "_id": "5"} }
{ "doc" : {"sub_title" : "haha, hello world"} }
{ "update": { "_id": "6"} }
{ "doc" : {"sub_title" : "learn java"} }
{ "update": { "_id": "7"} }
{ "doc" : {"sub_title" : "learned a lot of hadoop"} }
{ "update": { "_id": "8"} }
{ "doc" : {"sub_title" : "learning of flink"} }
{ "update": { "_id": "9"} }
{ "doc" : {"sub_title" : "the spark is good bigdata tool"} }
{ "update": { "_id": "10"} }
{ "doc" : {"sub_title" : "haha, hello world"} }
{ "update": { "_id": "11"} }
{ "doc" : {"sub_title" : "flink, hello world"} }
GET /forum/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}
{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1.219939,
"hits": [
{
"_index": "forum",
"_type": "article",
"_id": "2",
"_score": 1.219939,
"_source": {
"articleID": "KDKE-B-9947-#kL5",
"userID": 1,
"hidden": false,
"postDate": "2017-01-02",
"tag": [
"java"
],
"tag_cnt": 1,
"view_cnt": 50,
"title": "this is java blog",
"content": "i think java is the best programming language",
"sub_title": "learned a lot of course"
}
},
{
"_index": "forum",
"_type": "article",
"_id": "1",
"_score": 0.5063205,
"_source": {
"articleID": "XHDK-A-1293-#fJ3",
"userID": 1,
"hidden": false,
"postDate": "2017-01-01",
"tag": [
"java",
"hadoop"
],
"tag_cnt": 2,
"view_cnt": 30,
"title": "this is java and elasticsearch blog",
"content": "i like to write best elasticsearch article",
"sub_title": "learning more courses"
}
}
]
}
}
sub_title
用的是enligsh analyzer
,所以还原了单词
为什么,因为如果我们用的是类似于english analyzer这种分词器的话,就会将单词还原为其最基本的形态,stemmer
learning --> learn
learned --> learn
courses --> course
sub_titile: learning coureses --> learn course
{ "doc" : {"sub_title" : "learned a lot of course"} }
,就排在了{ "doc" : {"sub_title" : "learning more courses"} }
的前面
GET /forum/_search
{
"query": {
"match": {
"sub_title": "learning courses"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 2.3487744,
"hits" : [
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "1",
"_score" : 2.3487744,
"_source" : {
"articleID" : "XHDK-A-1293-#fJ3",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-01",
"tag" : [
"java",
"hadoop"
],
"tag_cnt" : 2,
"view_cnt" : 30,
"title" : "this is java and hadoop blog",
"content" : "i like to write best elasticsearch article",
"sub_title" : "learning more courses"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.3487744,
"_source" : {
"articleID" : "KDKE-B-9947-#kL5",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-02",
"tag" : [
"java"
],
"tag_cnt" : 1,
"view_cnt" : 50,
"title" : "this is java blog",
"content" : "i think java is the best programming language",
"sub_title" : "learned a lot of course"
}
}
]
}
}
在 ES7.x 是{ "doc" : {"sub_title" : "learning more courses"} }
,就排在了{ "doc" : {"sub_title" : "learned a lot of course"} }
的前面,应该是 ES7.x 对分词做了优化,搜索的更加的进准
GET /forum/_search
{
"query": {
"multi_match": {
"query": "learning courses",
"type": "most_fields",
"fields": [ "sub_title", "sub_title.std" ]
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 5,
"relation" : "eq"
},
"max_score" : 6.4124336,
"hits" : [
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "1",
"_score" : 6.4124336,
"_source" : {
"articleID" : "XHDK-A-1293-#fJ3",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-01",
"tag" : [
"java",
"hadoop"
],
"tag_cnt" : 2,
"view_cnt" : 30,
"title" : "this is java and hadoop blog",
"content" : "i like to write best elasticsearch article",
"sub_title" : "learning more courses"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "8",
"_score" : 2.6506605,
"_source" : {
"articleID" : "QQPX-R-3956-#aD0",
"userID" : 5,
"hidden" : true,
"postDate" : "2019-06-02",
"tag" : [
"spark",
"elasticsearch",
"flink"
],
"tag_cnt" : 7,
"view_cnt" : 80,
"title" : "this is spark, elasticsearch, flink blog",
"content" : "i am only an elasticsearch beginner",
"sub_title" : "learning of flink"
}
},
{
"_index" : "forum",
"_type" : "_doc",
"_id" : "2",
"_score" : 2.3487744,
"_source" : {
"articleID" : "KDKE-B-9947-#kL5",
"userID" : 1,
"hidden" : false,
"postDate" : "2017-01-02",
"tag" : [
"java"
],
"tag_cnt" : 1,
"view_cnt" : 50,
"title" : "this is java blog",
"content" : "i think java is the best programming language",
"sub_title" : "learned a lot of course"
}
}
]
}
}
你问我,具体的分数怎么算出来的,很难说,因为这个东西很复杂, 还不只是TF/IDF
算法。因为不同的query
,不同的语法,都有不同的计算score
的细节。
与best_fields的
区别
(1)best_fields
,是对多个field
进行搜索,挑选某个field
匹配度最高的那个分数,同时在多个query
最高分相同的情况下,在一定程度上考虑其他query
的分数。简单来说,你对多个field
进行搜索,就想搜索到某一个field
尽可能包含更多关键字的数据
- 优点:通过
best_fields
策略,以及综合考虑其他field
,还有minimum_should_match
支持,可以尽可能精准地将匹配的结果推送到最前面
- 缺点:除了那些精准匹配的结果,其他差不多大的结果,排序结果不是太均匀,没有什么区分度了
实际的例子:百度之类的搜索引擎,最匹配的到最前面,但是其他的就没什么区分度了
(2)most_fields
,综合多个field
一起进行搜索,尽可能多地让所有field
的query
参与到总分数的计算中来,此时就会是个大杂烩,出现类似best_fields
案例最开始的那个结果,结果不一定精准,某一个document
的一个field
包含更多的关键字,但是因为其他document
有更多field
匹配到了,所以排在了前面;所以需要建立类似sub_title.std
这样的field
,尽可能让某一个field
精准匹配query string
,贡献更高的分数,将更精准匹配的数据排到前面
- 优点:将尽可能匹配更多
field
的结果推送到最前面,整个排序结果是比较均匀的
- 缺点:可能那些精准匹配的结果,无法推送到最前面
实际的例子:wiki,明显的most_fields
策略,搜索结果比较均匀,但是的确要翻好几页才能找到最匹配的结果