I am trying to get search results using java.
Using elasticsaerch-0.19.0, java 1.6.
I have multiple values in one field.
When I run curl query everything is fine (result contains 2 'to' and 2
'from' values):
Query:
curl -XGET '192.168.2.128:9200/_search?pretty=true' -d '{
"query" : {
"query_string" : {
"default_field" : "to",
"query" : "lincoln"
}
}
}
'
Result:
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.15342641,
"hits" : [ {
"_index" : "kj",
"_type" : "test",
"_id" : "EBSzqMM_QBW-Ar2-4syExg",
"_score" : 0.15342641, "_source" : {
"user" : "kj",
"post_date" : "2012-05-20T14:12:12",
"from" : "George Bush",
"from" : "George W Bush jr.",
"to" : "Abraham Lincoln" ,
"to" : "George Washington" ,
"subject": "Need help with the Iraq and BinLaden"
}
} ]
}
However, if I run the same query in java, there is only one value per
field. What am I doing wrong?
Java test code:
private SearchResult query(final Client client, final SearchCriteria
criteria ) {
final SearchResult result = new SearchResult() ;
final BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery() ;
queryBuilder.must( QueryBuilders.fieldQuery( "to", "lincoln" ) ) ;
// search in 'kj' index
final SearchRequestBuilder searchRequestBuilder =
client.prepareSearch("kj")
.addField("to")
.addField("from")
.addField("subject")
.addField("messageid")
.addField("sent")
.addField("received")
.addField("body") ;
// search in 'test' type
searchRequestBuilder.setTypes("test");
searchRequestBuilder.setSearchType(SearchType.DEFAULT);
searchRequestBuilder.setQuery(queryBuilder);
searchRequestBuilder.setFrom(0).setSize(60).setExplain(true);
final SearchResponse resp =
searchRequestBuilder.execute().actionGet();
result.setNumberOfHits( resp.getHits().getHits().length ) ;
for (final SearchHit hit : resp.getHits()) {
System.out.println("Hit ID: " + hit.getId()+"
score:"+hit.getScore() );
final Iterator<SearchHitField> iterator = hit.iterator() ;
while( iterator.hasNext() ) {
final SearchHitField hitField = iterator.next() ;
System.out.println(" field:"+hitField.getName()+" value
count:"+hitField.getValues().size() ) ;
for( final Object value : hitField.getValues() ) {
if ( value instanceof String ) {
System.out.println(" value:"+value ) ;
} else {
System.out.println(" type:"+value.getClass().getName() ) ;
}
}
}
System.out.println(" ");
}
return result ;
}
Output from java:
Hit ID: EBSzqMM_QBW-Ar2-4syExg score:0.15342641
field:to value count:1
value:George Washington
field:subject value count:1
value:Need help with the Iraq and BinLaden
field:from value count:1
value:George W Bush jr.