首页 > 解决方案 > 如果字段中的数组包含与字符串匹配的某个对象,则提升

问题描述

我有看起来像这样的文件:

{
   "names": [
      {
         "type": "Main Name",
         "value": "My important title"
      },
      {
         "type": "Subtitle",
         "value": "Just my subtitle"
      }
   ],
}

我想要做的是传递一个字符串,它能够更多地提升文档,其中传递的字符串包含在带有type Main Name. 例如,搜索“重要”将意味着示例文档的得分高于搜索“只是”或“副标题”。

到目前为止,我有以下子查询(较大查询的一部分):

{
   "match_phrase": {
      "names.value": {
         "query": "important",
         "slop":  100
   }
}

但我真的不知道如何区分数组中的字段。我想做一些类似的事情,如果type是“主名称”并且value包含“重要”,那么提升 5。我玩过boost关键字。

这可能吗?

- 编辑

发表评论后,我能够构建以下似乎有效的查询。这是我完整使用的查询:

{  
   "nested":{  
      "path":"propertyNames",
      "boost":1000,
      "query":{  
         "bool":{  
            "must":[  
               {  
                  "match":{  
                     "propertyNames.type.raw":{  
                        "query":"Primary Name"
                     }
                  }
               },
               {  
                  "match_phrase":{  
                     "propertyNames.value.raw":{  
                        "query":"downing"
                     }
                  }
               }
            ]
         }
      }
   }
}

我要添加的部分是query.function_score.query.bool.should. propertyNames 中的数据如下所示:

{
   "names": [
      {
         "type": "Primary Name",
         "value": "10 Downing Street"
      },
      {
         "type": "Other Name",
         "value": "The prime minister's house"
      }
   ],
}

如果搜索词是“downing”,那么上面的文档应该得到更高的分数。如果它是“素数”,那么它不应该被提升。

但是,我添加的部分没有任何区别。

我认为这是大致正确的。我一直在玩很多shoulds 和musts 的组合,但没有一个组合能给出正确的结果。

我在这里是正确的吗?

我添加的嵌套查询是否存在并不重要。返回相同的结果。

标签: elasticsearch

解决方案


这是一个嵌套类型的工作示例。

这是嵌套类型的映射,用于跟踪名称值与其类型之间的关系。

PUT test_stack_overflow
{
  "mappings": {
    "document": {
      "properties": {
        "names": {
          "type": "nested",
          "properties": {
            "type": {
              "type": "keyword"
            },
            "value": {
              "type": "string"
            }
          }
        }
      }
    }
  }
}

一些测试文件

PUT test_stack_overflow/document/1
{
  "names": [
      {
         "type": "Primary Name",
         "value": "10 downing Street"
      },
      {
         "type": "Other Name",
         "value": "The something minister's house"
      }
   ]
}

PUT test_stack_overflow/document/2
{
  "names": [
      {
         "type": "Primary Name",
         "value": "10 something Street"
      },
      {
         "type": "Other Name",
         "value": "The downing house"
      }
   ]
}

还有一个例子(有很多方法可以实现你的目标)来促进Primary Name类型匹配。

我们使用布尔查询,指定我们需要在 any 中查找查询,并且匹配的文档应该在类型names.value为 a 中找到该值(如果满足条件,should 子句将增加分数)namesPrimary Name

GET test_stack_overflow/document/_search
{
  "query": {
    "nested": {
      "path": "names",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "names.value": "downing"
              }
            }
          ]
        }
      }
    }
  }
}

=> 返回具有完全相同分数的两个文档

GET test_stack_overflow/document/_search
{
  "query": {
    "nested": {
      "path": "names",
      "query": {
        "bool": {
          "must": [
            {
              "match": {
                "names.value": "downing"
              }
            }
          ],
          "should": [
            {
              "term": {
                "names.type": "Primary Name"
              }
            }
          ]
        }
      }
    }
  }
}

=> 首先返回文档 1

我希望它会帮助你。


推荐阅读