如何找出弹性搜索结果解析一个query_string? [英] How to find out result of elasticsearch parsing a query_string?

查看:110
本文介绍了如何找出弹性搜索结果解析一个query_string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法通过弹性搜索 API找到一个查询字符串查询实际上已解析?您可以通过查看 lucene查询语法来手动执行此操作,但这将是真的很好,如果你可以看看解析器的实际结果的一些表示。

解决方案

由于javanna在评论中提到了 _ validate api。以下是我的本地弹性(1.6版)的功能:

  curl -XGET'http:// localhost:9201 / _ $ _ $$$$$$$$$$$$$ OR(d AND NOT(e或f)),
default_field:t
}
}
}
'

pl 是我的集群上的索引名称。不同的索引可以有不同的分析器,这就是为什么查询验证在索引的范围内执行。



以上卷曲的结果如下:

  {
valid:true,
_shards:{
total:1,
success:1,
failed:0
},
explanation:[{
index:pl,
(t:a(+ t:b + t:c)(+ t:d - (t:et:或t:f))) - > cache(org.elasticsearch.index.search.nested.NonNestedDocsFilter@ce2d82f1)
}]
}

我在目的上制作了一个 OR 小写,正如您在说明中可以看到的,它被解释为一个令牌,而不是一个操作符。



关于解释的解释。格式类似于 + - 查询字符串的string-query.html#_boolean_operatorsrel =nofollow>运算符查询:




  • (和)字符开始和结束 bool query

  • +前缀表示将在必须

  • - 前缀为 must_not

  • 没有前缀意味着它将在应该(与 default_operator 等于 OR



所以上面将相当于以下内容:

  {
bool:{
should:[
{
term {t:a}
},
{
bool:{
必须:[
{
:{t:b}
},
{
term:{t:c}

]
}
},
{
bool:{
必须:{
term:{ t:d}
},
must_not:{
bool:{
should:[
{
term:{t:e}
},
{
term:{t:or}
},
{
term:{t:f}
}
]
}
}
}
}
]
}
}

我使用 _validate api相当大地调试复杂的过滤查询有许多条件。如果您想要检查分析器如何将URL输入如url或某些过滤器进行缓存,则特别有用。



还有一个非常棒的参数重写到目前为止我还没有意识到这一点,这将使得更详细的说明显示将执行的实际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.

解决方案

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 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.

The result of the above curl is following:

{
  "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)"
  } ]
}

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:

  • ( 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 (with default_operator equal to OR)

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" }
                }
              ]
            }
          }
        }
      }
    ]
  }
}

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.

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.

这篇关于如何找出弹性搜索结果解析一个query_string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆