Get query result when _source set disabled using Java API


(Gunho Shin) #1

Hi, I learning ES for log management system.
I was run on my index using putMapping() method, and set '_source : disabled'.
I confirmed at HEAD plugin that it has been applied.
My type has two columns that one is stored and another is not.

When I use match query, getTotalHits() method returns 4 but each SearchHit's getSource() method returns null in my loop clause.

So, I change my source code like this

SearchResponse searchResponse = client.prepareSearch(indexName).setTypes(typeName)***.addFields(field[0])***//Added
            .setSearchType(SearchType.QUERY_THEN_FETCH)
            .setQuery(matchQuery(field[0], value))
            .setFrom(0).setSize(1000).execute().actionGet();

SearchHit[] result = searchResponse.getHits().getHits();

 for (SearchHit hit : result) {
//My new code
        **Set<String> keys = hit.fields().keySet();**
        **Iterator<String> keyItr = keys.iterator();**
        **while(keyItr.hasNext()){**
            **SearchHitField hitField = hit.field(keyItr.next());**
            **System.out.println(hitField.getValue().toString());**
        **}**
// My older code
//            Map<String, Object> r = hit.getSource();
//            System.out.println(r);
    }

Changed code looks not bad. Returned data set looks like correct one.
But because I can't see my data list at my HEAD plugin, I can't be sure this code.
Is it right way? I need anyone to answer. Thanks!


(system) #2