Elasticsearch聚合将结果转换为小写
我一直在玩ElasticSearch,但在进行聚合时发现了一个问题 我有两个端点,/A和/B。在第一个项目中,我有第二个项目的父母。因此,B中的一个或多个对象必须属于A中的一个对象。因此,B中的对象具有由ElasticSearch生成的父索引的属性“parentId” 我想按B的子属性筛选A中的父项。为此,我首先按属性筛选B中的子项,并获取其唯一的父项ID,稍后将使用该ID获取父项 我提出以下请求:Elasticsearch聚合将结果转换为小写,
Warning: implode(): Invalid arguments passed in /data/phpspider/zhask/webroot/tpl/detail.html on line 45
,,我一直在玩ElasticSearch,但在进行聚合时发现了一个问题 我有两个端点,/A和/B。在第一个项目中,我有第二个项目的父母。因此,B中的一个或多个对象必须属于A中的一个对象。因此,B中的对象具有由ElasticSearch生成的父索引的属性“parentId” 我想按B的子属性筛选A中的父项。为此,我首先按属性筛选B中的子项,并获取其唯一的父项ID,稍后将使用该ID获取父项 我提出以下请求: POST http://localhost:9200/test/B/_search {
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请求父级
GET http://localhost:9200/test/A/au_ffvwm40hx1kh6rfqa
Response:
{
"_index": "test",
"_type": "A",
"_id": "au_ffvwm40hx1kh6rfqa",
"found": false
}
关于为什么会发生这种情况有什么想法吗?点击和聚合结果之间的区别在于聚合是根据创建的术语工作的。他们还将返还条款。点击返回原始源 这些术语是如何产生的?基于所选分析仪(在您的情况下为默认分析仪),标准分析仪。该分析器所做的一件事是将术语的所有字符小写。正如Andrei提到的,您应该将字段parentId配置为不进行分析
PUT test
{
"mappings": {
"B": {
"properties": {
"parentId": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
我从党内迟到了,但我也有同样的问题,我明白这是正常化造成的 如果要防止任何规范化将聚合值更改为小写,则必须更改
索引的映射
您可以在DevTools控制台中键入检查当前映射
GET /A/_mapping
GET /B/_mapping
当您看到索引的结构时,您必须看到parentId
字段的设置
如果您不想更改字段的行为,但也希望避免聚合期间的规范化,则可以向parentId
字段添加子字段
要更改映射,必须删除索引并使用新映射重新创建索引:
在您的例子中,它看起来是这样的(它只包含parentId字段)
然后您必须在查询中使用子字段:
POST http://localhost:9200/test/B/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "derp2*"
}
},
"aggregations": {
"ids": {
"terms": {
"field": "parentId.keyword",
"order": {"_key": "desc"}
}
}
}
}
您的parentId
需要使用关键字分析工具进行分析,或者应该是“索引”:“未分析”
。好的,我知道了。将尝试更新字段映射,我想这就足够了!谢谢我对关键字映射也有同样的问题。。知道为什么会这样吗?子字段的想法是正确的。糟糕的是,由于ES的工作方式,我们无法以更直观的方式(比如一些字段来检索源代码的原始值)。
POST http://localhost:9200/test/B/_search
{
"query": {
"query_string": {
"default_field": "name",
"query": "derp2*"
}
},
"aggregations": {
"ids": {
"terms": {
"field": "parentId.keyword",
"order": {"_key": "desc"}
}
}
}
}