【问题标题】:Significant terms aggregation for arrays in elasticsearchelasticsearch中数组的重要术语聚合
【发布时间】:2017-10-27 22:18:06
【问题描述】:

我无法使用数组字段执行重要的术语聚合。我的 Javascript 查询如下所示:

client.search({
  index: myIndex,
  body: {
    query: {
      terms: {
        myField: ['someuserid']
        // also tried with same result... myField: 'someuserid'
      }
    },
    aggregations: {
      recommendations: {
        significant_terms: {
          field: "myField",
          min_doc_count: 1
        }
      }
    }
  }
})

我收到此错误:

(node:13105) UnhandledPromiseRejectionWarning: Unhandled promise rejection 
(rejection id: 1): Error: [illegal_argument_exception] Fielddata is disabled 
on text fields by default. Set fielddata=true on [myField] in order to 
load fielddata in memory by uninverting the inverted index. Note that this can 
however use significant memory.

我的映射如下所示:

{
  index: 'myIndex',
  type: 'users',
  body: {
    properties: {
        'myField': []
    }
  }
}

我知道我不需要显式映射数组数据类型,但我这样做是为了可以轻松查看某个 type 的字段。根据错误消息,我会将映射更改为如下所示:

...
properties: {
  myField: {
    fielddata: "true"
  }
}
...

但是,这会导致以下错误:

Error: [mapper_parsing_exception] No type specified for field [myField]

如果我要添加一个类型: ... 特性: { 我的领域:{ 类型: [], 字段数据:“真” } } ... 我会收到这个错误:

[mapper_parsing_exception] No handler for type [[]] declared on field [myField]

目前,我正在聚合的数据来自通过 Javascript 客户端库完全使用由以下内容构建的更新 API 播种的数据:

const update = {
    "upsert": {
      "myField": ['myValue']
    },
    "script": {
    "inline": "ctx._source.myField.add(params.itemField)",
    "params": {
      "itemField": 'itemValue'
    }
  }
};

const req = {
    index: 'myIndex',
    type: 'users',
    id: 'someuserid',
    body: update
}

来自此查询 curl -XGET 'localhost:9200/myIndex/users/_search?pretty' 的命中将如下所示:

...
{
    "_index" : "myIndex",
    "_type" : "users",
    "_id" : "someuserid",
    "_score" : 1.0,
    "_source" : {
      "myField" : [
        "someFieldId1",
        "someFieldId1",
        "someFieldId2"
      ]
    }
  },
...

如何使用数组字段正确执行重要术语聚合?

【问题讨论】:

    标签: javascript arrays elasticsearch


    【解决方案1】:

    https://www.elastic.co/guide/en/elasticsearch/reference/current/array.html

    在 Elasticsearch 中,没有专用的数组类型。任何领域都可以 默认情况下包含零个或多个值,但是, 数组必须是相同的数据类型。

    假设您使用的是 ElasticSearch 5.x,请尝试将 type: [] 更改为 type: "text"type: "keyword"强>

    对于两者之间的区别,我建议阅读以下内容:https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html

    但是在你的情况下,由于它看起来像某种 id,它可能不需要分析,所以我建议使用“keyword”而不是“text”。

    对于早期版本的 ES,请改用“字符串”。 https://www.elastic.co/guide/en/elasticsearch/reference/2.4/string.html

    【讨论】:

    • 谢谢,这正是我最终所做的。是的,我一遍又一遍地阅读那篇文档,但我直觉地认为将类型添加为文本或关键字会使得如果我要更新文档,该字段将被替换(不是我想要的)而不是形成一个数组并添加到它。我需要再查一下,但我想如果我愿意的话,我会使用索引 API 而不是更新 API。
    • 您可以尝试将更新 API 与执行 ctx._source.myField += newValue elastic.co/guide/en/elasticsearch/reference/current/… 的脚本一起使用
    猜你喜欢
    • 2015-01-21
    • 2014-07-09
    • 2021-06-06
    • 1970-01-01
    • 2021-06-06
    • 2015-11-06
    • 2016-02-17
    • 2015-06-26
    • 2019-12-06
    相关资源
    最近更新 更多