【问题标题】:Searching in specific indices with JSON object使用 JSON 对象在特定索引中搜索
【发布时间】:2015-05-28 20:21:54
【问题描述】:

我有问题。在我的应用程序中,我使用的是 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();

任何人都知道出了什么问题吗?我真的很感激任何

【问题讨论】:

    标签: elasticsearch mapping elasticsearch-dsl


    【解决方案1】:

    我认为您在这里误解了indices 的功能:当需要对索引列表执行某个查询另一个需要对索引执行的查询时,可以使用它不匹配索引列表。

    所以,这一切都取决于您运行它的索引。

    例如:

    GET /abc,test1,test2,test3/_search
    {
      "query": {
        "indices": {
          "indices": ["abc"],
          "query": {
            "bool": {
              "must": [
    ...
    

    将针对abc, test1, test2, test3 索引运行。与 "indices": ["abc"] 匹配的索引将与 query 运行。其他不匹配的索引(在我的示例中 - test1, test2, test3)将使来自 no_match_query 的查询针对它们运行。

    因此,运行indices 查询的索引很重要。 ignoring unmapped fields 是这里的路。

    【讨论】:

    • 好的,有道理。因此,如果我只想在特定索引上运行,我需要在 Java 中这样指定:client.prepareSearch().setIndices("index-1").setSource(json);当我有很多索引时,我担心性能
    • 是的,使用setIndices(...) 应该可以。
    猜你喜欢
    • 2012-10-30
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多