我对ElasticSearch不太了解,并且想知道如何基于某个整数值增强搜索。
这是一个文档示例:
{
"_index": "links",
"_type": "db1",
"_id": "mV32vWcBZsblNn1WqTcN",
"_score": 8.115617,
"_source": {
"url": "example.com",
"title": "Example website",
"description": "This is an example website, used for various of examples around the world",
"likes": 9,
"popularity": 543,
"tags": [
{
"name": "example",
"votes": 5
},
{
"name": "test",
"votes": 2
},
{
"name": "testing",
"votes": 1
}
]
}
}
现在在此特定搜索中,重点放在tags
上,我想知道如何增强_score
并将其乘以{{下的votes
中的整数1}}。
如果这不可能(或很难实现),我只想知道如何将tags
提升为_score
(不在votes
下)
例如,为tags
中的每个整数向_score
添加0.1
这是我正在使用的当前搜索查询(仅用于搜索标签 ):
votes
我在网上找不到太多东西,希望有人可以帮我。
如何使用整数值提升{
"query": {
"nested": {
"path": "tags",
"query": {
"bool":{
"should":{
"match":{
"tags.name":"example,testing,something else"
}
}
}
}
}
}
}
?
有关更多信息,请参见以下映射:
_score
将{
"links": {
"mappings": {
"db1": {
"properties": {
"url": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"description": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"likes": {
"type": "long"
},
"popularity": {
"type": "long"
},
"tags": {
"type": "nested",
"properties": {
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
},
"votes": {
"type": "long"
}
}
}
}
}
}
}
}
/ tags.likes
更改为tags.dislikes
,并向tags.votes
添加了nested
属性
答案 0 :(得分:0)
您正在查看function score query
:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html
还有field value factor
https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#function-field-value-factor。
文档摘录:
GET /_search
{
"query": {
"function_score": {
"field_value_factor": {
"field": "tags.dislikes",
"factor": 1.2,
"modifier": "sqrt",
"missing": 1
}
}
}
}
或者使用script score
,因为您嵌套的tags
字段(不确定field value score
是否适用于嵌套结构)。
答案 1 :(得分:0)
这花了很长时间才弄清楚。在去那里的路上,我学到了很多东西。
这是最终结果:
{
"query": {
"nested": {
"path": "tags",
"query": {
"function_score": {
"query": {
"bool": {
"should": [
{
"match": {
"tags.name": "example"
}
},
{
"match": {
"tags.name": "testing"
}
},
{
"match": {
"tags.name": "test"
}
}
]
}
},
"functions": [
{
"field_value_factor": {
"field": "tags.votes"
}
}
],
"boost_mode": "multiply"
}
}
}
}
}
should
中的数组有很大帮助,很高兴我可以将其与function_score
组合