Zac*_*ack 2 querydsl elasticsearch
我在理解弹性搜索中嵌套查询的各种方法时遇到了一些麻烦.以下是我的索引数据的示例.
{
"Underlying" : "Eurodollar",
"Expiration" : "20160315"
},
{
"Underlying" : "Eurodollar",
"Expiration" : "20160415"
},
{
"Underlying" : "Eurodollar",
"Expiration" : "20160515"
}
Run Code Online (Sandbox Code Playgroud)
所以,我能够运行这样的查询
{
"query" : {
"range" : {
"Expiration" : {
"gte" : "20160315",
"lte" : "20160515"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
正如所料,我得到了所有参赛作品.但是,假设我还索引了这样的条目.
{
"Underlying" : "Something else",
"Expiration" : "20160415"
}
Run Code Online (Sandbox Code Playgroud)
我不希望"别的"结果回来,所以我现在尝试做这样的事情.
{
"query" : {
"bool" : {
"must" : [
{
"term" : {
"Underlying" : {
"value" : "eurodollar"
}
}
}
]
},
"range" : {
"Expiration" : {
"gte" : "20160315",
"lte" : "20160515"
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了一个错误
RequestError(400, u'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[cCrh939sR7yHdKgawRi6Sw][test-index][0]: SearchParseException[[test-index][0]: query[Expiration:[20160215 TO 20160415]],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"range": {"Expiration": {"gte": "20160215", "lte": "20160415"}}, "bool": {"must": [{"term": {"Underlying": {"value": "eurodollar"}}}]}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "bool"]; }{[cCrh939sR7yHdKgawRi6Sw][test-index][1]: SearchParseException[[test-index][1]: query[Expiration:[20160215 TO 20160415]],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"range": {"Expiration": {"gte": "20160215", "lte": "20160415"}}, "bool": {"must": [{"term": {"Underlying": {"value": "eurodollar"}}}]}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "bool"]; }]', {u'status': 400, u'error': u'SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[cCrh939sR7yHdKgawRi6Sw][test-index][0]: SearchParseException[[test-index][0]: query[Expiration:[20160215 TO 20160415]],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"range": {"Expiration": {"gte": "20160215", "lte": "20160415"}}, "bool": {"must": [{"term": {"Underlying": {"value": "eurodollar"}}}]}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "bool"]; }{[cCrh939sR7yHdKgawRi6Sw][test-index][1]: SearchParseException[[test-index][1]: query[Expiration:[20160215 TO 20160415]],from[-1],size[-1]: Parse Failure [Failed to parse source [{"query": {"range": {"Expiration": {"gte": "20160215", "lte": "20160415"}}, "bool": {"must": [{"term": {"Underlying": {"value": "eurodollar"}}}]}}}]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT "bool"]; }]'})
Run Code Online (Sandbox Code Playgroud)
错误的最相关文本似乎是这样
Expected field name but got START_OBJECT "bool"
Run Code Online (Sandbox Code Playgroud)
我知道我的bool/term查询正在运行,因为我运行了这个
{
"query" : {
"bool" : {
"must" : [
{
"term" : {
"Underlying" : {
"value" : "eurodollar"
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
我收回了预期的结果.
我认为这足以说明我的情况.我如何正确地组合这些查询,因为我猜测它们应该如何组合是不正确的.
您的范围查询必须位于must子句中:
{
"query" : {
"bool" : {
"must" : [
{
"term" : {
"Underlying" : {
"value" : "eurodollar"
}
}
},
{
"range" : {
"Expiration" : {
"gte" : "20160315",
"lte" : "20160515"
}
}
}
]
}
}
}
Run Code Online (Sandbox Code Playgroud)
您将不同的查询与bool
查询相结合.它采取4种不同的子句中它:must
,should
,not_must
和filter
.
filter
和...一样must
.差异是过滤器的分数不计算在内.
基本结构(取自doc):
{
"bool" : {
"must" : {
"term" : { "user" : "kimchy" }
},
"filter": {
"term" : { "tag" : "tech" }
},
"must_not" : { <= single inside query
"range" : {
"age" : { "from" : 10, "to" : 20 }
}
},
"should" : [ <= that is an array
{ <= start of inner query is important
"term" : { "tag" : "wow" }
},
{
"term" : { "tag" : "elasticsearch" }
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2703 次 |
最近记录: |