# Multi-value field search in java

**URL:** <https://discuss.elastic.co/t/multi-value-field-search-in-java/7824>\
**Category:** Elasticsearch\
**Created:** [May 23, 2012, 2:55pm UTC](https://discuss.elastic.co/t/multi-value-field-search-in-java/7824 "2012-05-23T14:55:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![chris123](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@chris123](https://discuss.elastic.co/u/chris123)\
**Post date:** [May 23, 2012, 2:55pm UTC](https://discuss.elastic.co/t/multi-value-field-search-in-java/7824/1 "2012-05-23T14:55:47Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![kimchy](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/kimchy/32/44952_2.png) [@kimchy](https://discuss.elastic.co/u/kimchy)\
**Post date:** [May 25, 2012, 8:35pm UTC](https://discuss.elastic.co/t/multi-value-field-search-in-java/7824/2 "2012-05-25T20:35:44Z")

</div>

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](mailto: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.

---

<div class="post-metadata">

**Author:** ![chris123](https://avatars.discourse-cdn.com/v4/letter/c/5fc32e/32.png) [@chris123](https://discuss.elastic.co/u/chris123)\
**Post date:** [June 19, 2012, 3:58pm UTC](https://discuss.elastic.co/t/multi-value-field-search-in-java/7824/3 "2012-06-19T15:58:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 3:23am UTC](https://discuss.elastic.co/t/multi-value-field-search-in-java/7824/4 "2017-07-06T03:23:24Z")

</div>


