如何找出elasticsearch解析query_string的结果? [英] How to find out result of elasticsearch parsing a query_string?
问题描述
有没有办法通过 elasticsearch API 找出 查询字符串查询 真的被解析了吗?您可以通过查看 lucene 查询语法来手动执行此操作,但它会是如果您可以查看解析器实际结果的一些表示,那就太好了.
Is there a way to find out via the elasticsearch API how a query string query is actually parsed? You can do that manually by looking at the lucene query syntax, but it would be really nice if you could look at some representation of the actual results the parser has.
推荐答案
正如 javanna 在评论中提到的那样,有 _validate api.以下是我的本地弹性(1.6 版)的工作原理:
As javanna mentioned in comments there's _validate api. Here's what works on my local elastic (version 1.6):
curl -XGET 'http://localhost:9201/pl/_validate/query?explain&pretty' -d'
{
"query": {
"query_string": {
"query": "a OR (b AND c) OR (d AND NOT(e or f))",
"default_field": "t"
}
}
}
'
pl
是我的集群上的索引名称.不同的索引可能有不同的分析器,这就是为什么在索引范围内执行查询验证的原因.
pl
is name of index on my cluster. Different index could have different analyzers, that's why query validation is executed in a scope of an index.
上述卷曲的结果如下:
{
"valid" : true,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"explanations" : [ {
"index" : "pl",
"valid" : true,
"explanation" : "filtered(t:a (+t:b +t:c) (+t:d -(t:e t:or t:f)))->cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@ce2d82f1)"
} ]
}
我故意将 OR
设为小写,正如您在解释中看到的那样,它被解释为标记而不是运算符.
I made one OR
lowercase on purpose and as you can see in explanation, it is interpreted as a token and not as a operator.
至于解释的解释.格式类似于 +-
运算符:
As for interpretation of the explanation. Format is similar to +-
operators of query string
query:
- ( 和 ) 字符开始和结束
bool 查询
- + 前缀表示将在
must
中的子句 - - 前缀表示将在
must_not
中的子句 - 没有前缀意味着它将在
should
中(default_operator
等于OR
)
- ( and ) characters start and end
bool query
- + prefix means clause that will be in
must
- - prefix means clause that will be in
must_not
- no prefix means that it will be in
should
(withdefault_operator
equal toOR
)
所以上面将等价于以下:
So above will be equivalent to following:
{
"bool" : {
"should" : [
{
"term" : { "t" : "a" }
},
{
"bool": {
"must": [
{
"term" : { "t" : "b" }
},
{
"term" : { "t" : "c" }
}
]
}
},
{
"bool": {
"must": {
"term" : { "t" : "d" }
},
"must_not": {
"bool": {
"should": [
{
"term" : { "t" : "e" }
},
{
"term" : { "t" : "or" }
},
{
"term" : { "t" : "f" }
}
]
}
}
}
}
]
}
}
我大量使用 _validate
api 来调试具有许多条件的复杂 filtered
查询.如果您想检查分析器如何标记化输入(如 url)或是否缓存了某些过滤器,则它特别有用.
I used _validate
api quite heavily to debug complex filtered
queries with many conditions. It is especially useful if you want to check how analyzer tokenized input like an url or if some filter is cached.
还有一个很棒的参数 rewrite
直到现在我才知道,这使得解释更加详细,显示了将要执行的实际 Lucene 查询.
There's also an awesome parameter rewrite
that I was not aware of until now, which causes the explanation to be even more detailed showing the actual Lucene query that will be executed.
这篇关于如何找出elasticsearch解析query_string的结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!