【发布时间】:2016-06-09 17:22:59
【问题描述】:
我正在使用 ES 2.1 并具有以下映射:
"startDate": {
"type": "date",
"format": "yyyy-MM-dd HH:mm:ss:SSS||yyyy-MM-dd HH:mm:ss",
"index": "not_analyzed",
"store": true
},"identities": {
"type": "nested",
"properties": {
"identityatt": { "type": "integer", "index": "not_analyzed", "store": true },
"identitykey": { "type": "string", "index": "not_analyzed", "store": true },
"identityval": { "type": "string", "index": "not_analyzed", "store": true },
"identitytype": { "type": "integer", "index": "not_analyzed", "store": true }
}
}
以下查询很好,它们返回我所期望的:
{ "size": 50,
"query": {
"filtered": {
"filter": {
"range": {
"startDate": {
"from": "2016-02-19 11:11:25",
"to": "2016-02-27 11:11:25",
"include_lower": true,
"include_upper": true
}
}}}}}
这一个按时间范围过滤,下一个我想检索所有具有特殊身份类型的内容
{
"size": 50,
"query": {
"nested": {
"path": "identities",
"filter": {
"term": {
"identities.identitytype": "2"
}
}}}}
但我似乎没有得到将这两者结合起来的查询。
我尝试将时间范围查询添加到嵌套过滤器中的过滤器,将嵌套过滤器中的两个过滤器包装成 bool 过滤器,我还尝试使用 filtered 查询,但也没有运气将这两者结合起来。
查看https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html 的示例,它还包含一个范围查询,但不同之处在于它在嵌套对象中,而我的startDate 不包含在我的嵌套对象中。
关于如何组合这些查询有什么想法吗?
编辑
我也尝试了这里的建议:Combined non-Nested and Nested Query in Elasticsearch 并得到错误"No query registered for [filter]"
{
"size": 50,
"query": {
"bool": {
"must": [
{"filter": {
"range": {
"startDate": {
"from": "2016-02-19 11:11:25",
"to": "2016-02-27 11:11:25",
"include_lower": true,
"include_upper": true
}
}
}},
{"nested": {
"path": "identities",
"filter": { "bool": { "must": [{
"term": {
"identities.identitytype": "2"
},
"range": {
"startDate": {
"from": "2016-02-19 11:11:25",
"to": "2016-02-27 11:11:25",
"include_lower": true,
"include_upper": true
}
}}]}
}
}
}
]
}}}
【问题讨论】:
标签: elasticsearch