Multi-value field search in java

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.

You are not executing the same search in the curl request compared to the
Java one. For example, in the curl one, you execute a search across all
indices and types, and in Java, you execute it against a specific index and
type.

On Wed, May 23, 2012 at 4:55 PM, Chris123 kjezak@reflexion.net wrote:

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.

You are right, of course.

However, I still have the problem, but only when I use the local node.

In other words, when I run my search connecting my client node as not
local, everything is fine.
In this case I use:
NodeBuilder.nodeBuilder().local(false).loadConfigSettings(true).node();

However, in my junit test I use a local node and create it in the following
way:
NodeBuilder.nodeBuilder().local(true).loadConfigSettings(true).node();

And in the later case I am getting only one value per field back.

I have the same elasticsearch.yml and template_test.json (template
definition) in both cases.
Using elasticsearch 0.19.4

It is not a biggie, just bothers me that I cannot make it work.
Everything else: attachments, replication, shards works just great.

On Friday, May 25, 2012 4:35:44 PM UTC-4, kimchy wrote:

You are not executing the same search in the curl request compared to the
Java one. For example, in the curl one, you execute a search across all
indices and types, and in Java, you execute it against a specific index and
type.

On Wed, May 23, 2012 at 4:55 PM, Chris123 wrote:

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.