![](/img/trans.png)
[英]How is a nested document relevance score (TF/IDF) calculated in Elasticsearch?
[英]Getting ElasticSearch to score number of total nested hits across results (idf?) higher than tf of single hit?
如果我正在修改術語,請原諒我,但是我在讓ES以對我的應用程序有意義的方式對結果進行評分時遇到了問題。
我正在使用幾個簡單字段索引成千上萬的用戶,以及可能有數百個嵌套在索引中的每個用戶的子對象(即Book - > Pages數據模型)。 發送到索引的JSON如下所示:
user_id: 1
full_name: First User
username: firstymcfirsterton
posts:
id: 2
title: Puppies are Awesome
tags:
- dog house
- dog supplies
- dogs
- doggies
- hot dogs
- dog lovers
user_id: 2
full_name: Second User
username: seconddude
posts:
id: 3
title: Dogs are the best
tags:
- dog supperiority
- dog
id: 4
title: Why dogs eat?
tags:
- dog diet
- canines
id: 5
title: Who let the dogs out?
tags:
- dogs
- terrible music
標簽是“標簽”類型,使用“關鍵字”分析器,並提升10.標題不會提升。
當我搜索“dog”時,第一個用戶的得分高於第二個用戶。 我假設這必須使用第一個用戶的tf-idf更高。 但是在我的應用程序中,理想情況下獲得該術語命中的用戶的帖子數量會更多。
我嘗試按帖子的數量進行排序,但如果用戶有很多帖子,這會產生垃圾結果。 基本上我想按照獨特的帖子點擊次數進行排序,這樣一個擁有更多帖子的用戶就會登上榜首。
我該怎么做呢 有任何想法嗎?
首先,我同意@karmi和@Zach的意見,通過匹配帖子弄清楚你的意思是很重要的。 為簡單起見,我假設一個匹配的帖子在其中的某個地方有一個單詞“dog”,我們沒有使用關鍵字分析器來對標簽進行匹配並提升更多的趣味性。
如果我正確理解您的問題,您希望根據相關帖子的數量訂購用戶。 這意味着您需要搜索帖子以查找相關帖子,然后將此信息用於您的用戶查詢。 只有當帖子被單獨索引時才有可能,這意味着帖子必須是子文檔或嵌套字段。
假設帖子是子文檔,我們可以像這樣對數據進行原型設計:
curl -XPOST 'http://localhost:9200/test-idx' -d '{
"settings" : {
"number_of_shards" : 1,
"number_of_replicas" : 0
},
"mappings" : {
"user" : {
"_source" : { "enabled" : true },
"properties" : {
"full_name": { "type": "string" },
"username": { "type": "string" }
}
},
"post" : {
"_parent" : {
"type" : "user"
},
"properties" : {
"title": { "type": "string"},
"tags": { "type": "string", "boost": 10}
}
}
}
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/user/1' -d '{
"full_name": "First User",
"username": "firstymcfirsterton"
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/user/2' -d '{
"full_name": "Second User",
"username": "seconddude"
}' && echo
#Posts of the first user
curl -XPUT 'http://localhost:9200/test-idx/post/1?parent=1' -d '{
"title": "Puppies are Awesome",
"tags": ["dog house", "dog supplies", "dogs", "doggies", "hot dogs", "dog lovers", "dog"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/2?parent=1' -d '{
"title": "Cats are Awesome too",
"tags": ["cat", "cat supplies", "cats"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/3?parent=1' -d '{
"title": "One fine day with a woof and a purr",
"tags": ["catdog", "cartoons"]
}' && echo
#Posts of the second user
curl -XPUT 'http://localhost:9200/test-idx/post/4?parent=2' -d '{
"title": "Dogs are the best",
"tags": ["dog supperiority", "dog"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/5?parent=2' -d '{
"title": "Why dogs eat?",
"tags": ["dog diet", "canines"]
}' && echo
curl -XPUT 'http://localhost:9200/test-idx/post/6?parent=2' -d '{
"title": "Who let the dogs out?",
"tags": ["dogs", "terrible music"]
}' && echo
curl -XPOST 'http://localhost:9200/test-idx/_refresh' && echo
我們可以使用Top Children Query查詢這些數據。 (或者在嵌套字段的情況下,我們可以使用嵌套查詢實現類似的結果)
curl 'http://localhost:9200/test-idx/user/_search?pretty=true' -d '{
"query": {
"top_children" : {
"type": "post",
"query" : {
"bool" : {
"should": [
{ "text" : { "title" : "dog" } },
{ "text" : { "tags" : "dog" } }
]
}
},
"score" : "sum"
}
}
}' && echo
此查詢將首先返回第一個用戶,因為來自匹配標記的巨大提升因子。 所以,它可能看起來不像你想要的,但有一些簡單的方法來修復它。 首先,我們可以減少標簽字段的提升因子。 10對於可以重復多次的場來說是非常大的因素。 或者,我們可以修改查詢以完全忽略子命中的分數,並使用最匹配的子文檔的數量作為分數:
curl 'http://localhost:9200/test-idx/user/_search?pretty=true' -d '{
"query": {
"top_children" : {
"type": "post",
"query" : {
"constant_score" : {
"query" : {
"bool" : {
"should": [
{ "text" : { "title" : "dog" } },
{ "text" : { "tags" : "dog" } }
]
}
}
}
},
"score" : "sum"
}
}
}' && echo
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.