# Strange search results with JAVA termQuery search

**URL:** <https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053>\
**Category:** Elasticsearch\
**Created:** [September 18, 2012, 3:35pm UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053 "2012-09-18T15:35:40Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Percival\_Xavier](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Percival\_Xavier](https://discuss.elastic.co/u/Percival_Xavier)\
**Post date:** [September 18, 2012, 3:35pm UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053/1 "2012-09-18T15:35:40Z")

</div>

I have created an index at this location:  
[http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48](http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48)

and it returns

{"\_index":"ana.smth","\_type":"Name","\_id":"120918225128-a7217b24-db48","\_version":2,"exists":true, "\_source" :  
{"feat":  
{"elements":  
{"lastName":"Smith","firstName":"Anna"},"featName":"Name"},  
"obsEntId":"120918225128-5c79d1ed-1e38",  
"\_srch\_index":"ana.smth",  
"param1":"Anna",  
"param2":"Smith",  
"\_srch\_name":"Name",  
"\_srch\_Id":"120918225128-a7217b24-db48","isValid":false  
}  
}

When i use the search java api like below :

```
       TermQueryBuilder tq = new TermQueryBuilder("_srch_index","ana.smth");
   //TermQueryBuilder tq = new TermQueryBuilder("param1","Anna");
	 
     SearchResponse response = client.prepareSearch(sq.getIndex())
            .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
	       
            .setQuery(tq)
       
	        .setFrom(0).setSize(60).setExplain(true)
	        .execute()
	        .actionGet();
    SearchHits sh = response.getHits();
    SearchHit[] shList = sh.getHits();

```

I can get a reponse (with hits) for TermQueryBuilder("\_srch\_index","ana.smth") but not TermQueryBuilder("param1","Anna") even though both  
terms are inside the JSON result. Whats seems to be the problem ?

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 18, 2012, 3:38pm UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053/2 "2012-09-18T15:38:37Z")

</div>

Try with anna to see if it works.

--  
David 😉  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

Le 18 sept. 2012 à 17:35, Percival Xavier [xavier.percival@gmail.com](mailto:xavier.percival@gmail.com) a écrit :

> I have created an index at this location:  
> [http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48](http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48)
> 
> and it returns  
> {"\_index":"ana.smth","\_type":"Name","\_id":"120918225128-a7217b24-db48","\_version":2,"exists":true, "\_source" :  
> {"feat":  
> {"elements":  
> {"lastName":"Smith","firstName":"Anna"},"featName":"Name"},  
> "obsEntId":"120918225128-5c79d1ed-1e38",  
> "\_srch\_index":"ana.smth",  
> "param1":"Anna",  
> "param2":"Smith",  
> "\_srch\_name":"Name",  
> "\_srch\_Id":"120918225128-a7217b24-db48","isValid":false  
> }  
> }
> 
> When i use the search java api like below :
> 
> ```
> TermQueryBuilder tq = new TermQueryBuilder("_srch_index","ana.smth");
> //TermQueryBuilder tq = new TermQueryBuilder("param1","Anna");
>    
> SearchResponse response = client.prepareSearch(sq.getIndex())
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
>          
> .setQuery(tq)
>        
> .setFrom(0).setSize(60).setExplain(true)
> .execute()
> .actionGet();
> SearchHits sh = response.getHits();
> SearchHit[] shList = sh.getHits();
> 
> ```
> 
> I can get a reponse (with hits) for TermQueryBuilder("\_srch\_index","ana.smth") but not TermQueryBuilder("param1","Anna") even though both  
> terms are inside the JSON result. Whats seems to be the problem ?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![Percival\_Xavier](https://avatars.discourse-cdn.com/v4/letter/p/ee7513/32.png) [@Percival\_Xavier](https://discuss.elastic.co/u/Percival_Xavier)\
**Post date:** [September 18, 2012, 3:54pm UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053/3 "2012-09-18T15:54:38Z")

</div>

yes, it does! is there a reason why "anna" but not "Anna". both ways work  
well for me but i'm just curious.

On Tuesday, September 18, 2012 11:38:48 PM UTC+8, David Pilato wrote:

> Try with anna to see if it works.
> 
> --  
> David 😉  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> Le 18 sept. 2012 à 17:35, Percival Xavier \<[xavier....@gmail.com](mailto:xavier....@gmail.com)\<javascript:\>\>  
> a écrit :
> 
> I have created an index at this location:  
> [http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48](http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48)
> 
> and it returns
> 
> {"\_index":"ana.smth","\_type":"Name","\_id":"120918225128-a7217b24-db48","\_version":2,"exists":true, "\_source" :  
> {"feat":  
> {"elements":  
> {"lastName":"Smith","firstName":"Anna"},"featName":"Name"},  
> "obsEntId":"120918225128-5c79d1ed-1e38",  
> "\_srch\_index":"ana.smth",  
> "param1":"Anna",  
> "param2":"Smith",  
> "\_srch\_name":"Name",  
> "\_srch\_Id":"120918225128-a7217b24-db48","isValid":false  
> }  
> }
> 
> When i use the search java api like below :
> 
> ```
> TermQueryBuilder tq = new TermQueryBuilder("_srch_index","ana.smth");
> //TermQueryBuilder tq = new TermQueryBuilder("param1","Anna");
>    
> SearchResponse response = client.prepareSearch(sq.getIndex())
> .setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
>          
> .setQuery(tq)
>        
> .setFrom(0).setSize(60).setExplain(true)
> .execute()
> .actionGet();
> SearchHits sh = response.getHits();
> SearchHit[] shList = sh.getHits();
> 
> ```
> 
> I can get a reponse (with hits) for TermQueryBuilder("\_srch\_index","ana.smth") but not TermQueryBuilder("param1","Anna") even though both  
> terms are inside the JSON result. Whats seems to be the problem ?
> 
> --

--

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [September 18, 2012, 4:02pm UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053/4 "2012-09-18T16:02:33Z")

</div>

Yes. There's a good reason. TermQuery does not analyze content.  
But, you have indexed your field with a default analyzer and Anna is indexed in  
Lucene as anna.

TermQuery compare Anna with anna which did not match.  
You can use a matchQuery (starting 0.19.9 or a TextQuery for previous versions)  
which will analyze your content and will compare anna with anna

HTH  
David

Le 18 septembre 2012 à 17:54, Percival Xavier [xavier.percival@gmail.com](mailto:xavier.percival@gmail.com) a  
écrit :

> yes, it does! is there a reason why "anna" but not "Anna". both ways work  
> well for me but i'm just curious.
> 
> On Tuesday, September 18, 2012 11:38:48 PM UTC+8, David Pilato wrote:
> 
> > > Try with anna to see if it works.
> > 
> > --  
> > David 😉  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > Le 18 sept. 2012 à 17:35, Percival Xavier \< [xavier....@gmail.com](mailto:xavier....@gmail.com)\> a écrit  
> > :
> > 
> > ```
> > > > > 
> > 
> > ```
> > 
> > > ```
> > > I have created an index at this location:
> > > http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48
> > > 
> > > and it returns
> > > <http://localhost:9200/ana.smth/Name/120918225128-a7217b24-db48>
> > > 
> > > {"_index":"ana.smth","_type":"Name","_id":"120918225128-a7217b24-db48","_version":2,"exists":true,
> > > 
> > > ```
> > > 
> > > "\_source" :  
> > > {"feat":  
> > > {"elements":
> > > 
> > > ```
> > > {"lastName":"Smith","firstName":"Anna"},"featName":"Name"},
> > > "obsEntId":"120918225128-5c79d1ed-1e38",
> > > "_srch_index":"ana.smth",
> > > "param1":"Anna",
> > > "param2":"Smith",
> > > "_srch_name":"Name",
> > > 
> > > "_srch_Id":"120918225128-a7217b24-db48","isValid":false
> > > }
> > > }
> > > 
> > > When i use the search java api like below :
> > > 
> > > TermQueryBuilder tq = new
> > > 
> > > ```
> > > 
> > > TermQueryBuilder("\_srch\_index","ana.smth");  
> > > //TermQueryBuilder tq = new TermQueryBuilder("param1","Anna");
> > > 
> > > ```
> > > SearchResponse response =
> > > 
> > > ```
> > > 
> > > client.prepareSearch(sq.getIndex())  
> > > .setSearchType(SearchType.DFS\_QUERY\_THEN\_FETCH)
> > > 
> > > ```
> > > .setQuery(tq)
> > > 
> > > .setFrom(0).setSize(60).setExplain(true)
> > > .execute()
> > > .actionGet();
> > > SearchHits sh = response.getHits();
> > > SearchHit[] shList = sh.getHits();
> > > 
> > > I can get a reponse (with hits) for
> > > 
> > > ```
> > > 
> > > TermQueryBuilder("\_srch\_index","ana.smth") but not  
> > > TermQueryBuilder("param1","Anna") even though both  
> > > terms are inside the JSON result. Whats seems to be the problem ?
> > > 
> > > ```
> > > --
> > > 
> > > --
> > > 
> > > ```
> > > 
> > > > > >

--  
David Pilato  
[http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
[http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

---

<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:12am UTC](https://discuss.elastic.co/t/strange-search-results-with-java-termquery-search/9053/5 "2017-07-06T03:12:28Z")

</div>


