gpt4 book ai didi

在 Elasticsearch 中搜索带空格的名称(文本)

转载 作者:行者123 更新时间:2023-11-29 02:44:42 25 4
gpt4 key购买 nike

正在搜索其中包含空格的名称(文本),给我带来了问题,我有类似于

的映射
"{"user":{"properties":{"name":{"type":"string"}}}}"

理想情况下,它应该返回什么并对结果进行如下排序

1) Bring on top names that exact match the search term (highest score)
2) Names that starts with the search term (high score)
3) Names that contains the exact search term as substring (medium score)
4) Names that contains any of the search term token (lowest score)

例子对于 Elasticsearch 中的以下名称

Maaz Tariq
Ahmed Maaz Tariq
Maaz Sheeba
Maaz Bin Tariq
Sana Tariq
Maaz Tariq Ahmed

搜索“Maaz Tariq”,结果应按以下顺序

Maaz Tariq (highest score)
Maaz Tariq Ahmed (high score)
Ahmed Maaz Tariq (medium score)
Maaz Bin Tariq (lowest score)
Maaz Sheeba (lowest score)
Sana Tariq (lowest score)

任何人都可以告诉我如何使用以及使用哪些分析器吗?以及如何对名称的搜索结果进行排名?

最佳答案

您可以使用 multi field type , 一个 bool querycustom boost factor query来解决这个问题。

映射:

{
"mappings" : {
"user" : {
"properties" : {
"name": {
"type": "multi_field",
"fields": {
"name": { "type" : "string", "index": "analyzed" },
"exact": { "type" : "string", "index": "not_analyzed" }
}
}
}
}
}
}

查询:

{
"query": {
"bool": {
"must": [
{
"match": {
"name": "Maaz Tariq"
}
}
],
"should": [
{
"custom_boost_factor": {
"query": {
"term": {
"name.exact": "Maaz Tariq"
}
},
"boost_factor": 15
}
},
{
"custom_boost_factor": {
"query": {
"prefix": {
"name.exact": "Maaz Tariq"
}
},
"boost_factor": 10
}
},
{
"custom_boost_factor": {
"query": {
"match_phrase": {
"name": {
"query": "Maaz Tariq",
"slop": 0
}
}
},
"boost_factor": 5
}
}
]
}
}
}

编辑:

正如 javanna 所指出的,custom_boost_factor 不是必需的。

不使用 custom_boost_factor 的查询:

{
"query": {
"bool": {
"must": [
{
"match": {
"name": "Maaz Tariq"
}
}
],
"should": [
{
"term": {
"name.exact": {
"value": "Maaz Tariq",
"boost": 15
}
}
},
{
"prefix": {
"name.exact": {
"value": "Maaz Tariq",
"boost": 10
}
}
},
{
"match_phrase": {
"name": {
"query": "Maaz Tariq",
"slop": 0,
"boost": 5
}
}
}
]
}
}
}

关于在 Elasticsearch 中搜索带空格的名称(文本),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16708878/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com