如何找出elasticsearch解析query_string的结果?

Han*_*örr 8 lucene elasticsearch

有没有办法通过elasticsearch API 找出实际解析查询字符串查询的方式?您可以通过查看lucene查询语法手动执行此操作,但如果您可以查看解析器具有的实际结果的某些表示,那将非常好.

sla*_*wek 5

正如javanna在评论中提到的那样_validate api.这是我的本地弹性(版本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"
    }
  }
}
'
Run Code Online (Sandbox Code Playgroud)

pl是我的群集上的索引名称.不同的索引可能有不同的分析器,这就是查询验证在索引范围内执行的原因.

以上卷曲的结果如下:

{
  "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)"
  } ]
}
Run Code Online (Sandbox Code Playgroud)

OR故意制作了一个小写,正如你在解释中看到的那样,它被解释为一个标记而不是一个操作符.

至于解释的解释.格式类似于+- 运营商query string查询:

  • (和)字符的开始和结束 bool query
  • + prefix表示将在的子句 must
  • - prefix表示将在的子句 must_not
  • 没有前缀意味着它将在should(default_operator等于OR)

所以上面将等同于以下内容:

{
  "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" }
                }
              ]
            }
          }
        }
      }
    ]
  }
}
Run Code Online (Sandbox Code Playgroud)

我使用_validateapi来调试filtered具有许多条件的复杂查询.如果您想要检查分析器如何标记输入(如url)或缓存某些过滤器,它尤其有用.

还有一个令人敬畏的参数rewrite,直到现在我还没有意识到,这使得解释更加详细,显示了将要执行的实际Lucene查询.