Search code examples
elasticsearchmappingelasticsearch-dsl

Searching in specific indices with JSON object


I have a problem. In my application I'm using ElasticSearch. I'm posting JSON object to my ElasticSearch server. That JSON object contain DSL query. So, what I need to do, is to query specific index for some data.

This is the query:

{  
   "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"
      }
   }
}

Query works just fine, it returns data which I want to. However, in ElasticSearch logs I can see:

[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]

It tries to fetch something from twitter index, which is some standard out-of-the-box index for testing. Why? I specified that I want to search in index-1, not all of them.

I found workaround, just to add: "ignore_unmapped" : true in sort, but it's not really a solution.

I don't know if it matters, but I set up a REST which I'm calling, and inside my Java app I'm passing JSON to ElasticSearch like that:

Client client = new TransportClient(settings);
SearchRequestBuilder builder = client .prepareSearch().setSource(json);
SearchResponse response = builder.execute().actionGet();

Anyone have any clue what is wrong? I would really appreciate any


Solution

  • I think you misunderstood the functionality of indices here: it can be used when a certain query needs to be executed against a list of indices and another query that needs to be executed on indices that do not match the list of indices.

    So, all depends on the indices you run this against.

    For example:

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

    will be run against abc, test1, test2, test3 indices. Indices that match "indices": ["abc"] will have the query run against. The other indices that don't match (in my example - test1, test2, test3) will have the query from no_match_query run against them.

    So, it is important against which indices you run your indices query. And ignoring unmapped fields is the way to go here.