Validate API

原文链接 : https://www.elastic.co/guide/en/elasticsearch/reference/current/search-validate.html

译文链接 : http://apache.wiki/display/Elasticsearch/Validate+API

贡献者 : @琴剑蓝天

validate API 允许用户验证一个可能复杂(expensive)的查询而不执行它。 我们将使用以下测试数据来解释_validate:

  1. PUT twitter/tweet/_bulk?refresh
  2. {"index":{"_id":1}}
  3. {"user" : "kimchy", "post_date" : "2009-11-15T14:12:12", "message" : "trying out Elasticsearch"}
  4. {"index":{"_id":2}}
  5. {"user" : "kimchi", "post_date" : "2009-11-15T14:12:13", "message" : "My username is similar to @kimchy!"}

当发送一个有效查询时:

  1. GET twitter/_validate/query?q=user:foo

响应包含有效:true:

  1. {"valid":true,"_shards":{"total":1,"successful":1,"failed":0}}

Request Parameters

当执行查询使用查询参数q时,传递的查询是使用Lucene查询解析器的查询字符串。 还有其他可以传递的参数:

Name Description
df 在查询中未定义字段前缀时使用的默认字段。
analyzer 分析查询字符串时使用的分析器名称。
default_operator 要使用的默认运算符,可以是 ANDOR。 默认为 OR
lenient 如果设置为 true 将导致基于格式的失败(例如向数字字段提供文本)被忽略。 默认为 false
lowercase_expanded_terms 术语是否自动小写,默认为 true
analyze_wildcard 是否分析通配符和前缀查询。 默认为false。

查询也可以在请求主体中发送:

  1. GET twitter/tweet/_validate/query
  2. {
  3. "query" : {
  4. "bool" : {
  5. "must" : {
  6. "query_string" : {
  7. "query" : "*:*"
  8. }
  9. },
  10. "filter" : {
  11. "term" : { "user" : "kimchy" }
  12. }
  13. }
  14. }
  15. }

注意

在正文中发送的查询必须嵌套在查询键中,与 Search API 相同。

如果查询无效,则返回信息中 valid 将为 false。 在这里,查询无效,因为 Elasticsearch 知道post_date字段应该是动态映射的日期,foo无法正确解析为日期:

  1. GET twitter/tweet/_validate/query?q=post_date:foo
  2. {"valid":false,"_shards":{"total":1,"successful":1,"failed":0}}

可以指定 explain 参数以获取有关查询失败原因的更详细信息:

  1. GET twitter/tweet/_validate/query?q=post_date:foo&explain=true

响应是:

  1. {
  2. "valid" : false,
  3. "_shards" : {
  4. "total" : 1,
  5. "successful" : 1,
  6. "failed" : 0
  7. },
  8. "explanations" : [ {
  9. "index" : "twitter",
  10. "valid" : false,
  11. "error" : "twitter/IAEc2nIXSSunQA_suI0MLw] QueryShardException[failed to create query:...failed to parse date field [foo]"
  12. } ]
  13. }

当查询有效时,explanations 默认为该查询的字符串表示形式。 将 rewrite 设置为 true 时,explanations 将更详细地显示将要执行的实际Lucene查询。

模糊查询(Fuzzy Queries):

  1. GET twitter/tweet/_validate/query?rewrite=true
  2. {
  3. "query": {
  4. "match": {
  5. "user": {
  6. "query": "kimchy",
  7. "fuzziness": "auto"
  8. }
  9. }
  10. }
  11. }

响应:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "twitter",
  11. "valid": true,
  12. "explanation": "+user:kimchy +user:kimchi^0.75 #(ConstantScore(_type:tweet))^0.0"
  13. }
  14. ]
  15. }

相似度查询(More Like This):

  1. GET twitter/tweet/_validate/query?rewrite=true
  2. {
  3. "query": {
  4. "more_like_this": {
  5. "like": {
  6. "_id": "2"
  7. },
  8. "boost_terms": 1
  9. }
  10. }
  11. }

响应:

  1. {
  2. "valid": true,
  3. "_shards": {
  4. "total": 1,
  5. "successful": 1,
  6. "failed": 0
  7. },
  8. "explanations": [
  9. {
  10. "index": "twitter",
  11. "valid": true,
  12. "explanation": "((user:terminator^3.71334 plot:future^2.763601 plot:human^2.8415773 plot:sarah^3.4193945 plot:kyle^3.8244398 plot:cyborg^3.9177752 plot:connor^4.040236 plot:reese^4.7133346 ... )~6) -ConstantScore(_uid:tweet#2)) #(ConstantScore(_type:tweet))^0.0"
  13. }
  14. ]
  15. }

警告

请求只在单个分片上执行,这是随机选择的。 查询的详细解释可以取决于哪个分片被命中,并且因此可以从一个请求到另一个请求而变化。