'must'功能不适用于elasticsearch

时间:2012-05-17 06:16:14

标签: elasticsearch

我是Elasticsearch的新手并使用数据库。我想做这样的查询:

curl -X GET http://localhost:9200/project/flat_order/_search?pretty=true -d' { query:{ bool:{ must:{range:{created_at:{gte:"2012-01-01 00:00:00",lte:"2012-02-01 00:00:00"}}}}, {should:[{term:{status:"canceled"}}],minimum_number_should_match:1} } } }'

我收到了一个错误。 "error" : "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[YfeNrRnOTOqLOtQt65uPVw][project][1]: SearchParseException[[project][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [ { query:{ bool:{ must:{range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}}}, {should:[{term:{status:\"canceled\"}}],minimum_number_should_match:1} } } }]]]; nested: QueryParsingException[[project] Failed to parse]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@3742925a; line: 1, column: 102]]; }{[YfeNrRnOTOqLOtQt65uPVw][project][2]: SearchParseException[[project][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [ { query:{ bool:{ must:{range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}}}, {should:[{term:{status:\"canceled\"}}],minimum_number_should_match:1} } } }]]]; nested: QueryParsingException[[project] Failed to parse]; nested: JsonParseException[Unexpected character ('{' (code 123)): was expecting either valid name character (for unquoted name) or double-quote (for quoted) to start field name\n at [Source: [B@3742925a; line: 1, column: 102]]; }]", "status" : 500 }

怎么办? 提前谢谢。

这是我没有那些大括号的错误(封闭应该) "error" : "SearchPhaseExecutionException[Failed to execute phase [query], total failure; shardFailures {[YfeNrRnOTOqLOtQt65uPVw][project][0]: SearchParseException[[project][0]: query[created_at:[1325376000000 TO 1328054400999]],from[-1],size[-1]: Parse Failure [Failed to parse source [ { query:{ bool:{ must:{range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}}}, should:[{term:{status:\"canceled\"}}],minimum_number_should_match:1 } } }]]]; nested: SearchParseException[[project][0]: query[created_at:[1325376000000 TO 1328054400999]],from[-1],size[-1]: Parse Failure [No parser for element [term]]]; }{[YfeNrRnOTOqLOtQt65uPVw][project][4]: SearchParseException[[project][4]: query[created_at:[1325376000000 TO 1328054400999]],from[-1],size[-1]: Parse Failure [Failed to parse source [ { query:{ bool:{ must:{range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}}}, should:[{term:{status:\"canceled\"}}],minimum_number_should_match:1 } } }]]]; nested: SearchParseException[[project][4]: query[created_at:[1325376000000 TO 1328054400999]],from[-1],size[-1]: Parse Failure [No parser for element [term]]]; }]", "status" : 500 }

1 个答案:

答案 0 :(得分:2)

查看错误详细信息,我认为问题在于您的should子句有一个额外级别的嵌套,您不应该这样做。而不是:

must: { ... },
{ should: { ... },
  minimum_number_should_match:1}
}

尝试:

must: { ... },
should: { ... },
minimum_number_should_match:1}

编辑:您编辑的代码看起来像是您的查询:

query:{ bool:{ must:{range:{created_at:{gte:\"2012-01-01 00:00:00\",
lte:\"2012-02-01 00:00:00\"}}}}, should:[{term:status:\"canceled\"}}],
minimum_number_should_match:1 }
格式化的

将是:

query:{ 
 bool:{ 
  must:{
   range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}
  }
 },
 should: [
  {term:{status:\"canceled\"}}
 ],
 minimum_number_should_match:1
}

... should 之外的bool部分。换句话说,在must子句之后你有太多的结束括号。我相信它应该是:

query:{ 
 bool:{ 
  must:{
   range:{created_at:{gte:\"2012-01-01 00:00:00\",lte:\"2012-02-01 00:00:00\"}}
  },
  should: [
   {term:{status:\"canceled\"}}
  ],
  minimum_number_should_match:1
 } 
}