AddFields Seems to not return result

Hi,

I am trying to get elastic search to return a subset of fields from my documents. I am using the following code:

	SearchResponse response = client.prepareSearch("myIndex")
			.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
			.setQuery(query)
			.addField("myField")
			.setFrom(0)
			.setSize(10000).setExplain(false).execute().actionGet();
	SearchHit[] docs = response.getHits().getHits();
	for (SearchHit doc : docs)
		System.err.println(doc.getSource().keySet());
	System.out.println("size is " + docs.length);
	client.close();

This returns 8000 results but getSource() is null, when I use the REST API with

	String url  = "http://myServer:9200/myIndex/_search";
	String data = "{\"fields\":[\"myField\"], \"from\":0,\"size\" : 10000, \"query\": { \"bool\": { \"should\": [{ \"query_string\": { \"default_field\": \"body\", \"query\": \"*\" }}]}}}";
	HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
	
	httpGet.setEntity(new StringEntity(data, "UTF-8"));
	HttpResponse res = httpclient.execute(httpGet);

This works well, and returns the fields I am looking for.

What am I doing incorrectly?

Regards

D.

--

Hi All,

Fixed my problem,

I had to use

System.err.println(doc.getField("myField").getValue());

Regards

D.

Hi,

I am trying to get Elasticsearch to return a subset of fields from my documents. I am using the following code:

  SearchResponse response = client.prepareSearch("myIndex")
  		.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
  		.setQuery(query)
  		.addField("myField")
  		.setFrom(0)
  		.setSize(10000).setExplain(false).execute().actionGet();
  SearchHit[] docs = response.getHits().getHits();
  for (SearchHit doc : docs)
  	System.err.println(doc.getSource().keySet());
  System.out.println("size is " + docs.length);
  client.close();

This returns 8000 results but getSource() is null, when I use the REST API with

  String url  = "http://myServer:9200/myIndex/_search";
  String data = "{\"fields\":[\"myField\"], \"from\":0,\"size\" : 10000, \"query\": { \"bool\": { \"should\": [{ \"query_string\": { \"default_field\": \"body\", \"query\": \"*\" }}]}}}";
  HttpGetWithEntity httpGet = new HttpGetWithEntity(url);
  
  httpGet.setEntity(new StringEntity(data, "UTF-8"));
  HttpResponse res = httpclient.execute(httpGet);

This works well, and returns the fields I am looking for.

What am I doing incorrectly?

Regards

D.

--