【问题标题】:How to use Term against a sub field which is an array in elasticsearch?如何对弹性搜索中的数组子字段使用术语?
【发布时间】:2019-01-12 22:55:51
【问题描述】:

我正在研究弹性搜索并在搜索查询中使用术语。请查看以下示例索引数据:

"_source": {
    "type": "ITEM",
    "primaryKey": "3923",
    "displayName": "Lumia 505",
    "attributes": {
        "n26273": "Lumia 505",
        "n26275": "Mobile"
    },
    "mappings": {
        "Primary Hierarchy": [
            "Nokia"
        ]
    }
}

为了根据我正在使用的任何搜索文本搜索数据,下面的查询运行良好:

{
   "query":{
      "bool":{
         "must":[
            {
               "query_string":{
                  "query":"----MySearchTextHere----",
                  "default_operator":"AND"
               }
            },
            {
               "term":{
                  "type":"item"
               }
            }
         ]
      }
   },
   "size":10,
   "from":0
}

查询:

1. 如何在查询中添加术语以搜索字段“mappings.Primary Hierarchy”,它是一个数组?

2.如何在不提及嵌套字段名称的情况下实现#1,因为嵌套字段名称是动态的?只有字段名称 mappings 是常量。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在术语查询中引用嵌套字段(在您的情况下为"mappings.Primary Hierarchy")没有问题

    {
       "query":{
          "bool":{
             "must":[
                {
                   "query_string":{
                      "query":"Nokia",
                      "default_operator":"AND"
                   }
                },
                {
                   "term":{
                      "mappings.Primary Hierarchy":"nokia"
                   }
                }
             ]
          }
       },
       "size":10,
       "from":0
    }
    

    这只会被 Nokia 成功过滤。如果您想准确使用诺基亚(未分析的术语),您应该使用"mappings.Primary Hierarchy.keyword" 字段。 Keyword 这里的意思是,这个字段的内容和你在索引时传递的完全一样。

    第二件事也是可能的,但需要适当的映射。您需要指定,您的映射是 nested type(这意味着其中可能有其他字段)。

    在您的情况下的简单示例可能是这样的:

    {
        "mappings": {
            "_doc" : {
                "properties" : {
                    "mappings" : {
                        "type" : "nested"
                    }
                }
            }
        }
    }
    

    您需要在应用这些设置的情况下创建索引。

    在这种情况下,您的查询可能如下所示:

    {
      "query": {
        "nested": {
          "path": "mappings",
          "query": {
            "multi_match": {
              "query": "Nokia"
            }
          }
        }
      }
    }
    

    Multi match query 未指定字段将默认为 ma​​ppings 对象下的所有字段

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多