gpt4 book ai didi

ElasticSearch - 基于递归结构中的深度提升

转载 作者:行者123 更新时间:2023-11-29 02:53:27 24 4
gpt4 key购买 nike

我正在使用 Elastic search 2.4.4(兼容 spring boot 1.5.2)。

我有一个具有以下结构的文档对象:

{
id : 1,
title : Doc title
//some more metadata
sections :[
{
"id" : 2,
"title: Sec title 1,
sections:[...]
},{
id : 3,
title: Sec title 2,
sections:[...]
}

]
}

基本上我想让文档中的标题可搜索(所有文档标题、章节标题和任何级别的小节标题)并且我希望能够根据它们在树层次结构中匹配的级别对文档进行评分.

我最初的想法是使用这样的结构:

 {
titles:[
{
title : doc title,
depth : 0
},
{
title : sec title 1,
depth : 1
},
{
title : sec title 2,
depth : 1
},
......
]
}

我想根据匹配的深度对文档进行排名(深度越高,分数越低)。

我知道基于该领域的基本提升,但是,

有没有办法在 Elasticsearch 中做到这一点?

是否可以通过改变结构来实现?

最佳答案

是的,您可以通过使用 Nested datatype 以修改后的格式(对象的平面数组)索引文档来实现此目的映射和 Function Score QueryNested Query 里面:

PUT someindex
{
"mappings": {"sometype":{"properties": {"titles":{"type": "nested"}}}}
}

POST someindex/sometype/0
{
"titles": [
{ "title": "doc title", "depth": 0 },
{ "title": "sec title 1", "depth": 1 },
{ "title": "sec title 2", "depth": 1 }
]
}

POST someindex/sometype/1
{
"titles": [
{ "title": "sec doc title", "depth": 0 }
]
}

GET someindex/sometype/_search
{
"query": {
"nested": {
"path": "titles",
"score_mode": "max",
"query": {
"function_score": {
"query": {
"match": {
"titles.title": "sec"
}
},
"functions": [
{
"exp": {
"titles.depth": {
"origin": 0,
"scale": 1
}
}
}
]
}
}
}
}
}

在此示例中,文档 1 的得分较高,因为它的标题与深度 0 的 sec 匹配,而文档 2 的标题仅与深度 1 的 sec 匹配。

嵌套的数据类型和查询确保 function_score 将匹配的标题与其深度相关联,函数 score exp 优先考虑深度较低的标题。

关于ElasticSearch - 基于递归结构中的深度提升,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43078174/

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