【问题标题】:ElasticSearch - Boosting based on depth in a recursive structureElasticSearch - 基于递归结构中的深度提升
【发布时间】:2017-08-22 00:51:35
【问题描述】:

我使用的是 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 自动对文档进行评分,而不需要查询本身使用特殊提升?
  • 为什么将它们作为嵌套文档?为什么不是带有字段的单个文档:document_id、标题、深度?

标签: elasticsearch spring-boot spring-data-elasticsearch


【解决方案1】:

是的,您可以通过使用 Nested datatype 映射和 Nested Query 内的 Function Score 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 将匹配的标题与其深度相关联,函数得分exp 优先考虑深度较低的标题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 2018-09-12
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 2021-07-11
    • 1970-01-01
    相关资源
    最近更新 更多