如何直接从Elasticsearch获取用于按字段过滤的选项?

时间:2018-10-02 11:05:31

标签: elasticsearch

我想根据我在Elasticsearch中建立索引的数据填充一个过滤字段。如何获取这些数据?例如,我在索引“ test”和类型“ doc”中的文档可能是

{"id":1, "tag":"foo",  "name":"foothing"}
{"id":2, "tag":"bar",  "name":"barthing"}
{"id":3, "tag":"foo",  "name":"something"}
{"id":4, "tag":"quux", "name":"quuxthing"}

我正在寻找类似GET /test/doc/_magic?q=tag这样的东西,该东西将从我的数据中返回[foo,bar,quux]。我不知道这叫什么甚至不可能。我不想将所有索引条目都存储到内存中并以编程方式执行此操作,索引中有数百万个文档,其中包含大约一百个不同的标签。

ES可以做到吗?

1 个答案:

答案 0 :(得分:1)

是的,这是可能的,这称为terms aggregation

您可以这样做:

GET /test/doc/_search
{   
    "size": 0,
    "aggs" : {
        "tags" : {
            "terms" : { 
               "field" : "tag.keyword",
               "size": 100
            }
        }
    }
}

请注意,根据tag字段的基数,可以增加/减少size设置(默认为10)。