我有一个问题。在我的应用程序中,我使用 ElasticSearch。我正在将 JSON 对象发布到我的 ElasticSearch 服务器。该 JSON 对象包含 DSL 查询。所以,我需要做的是查询某些数据的特定索引。
这是查询:
{
"query":{
"indices":{
"indices":[
"index-1"
],
"query":{
"bool":{
"must":[
{
"match":{
"_all":"this is test"
}
}
],
"should":[
{
"match":{
"country":"PL"
}
}
],
"minimum_should_match":1
}
},
"no_match_query":"none"
}
},
"from":0,
"size":10,
"sort":{
"name":{
"order":"ASC"
}
}
}
查询工作正常,它返回我想要的数据。但是,在 ElasticSearch 日志中我可以看到:
[2015-05-28 22:08:20,942][DEBUG][action.search.type] [index] [twitter][0], node[X_ASxSKmn-Bzmn-nHLQ8g], [P], s[STARTED]: Failed to execute [org.elasticsearch.action.search.SearchRequest@7b98e9ad] lastShard [true]
org.elasticsearch.search.SearchParseException: [twitter][0]: query[MatchNoDocsQuery],from[0],size[10]: Parse Failure [Failed to parse source [HERE_COMES_MY_JSON]]
at org.elasticsearch.search.SearchService.parseSource(SearchService.java:681)
at org.elasticsearch.search.SearchService.createContext(SearchService.java:537)
at org.elasticsearch.search.SearchService.createAndPutContext(SearchService.java:509)
at org.elasticsearch.search.SearchService.executeQueryPhase(SearchService.java:264)
at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:231)
at org.elasticsearch.search.action.SearchServiceTransportAction$5.call(SearchServiceTransportAction.java:228)
at org.elasticsearch.search.action.SearchServiceTransportAction$23.run(SearchServiceTransportAction.java:559)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:745)
Caused by: org.elasticsearch.search.SearchParseException: [twitter][0]: query[MatchNoDocsQuery],from[0],size[10]: Parse Failure [No mapping found for [name] in order to sort on]
它尝试从 Twitter 索引中获取某些内容,这是一些用于测试的标准开箱即用索引。为什么?我指定要在index-1 中搜索,而不是全部。
我找到了解决方法,只是添加:
“ignore_unmapped”:true
排序,但这并不是真正的解决方案。
我不知道这是否重要,但我设置了一个我正在调用的 REST,并在我的 Java 应用程序中将 JSON 传递给 ElasticSearch,如下所示:
Client client = new TransportClient(settings);
SearchRequestBuilder builder = client .prepareSearch().setSource(json);
SearchResponse response = builder.execute().actionGet();
有人知道出了什么问题吗?我真的很感激任何
最佳答案
我认为您在这里误解了索引
的功能:当需要针对索引列表和另一个需要执行的查询执行某个查询时,可以使用它。对不匹配索引列表的索引执行。
因此,一切都取决于您运行此操作所针对的索引。
例如:
GET /abc,test1,test2,test3/_search
{
"query": {
"indices": {
"indices": ["abc"],
"query": {
"bool": {
"must": [
...
将针对 abc、test1、test2、test3
索引运行。与 "indices": ["abc"]
匹配的索引将运行查询
。其他不匹配的索引(在我的示例中 - test1、test2、test3
)将针对它们运行来自 no_match_query
的查询。
因此,针对哪些索引运行 indices
查询非常重要。和ignoring unmapped fields这是去这里的路。
关于elasticsearch - 使用 JSON 对象搜索特定索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30516388/