Elasticsearch聚合将结果转换为小写 [英] Elasticsearch aggregation turns results to lowercase

查看:516
本文介绍了Elasticsearch聚合将结果转换为小写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been playing with ElasticSearch a little and found an issue when doing aggregations.

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

I have two endpoints, /A and /B. In the first one I have parents for the second one. So, one or many objects in B must belong to one object in A. Therefore, objects in B have an attribute "parentId" with parent index generated by ElasticSearch.

I想要按B的子级属性过滤A中的父母。为此,我首先按B的属性过滤B中的子级,并获取其唯一的父级ID,以后将用于获取父级。

I want to filter parents in A by children attributes of B. In order to do it, I first filter children in B by attributes and get its unique parent ids that I'll later use to get parents.

我发送此请求:

POST http://localhost:9200/test/B/_search
{
    "query": {
        "query_string": {
            "default_field": "name",
            "query": "derp2*"
        }
    },
    "aggregations": {
        "ids": {
            "terms": {
                "field": "parentId"
            }
        }
    }
}

并得到以下响应:

{
  "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
        }
      ]
    }
  }
}

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

For some reason, the filtered key is returned in lowercase, hence not being able to request parent to ElasticSearch

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

Response:
{
  "_index": "test",
  "_type": "A",
  "_id": "au_ffvwm40hx1kh6rfqa",
  "found": false
}

关于为什么的任何想法

推荐答案

命中与聚合结果之间的区别在于,聚合按创建的条件工作。他们还将返回条款。这些匹配返回原始来源。

The difference between the hits and the results of the aggregations is that the aggregations work on the created terms. They will also return the terms. The hits return the original source.

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

How are these terms created? Based on the chosen analyser, which in your case is the default one, the standard analyser. One of the things this analyser does is lowercasing all the characters of the terms. Like mentioned by Andrei, you should configure the field parentId to be not_analyzed.

PUT test
{
  "mappings": {
    "B": {
      "properties": {
        "parentId": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  }   
}

这篇关于Elasticsearch聚合将结果转换为小写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆