SearchHits in ElasticSearch contains no fields using Java API

I run this code to return matches

    public ArrayList<InnerText> suggest(String text, boolean isPersonal){

            TransportClient transportClient = new TransportClient();
            Client ESclient = transportClient.addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
            OrientGraph graph = new OrientGraph(ConnectionStrings.dbConnection);
            Map<String, Object> template_params = new HashMap<>();
            template_params.put("innerText", text);
            SearchResponse response = ESclient.prepareSearch()
                    .setTemplateName("innerText_match")
                    .setTemplateType(ScriptService.ScriptType.FILE)
                    .setTemplateParams(template_params)
                    .execute()
                    .actionGet();
            int index = 0;
            ArrayList<InnerText> list = new ArrayList<InnerText>();
            for (SearchHit hit : response.getHits()) {

                //String uuid = hit.field(InnerTextProps.uuid).toString();
                //InnerText innerText = vertexToInnerText(graph.getVertexByKey("InnerText.uuid",uuid),false);
                //list.add(null);
                //hit fields map is  org.elasticsearch.util.collect.EmptyImmutableMap
            }
            return list; 
    }

and I get backbeats, but the field map for each of the SearchHits is empty (org.elasticsearch.util.collect.EmptyImmutableMap). They have the correct identifier and type. When using the Sense plugin, I see that things are stored and retrieved properly with the same request. What am I doing wrong?

Thank!

+4
source share
1 answer

After some study of the various methods, I found that:

  • SearchHit.sourceAsMap() will give you access to data if no specific fields have been requested.
  • SearchHit.getFields() will give you access to certain fields requested in your request using SearchResponse.addFieldDataField( String )

( ), Map String, Map:

Map<String,Object> contents = hit.sourceAsMap();
for ( String name : contents.keySet() )
{
    Object value = contents.get(name);
    System.out.println( "Hit field: "+name+ " object: "+value.getClass().getName() );
    if ( contents.get(name) instanceof java.util.HashMap )
    {
        // Handle map object
    }
    else
    {
        // Handle string value
    }
}

, , API SearchHit SearchHitField, .

SearchHitField :

        Map<String,SearchHitField> fields = hit.getFields();
        System.out.println( "Hit fields: "+fields.size() );
        for ( SearchHitField field : fields.values() )
        {
            System.out.println( " Hit field: "+field.getName() );
            System.out.println( " Value: "+field.getValue() );
        }

null . ...

EDIT2:

-, addFieldDataField() , . " <fieldname>".

-, addField(), .

  • SearchResponse.addFieldDataField( <fieldname>.raw )
  • SearchResponse.addField( <leaffieldname> )

SearchHitField.getValue(). .

+2

Source: https://habr.com/ru/post/1584037/


All Articles