Elasticsearch 聚合将结果转为小写

Rec*_*nes 5 lowercase analyzer elasticsearch elasticsearch-aggregation

我一直在玩 ElasticSearch,在进行聚合时发现了一个问题。

我有两个端点,/A/B。在第一个我有第二个的父母。因此,B 中的一个或多个对象必须属于 A 中的一个对象。因此,B 中的对象具有属性“parentId”,其父索引由 ElasticSearch 生成。

我想通过 B 的子属性过滤 A 中的父项。为了做到这一点,我首先按属性过滤 B 中的子项并获取其唯一的父 ID,稍后我将使用它来获取父 ID。

我发送这个请求:

POST http://localhost:9200/test/B/_search
{
    "query": {
        "query_string": {
            "default_field": "name",
            "query": "derp2*"
        }
    },
    "aggregations": {
        "ids": {
            "terms": {
                "field": "parentId"
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

并得到这个回应:

{
  "took": 91,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 1,
    "hits": [
      {
        "_index": "test",
        "_type": "child",
        "_id": "AU_fjH5u40Hx1Kh6rfQG",
        "_score": 1,
        "_source": {
          "parentId": "AU_ffvwM40Hx1Kh6rfQA",
          "name": "derp2child2"
        }
      },
      {
        "_index": "test",
        "_type": "child",
        "_id": "AU_fjD_U40Hx1Kh6rfQF",
        "_score": 1,
        "_source": {
          "parentId": "AU_ffvwM40Hx1Kh6rfQA",
          "name": "derp2child1"
        }
      },
      {
        "_index": "test",
        "_type": "child",
        "_id": "AU_fjKqf40Hx1Kh6rfQH",
        "_score": 1,
        "_source": {
          "parentId": "AU_ffvwM40Hx1Kh6rfQA",
          "name": "derp2child3"
        }
      }
    ]
  },
  "aggregations": {
    "ids": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "au_ffvwm40hx1kh6rfqa",
          "doc_count": 3
        }
      ]
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

由于某种原因,过滤后的键以小写形式返回,因此无法向 ElasticSearch 请求父项

GET http://localhost:9200/test/A/au_ffvwm40hx1kh6rfqa

Response:
{
  "_index": "test",
  "_type": "A",
  "_id": "au_ffvwm40hx1kh6rfqa",
  "found": false
}
Run Code Online (Sandbox Code Playgroud)

关于为什么会发生这种情况的任何想法?

Jet*_*die 5

命中和聚合结果之间的区别在于聚合适用于创建的术语。他们还将返回条款。命中返回原始来源。

这些术语是如何创建的?基于所选的分析仪,在您的情况下是默认分析仪,即标准分析仪。该分析器所做的一件事是将术语的所有字符小写。就像安德烈提到的那样,您应该将字段 parentId 配置为 not_analyzed。

PUT test
{
  "mappings": {
    "B": {
      "properties": {
        "parentId": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }   
}
Run Code Online (Sandbox Code Playgroud)