带有文本字段的 Elasticsearch 基数聚合

Elasticsearch cardinality aggregation with text fields

我正在尝试查询每个会话中生成的平均请求,我在将数据放入索引时插入 session_id,我想计算不同的会话并取出平均值,而检查数据映射我知道它在文本字段中:

"session_id": {
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword",
        "ignore_above": 256
      }
    }
  },

为了获取我调用的数据:

$this->elasticsearch->search([
    'index' => 'nits_media_bid_won',
    'body' => [
        'query' => $query,
        'aggs' => [
            'total_session' => [
                'cardinality' => [
                    'field' => 'session_id',
                    'precision_threshold' => 100,
                ]
            ]
        ]
    ]
]);

但我收到一条错误消息:

{
   "error":{
      "root_cause":[
         {
            "type":"illegal_argument_exception",
            "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [session_id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
         }
      ],
      "type":"search_phase_execution_exception",
      "reason":"all shards failed",
      "phase":"query",
      "grouped":true,
      "failed_shards":[
         {
            "shard":0,
            "index":"nits_media_bid_won",
            "node":"q438L5GRSqaHJz1_vRtZXg",
            "reason":{
               "type":"illegal_argument_exception",
               "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [session_id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
            }
         }
      ],
      "caused_by":{
         "type":"illegal_argument_exception",
         "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [session_id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.",
         "caused_by":{
            "type":"illegal_argument_exception",
            "reason":"Fielddata is disabled on text fields by default. Set fielddata=true on [session_id] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead."
         }
      }
   },
   "status":400
}

如果我按照关键字进行更改:

'total_session' => [
    'cardinality' => [
        'field' => 'session_id.fields.keyword',
        'precision_threshold' => 100,
    ]
]

它给了我 0 个值

"aggregations": {
  "total_session": {
    "value": 0
  }
}

虽然我没有完全检查您的查询,但关键字字段的名称似乎已关闭。根据提供的映射,session_id 的关键字字段将是 session_id.keyword