【发布时间】:2012-04-12 12:41:43
【问题描述】:
我正在向 elasticsearch 发出查询,并且我得到了多种记录类型。如何将结果限制为一种类型?
【问题讨论】:
-
一般来说,最好包含一个您向 ES 发出的查询示例...
标签: api search schema dsl elasticsearch
我正在向 elasticsearch 发出查询,并且我得到了多种记录类型。如何将结果限制为一种类型?
【问题讨论】:
标签: api search schema dsl elasticsearch
您还可以使用查询 dsl 过滤掉特定类型的结果,如下所示:
$ curl -XGET 'http://localhost:9200/_search' -d '{
"query": {
"filtered" : {
"filter" : {
"type" : { "value" : "my_type" }
}
}
}
}
'
6.1 版更新: 类型过滤器现在替换为类型查询:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-type-query.html 您可以在 Query 和 Filter 上下文中使用它。
【讨论】:
在2.3版本上可以查询_type fieldlike:
{
"query": {
"terms": {
"_type": [ "type_1", "type_2" ]
}
}
}
或者如果你想排除一个类型:
{
"query": {
"bool" : {
"must_not" : {
"term" : {
"_type" : "Hassan"
}
}
}
}
}
【讨论】:
{
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"must" :[{"term":{"_type":"UserAudit"}}, {"term" : {"eventType": "REGISTRATION"}}]
}
}
}
},
"aggs":{
"monthly":{
"date_histogram":{
"field":"timestamp",
"interval":"1y"
},
"aggs":{
"existing_visitor":{
"terms":{
"field":"existingGuest"
}
}
}
}
}
}
"_type":"UserAudit" 条件将只查看特定于类型的记录
【讨论】:
以下查询会将结果限制为“your_type”类型的记录:
curl - XGET 'http://localhost:9200/_all/your_type/_search?q=your_query'
更多详情请见http://www.elasticsearch.org/guide/reference/api/search/indices-types.html。
【讨论】: