将fielddata设置为true后,Elasticsearch不同的查询

时间:2018-05-29 22:21:43

标签: elasticsearch

我试图在索引上获取字段“vip_name”的不同值。 这是我试图开始的:

curl -XGET http://172.31.38.157:9200/cb_inventory/_search -d 
'{"size":0,"aggs":{"vips":{"terms":{"field":"vip_name"}}}}'

{"error":{"root_cause":
[{"type":"illegal_argument_exception","reason":"Fielddata is disabled 
on text fields by default. Set fielddata=true on [vip_name] 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":"cb_inventory","node":"7_t7zG82QsS__Q_vRHWy9A","reason":
{"type":"illegal_argument_exception","reason":"Fielddata is disabled on text fields
 by default. 

行。所以我将fielddata设置为true,如下所示:

curl -XPUT http://172.31.38.157:9200/cb_inventory/_mapping/cb_inventory -d '{"properties":{"vip_name":{"type":"text","fielddata":true}}}'
{"acknowledged":true}

现在我进行搜索并返回以下内容:

curl -XGET http://172.31.38.157:9200/cb_inventory/_search?pretty=true -d '{"size":0,"aggs":{"vips":{"terms":{"field":"vip_name","size":1000}}}}'
{"took" : 5,
 "timed_out" : false,
 "_shards" : {
   "total" : 5,
   "successful" : 5,
   "skipped" : 0,
   "failed" : 0
 },
"hits" : {
  "total" : 3,
   "max_score" : 0.0,
   "hits" : [ ]
 },
"aggregations" : {
  "vips" : {
   "doc_count_error_upper_bound" : 0,
  "sum_other_doc_count" : 0,
  "buckets" : [
    {
      "key" : "domain.com",
      "doc_count" : 3
    },
    {
      "key" : "ppcbcl00021",
      "doc_count" : 3
    }
  ]
}
}
}

这有点好笑,因为我只有一个不同的值ppcbcl00021.domain.com。现在它显示为2个不同的值。

我如何获得“ppcbcl00021.domain.com”

的独特价值

1 个答案:

答案 0 :(得分:0)

这是因为vip_name设置为text而不是keyword。因此,即使您有ppcbcl00021.domain.com,在ES中,它也会被存储为文本块,即ppcbcl00021domain.com

vip_name设置为keyword

再试一次
curl -XPUT http://172.31.38.157:9200/cb_inventory/_mapping/cb_inventory -d '{"properties":{"vip_name":{"type":"keyword"}}}'