带有query_string和range的Elasticsearch

时间:2015-04-16 13:34:04

标签: elasticsearch

我正在尝试获得大于X的结果并且字段等于Y.我试过这个:

{
   "sort": {
      "datapublicacao": "desc"
   },
   "query": {
      "query_string": {
         "query": "fullslug:noticia"
      },
      "range": {
         "id": {
            "gt": "145890"
         }
      }
   }
}

但发生以下错误:

{
   "error": "SearchPhaseExecutionException[Failed to execute phase [query], all shards failed; shardFailures {[PkbOJvspSO-yk_8dRMmxcg][jornal.jurid][2]: SearchParseException[[jornal.jurid][2]: query[fullslug:noticia],from[-1],size[-1],sort[<custom:\"datapublicacao\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@4105c103>!]: Parse Failure [Failed to parse source [{\n   \"sort\": {\n      \"datapublicacao\": \"desc\"\n   },\n   \"query\": {\n      \"query_string\": {\n         \"query\": \"fullslug:noticia\"\n      },\n      \"range\": {\n         \"id\": {\n            \"gt\": \"145890\"\n         }\n      }\n   }\n}\n]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"range\"]; }{[PkbOJvspSO-yk_8dRMmxcg][jornal.jurid][3]: SearchParseException[[jornal.jurid][3]: query[fullslug:noticia],from[-1],size[-1],sort[<custom:\"datapublicacao\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@32ef6244>!]: Parse Failure [Failed to parse source [{\n   \"sort\": {\n      \"datapublicacao\": \"desc\"\n   },\n   \"query\": {\n      \"query_string\": {\n         \"query\": \"fullslug:noticia\"\n      },\n      \"range\": {\n         \"id\": {\n            \"gt\": \"145890\"\n         }\n      }\n   }\n}\n]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"range\"]; }{[PkbOJvspSO-yk_8dRMmxcg][jornal.jurid][0]: SearchParseException[[jornal.jurid][0]: query[fullslug:noticia],from[-1],size[-1],sort[<custom:\"datapublicacao\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@1534ed83>!]: Parse Failure [Failed to parse source [{\n   \"sort\": {\n      \"datapublicacao\": \"desc\"\n   },\n   \"query\": {\n      \"query_string\": {\n         \"query\": \"fullslug:noticia\"\n      },\n      \"range\": {\n         \"id\": {\n            \"gt\": \"145890\"\n         }\n      }\n   }\n}\n]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"range\"]; }{[PkbOJvspSO-yk_8dRMmxcg][jornal.jurid][1]: SearchParseException[[jornal.jurid][1]: query[fullslug:noticia],from[-1],size[-1],sort[<custom:\"datapublicacao\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@21c787c>!]: Parse Failure [Failed to parse source [{\n   \"sort\": {\n      \"datapublicacao\": \"desc\"\n   },\n   \"query\": {\n      \"query_string\": {\n         \"query\": \"fullslug:noticia\"\n      },\n      \"range\": {\n         \"id\": {\n            \"gt\": \"145890\"\n         }\n      }\n   }\n}\n]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"range\"]; }{[PkbOJvspSO-yk_8dRMmxcg][jornal.jurid][4]: SearchParseException[[jornal.jurid][4]: query[fullslug:noticia],from[-1],size[-1],sort[<custom:\"datapublicacao\": org.elasticsearch.index.fielddata.fieldcomparator.LongValuesComparatorSource@45d53e55>!]: Parse Failure [Failed to parse source [{\n   \"sort\": {\n      \"datapublicacao\": \"desc\"\n   },\n   \"query\": {\n      \"query_string\": {\n         \"query\": \"fullslug:noticia\"\n      },\n      \"range\": {\n         \"id\": {\n            \"gt\": \"145890\"\n         }\n      }\n   }\n}\n]]]; nested: ElasticsearchParseException[Expected field name but got START_OBJECT \"range\"]; }]",
   "status": 400
}

如果我只使用query_string或只是范围进行查询,它就可以工作,但是它们都不起作用。

1 个答案:

答案 0 :(得分:5)

使用弹性搜索bool search,如下所示

{
"query": {
    "filtered": {
        "query": {
            "bool": {
                "must": [
                    {
                        "query_string": {
                            "query": "fullslug:noticia"
                        }
                    },
                    {
                        "range": {
                            "id": {
                                "gte": 145890
                            }
                        }
                    }
                ]
            }
        }
    }
},
"sort": {
    "datapublicacao": "desc"
}
}