【问题标题】:nested terms aggregation on object containing a string field包含字符串字段的对象上的嵌套术语聚合
【发布时间】:2020-04-29 15:09:17
【问题描述】:

我喜欢在对象内部的字符串字段上运行嵌套术语聚合。

通常,我使用这个查询

      "terms": {
        "field": "fieldname.keyword"
      }

启用字段数据

但对于这样的嵌套文档,我无法做到这一点

{
      "nested": {
        "path": "objectField"
      },
      "aggs": {
        "allmyaggs": {
          "terms": {
            "field": "objectField.fieldName.keyword"
          }
        }
      }
    }

上面的查询只是返回一个空的桶数组

有没有办法在索引映射期间默认不启用字段数据。 因为这将占用大量堆内存,而我已经加载了大量数据而没有它

文档映射

{
  "mappings": {
    "properties": {
      "productname": {
        "type": "nested",
        "properties": {
          "productlineseqno": {
            "type": "text"
          },
          "invoiceitemname": {
            "type": "text"
          },
          "productlinename": {
            "type": "text"
          },
          "productlinedescription": {
            "type": "text"
          },
          "isprescribable": {
            "type": "boolean"
          },
          "iscontrolleddrug": {
            "type": "boolean"
          }
        }
      }

示例文档

{
  "productname": [
    {
      "productlineseqno": "1.58",
      "iscontrolleddrug": "false",
      "productlinename": "Consultations",
      "productlinedescription": "Consultations",
      "isprescribable": "false",
      "invoiceitemname": "invoice name"
    }
  ]
}

固定

通过更改映射来启用字段数据

【问题讨论】:

    标签: elasticsearch elastic-stack


    【解决方案1】:

    嵌套查询用于访问嵌套字段,类似nested aggregation 需要聚合嵌套字段

    {
      "aggs": {
        "fieldname": {
          "nested": {
            "path": "objectField"
          },
          "aggs": {
            "fields": {
              "terms": {
                "field": "objectField.fieldname.keyword",
                "size": 10
              }
            }
          }
        }
      }
    }
    

    编辑1:

    如果您正在搜索 productname.invoiceitemname.keyword,那么它将给出空存储桶,因为不存在具有该名称的字段。

    你需要像下面这样定义你的映射

    {
      "mappings": {
        "properties": {
          "productname": {
            "type": "nested",
            "properties": {
              "productlineseqno": {
                "type": "text"
              },
              "invoiceitemname": {
                "type": "text",
                "fields":{       --> note
                  "keyword":{
                    "type":"keyword"
                  }
                }
              },
              "productlinename": {
                "type": "text"
              },
              "productlinedescription": {
                "type": "text"
              },
              "isprescribable": {
                "type": "boolean"
              },
              "iscontrolleddrug": {
                "type": "boolean"
              }
            }
          }
        }
      }
    }
    

    Fields

    以不同的方式索引相同的字段通常很有用 不同的目的。这就是多领域的目的。例如, 字符串字段可以映射为全文搜索的文本字段, 并作为排序或聚合的关键字字段:

    如果未明确提供映射,则默认创建关键字字段。如果您正在创建自己的映射(您需要为嵌套类型执行此操作),则需要在映射中提供关键字字段,无论您打算在哪里使用它们

    【讨论】:

    • 这不起作用。让我编辑我的问题以显示完整的查询
    • @gdvigneshwar,它对我有用。您能否添加您的映射和有问题的示例文档
    • 感谢您的回复。已添加映射和示例文档
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2017-09-17
    • 2017-09-13
    • 1970-01-01
    相关资源
    最近更新 更多