# Search API help

**URL:** <https://discuss.elastic.co/t/search-api-help/9843>\
**Category:** Elasticsearch\
**Created:** [November 27, 2012, 2:54pm UTC](https://discuss.elastic.co/t/search-api-help/9843 "2012-11-27T14:54:10Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![smhdiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smhdiu/32/1808_2.png) [@smhdiu](https://discuss.elastic.co/u/smhdiu)\
**Post date:** [November 27, 2012, 2:54pm UTC](https://discuss.elastic.co/t/search-api-help/9843/1 "2012-11-27T14:54:10Z")

</div>

I am searching using the JAVA API as follows :

# Test case 1 : (it should return user with \*[gmail.com](http://gmail.com) and have firstname as "abc"

email : "[gmail.com](http://gmail.com)"  
firstname : "abc"

boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
.should(wildcardQuery("firstname",  
"_"+criteria.getFirstName()+"_"));

# result :

email : [xyz@gmail.com](mailto:xyz@gmail.com)  
firstname : xyz

email : [abc@gmail.com](mailto:abc@gmail.com)  
firstname : abc

here i want only one result(2nd result) that is where both the field is  
satisfied.  
how to achieve that?

note that user can input in email partial values as "[gmail.com](http://gmail.com)", thats ahy  
i am using wildcard there.

Thanks in advanced.

Mohsin

--

---

<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:** [November 27, 2012, 3:02pm UTC](https://discuss.elastic.co/t/search-api-help/9843/2 "2012-11-27T15:02:35Z")

</div>

Not sure to fully understand but did you try with must clauses instead of  
should?

David.

Le 27 novembre 2012 à 15:54, mohsin husen [smhdiu@gmail.com](mailto:smhdiu@gmail.com) a écrit :

> I am searching using the JAVA API as follows :
> 
> # Test case 1 : (it should return user with \*[gmail.com](http://gmail.com) and have firstname as "abc"
> 
> email : "[gmail.com](http://gmail.com)"  
> firstname : "abc"
> 
> boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> .should(wildcardQuery("firstname",  
> "_"+criteria.getFirstName()+"_"));
> 
> # result :
> 
> email : [xyz@gmail.com](mailto:xyz@gmail.com)  
> firstname : xyz
> 
> email : [abc@gmail.com](mailto:abc@gmail.com)  
> firstname : abc
> 
> here i want only one result(2nd result) that is where both the field is  
> satisfied.  
> how to achieve that?
> 
> note that user can input in email partial values as "[gmail.com](http://gmail.com)", thats ahy i  
> am using wildcard there.
> 
> Thanks in advanced.
> 
> Mohsin
> 
> --

--  
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:** ![smhdiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smhdiu/32/1808_2.png) [@smhdiu](https://discuss.elastic.co/u/smhdiu)\
**Post date:** [November 27, 2012, 3:24pm UTC](https://discuss.elastic.co/t/search-api-help/9843/3 "2012-11-27T15:24:15Z")

</div>

Thanks David for reply 🙂

Let me repeat my use case.

# ==================================================== Case 1 : (which is working as expected)

email : [gmail.com](http://gmail.com)

# query

boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))

# result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)

[abc@gmail.com](mailto:abc@gmail.com)  
[xyz@gmail.com](mailto:xyz@gmail.com)

# ================================================ Case 2 : (now here i have problem ! )

email : [gmail.com](http://gmail.com)  
firstName : abc

# Query:

boolQuery()  
.should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
.should(termQuery("firstName", criteria.getFirstName()));

# Result

email : [abc@gmail.com](mailto:abc@gmail.com) , Firstname : abc  
email : [xyz@gmail.com](mailto:xyz@gmail.com) , Firstname : xyz

problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as abc  
so it should return only one result rather than two results.

David, I also tried must after your suggestion but it is not returning any  
hit.

Please advice,  
Thanks in advanced  
Mohsin H

On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:

> Not sure to fully understand but did you try with must clauses instead  
> of should?
> 
> David.
> 
> Le 27 novembre 2012 à 15:54, mohsin husen \<[smh...@gmail.com](mailto:smh...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> I am searching using the JAVA API as follows :
> 
> # Test case 1 : (it should return user with \*[gmail.com](http://gmail.com) and have firstname as "abc"
> 
> email : "[gmail.com](http://gmail.com)"  
> firstname : "abc"
> 
> boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> .should(wildcardQuery("firstname",  
> "_"+criteria.getFirstName()+"_"));
> 
> # result :
> 
> email : [x...@gmail.com](mailto:x...@gmail.com) \<javascript:\>  
> firstname : xyz
> 
> email : [a...@gmail.com](mailto:a...@gmail.com) \<javascript:\>  
> firstname : abc
> 
> here i want only one result(2nd result) that is where both the field is  
> satisfied.  
> how to achieve that?
> 
> note that user can input in email partial values as "[gmail.com](http://gmail.com)", thats  
> ahy i am using wildcard there.
> 
> Thanks in advanced.
> 
> Mohsin
> 
> --
> 
> --  
> 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:** ![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:** [November 27, 2012, 3:49pm UTC](https://discuss.elastic.co/t/search-api-help/9843/4 "2012-11-27T15:49:16Z")

</div>

Have a look here: [Test case for must clause · GitHub](https://gist.github.com/4154982)

It works fine for me.

David.

Le 27 novembre 2012 à 16:24, mohsin husen [smhdiu@gmail.com](mailto:smhdiu@gmail.com) a écrit :

> Thanks David for reply 🙂
> 
> Let me repeat my use case.
> 
> # ==================================================== Case 1 : (which is working as expected)
> 
> email : [gmail.com](http://gmail.com)
> 
> # query
> 
> boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))
> 
> # result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)
> 
> [abc@gmail.com](mailto:abc@gmail.com)  
> [xyz@gmail.com](mailto:xyz@gmail.com)
> 
> # ================================================ Case 2 : (now here i have problem ! )
> 
> email : [gmail.com](http://gmail.com)  
> firstName : abc
> 
> # Query:
> 
> boolQuery()  
> .should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> .should(termQuery("firstName", criteria.getFirstName()));
> 
> # Result
> 
> email : [abc@gmail.com](mailto:abc@gmail.com) , Firstname : abc  
> email : [xyz@gmail.com](mailto:xyz@gmail.com) , Firstname : xyz
> 
> problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as abc so  
> it should return only one result rather than two results.
> 
> David, I also tried must after your suggestion but it is not returning any  
> hit.
> 
> Please advice,  
> Thanks in advanced  
> Mohsin H
> 
> On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:
> 
> > > Not sure to fully understand but did you try with must clauses  
> > > instead of should?
> > 
> > David.
> > 
> > Le 27 novembre 2012 à 15:54, mohsin husen \< [smh...@gmail.com](mailto:smh...@gmail.com)\> a écrit :
> > 
> > ```
> > > > > I am searching using the JAVA API as follows :
> > 
> > ```
> > 
> > > ```
> > > Test case 1 : (it should return user with * gmail.com
> > > 
> > > ```
> > > 
> > > [http://gmail.com](http://gmail.com) and have firstname as "abc"  
> > > =====================  
> > > email : " [gmail.com](http://gmail.com) [http://gmail.com](http://gmail.com) "  
> > > firstname : "abc"
> > > 
> > > ```
> > > boolQuery().should(wildcardQuery("email",
> > > 
> > > ```
> > > 
> > > "_"+criteria.getEmail()+"_"))  
> > > .should(wildcardQuery("firstname",  
> > > "_"+criteria.getFirstName()+"_"));
> > > 
> > > ```
> > > result :
> > > =============
> > > email : x...@gmail.com
> > > firstname : xyz
> > > 
> > > email : a...@gmail.com
> > > firstname : abc
> > > 
> > > here i want only one result(2nd result) that is where both the field
> > > 
> > > ```
> > > 
> > > is satisfied.  
> > > how to achieve that?
> > > 
> > > ```
> > > note that user can input in email partial values as " gmail.com
> > > 
> > > ```
> > > 
> > > [http://gmail.com](http://gmail.com) ", thats ahy i am using wildcard there.
> > > 
> > > ```
> > > Thanks in advanced.
> > > 
> > > Mohsin
> > > 
> > > --
> > > 
> > > ```
> > > 
> > > > >
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/) [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/) [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > >
> 
> --

--  
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:** ![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:** [November 27, 2012, 4:23pm UTC](https://discuss.elastic.co/t/search-api-help/9843/5 "2012-11-27T16:23:06Z")

</div>

FYI, I created a full test case here:

> **[GitHub - elasticsearchfr/elasticsearch-java-tests: Elasticsearch Java Tests](https://github.com/elasticsearchfr/elasticsearch-java-tests)**
>
> Elasticsearch Java Tests. Contribute to elasticsearchfr/elasticsearch-java-tests development by creating an account on GitHub.

HTH  
David.

Le 27 novembre 2012 à 16:49, David Pilato [david@pilato.fr](mailto:david@pilato.fr) a écrit :

> Have a look here: [Test case for must clause · GitHub](https://gist.github.com/4154982)
> 
> It works fine for me.
> 
> David.
> 
> Le 27 novembre 2012 à 16:24, mohsin husen [smhdiu@gmail.com](mailto:smhdiu@gmail.com) a écrit :
> 
> > > Thanks David for reply 🙂
> > 
> > Let me repeat my use case.
> > 
> > # ==================================================== Case 1 : (which is working as expected)
> > 
> > email : [gmail.com](http://gmail.com)
> > 
> > # query
> > 
> > boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))
> > 
> > # result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)
> > 
> > [abc@gmail.com](mailto:abc@gmail.com)  
> > [xyz@gmail.com](mailto:xyz@gmail.com)
> > 
> > # ================================================ Case 2 : (now here i have problem ! )
> > 
> > email : [gmail.com](http://gmail.com)  
> > firstName : abc
> > 
> > # Query:
> > 
> > boolQuery()  
> > .should(wildcardQuery("email",  
> > "_"+criteria.getEmail()+"_"))  
> > .should(termQuery("firstName", criteria.getFirstName()));
> > 
> > # Result
> > 
> > email : [abc@gmail.com](mailto:abc@gmail.com) , Firstname : abc  
> > email : [xyz@gmail.com](mailto:xyz@gmail.com) , Firstname : xyz
> > 
> > problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as abc  
> > so it should return only one result rather than two results.
> > 
> > David, I also tried must after your suggestion but it is not returning any  
> > hit.
> > 
> > Please advice,  
> > Thanks in advanced  
> > Mohsin H
> > 
> > On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:  
> > \> \> \> Not sure to fully understand but did you try with must clauses  
> > \> \> \> instead of should?
> > 
> > > ```
> > > David.
> > > 
> > > Le 27 novembre 2012 à 15:54, mohsin husen < smh...@gmail.com> a écrit
> > > 
> > > ```
> > > 
> > > :
> > > 
> > > ```
> > > > > > > I am searching using the JAVA API as follows :
> > > 
> > > ```
> > > 
> > > > ```
> > > > Test case 1 : (it should return user with * gmail.com
> > > > 
> > > > ```
> > > > 
> > > > [http://gmail.com](http://gmail.com) and have firstname as "abc"  
> > > > =====================  
> > > > email : " [gmail.com](http://gmail.com) [http://gmail.com](http://gmail.com) "  
> > > > firstname : "abc"
> > > > 
> > > > ```
> > > > boolQuery().should(wildcardQuery("email",
> > > > 
> > > > ```
> > > > 
> > > > "_"+criteria.getEmail()+"_"))  
> > > > .should(wildcardQuery("firstname",  
> > > > "_"+criteria.getFirstName()+"_"));
> > > > 
> > > > ```
> > > > result :
> > > > =============
> > > > email : x...@gmail.com
> > > > firstname : xyz
> > > > 
> > > > email : a...@gmail.com
> > > > firstname : abc
> > > > 
> > > > here i want only one result(2nd result) that is where both the
> > > > 
> > > > ```
> > > > 
> > > > field is satisfied.  
> > > > how to achieve that?
> > > > 
> > > > ```
> > > > note that user can input in email partial values as " gmail.com
> > > > 
> > > > ```
> > > > 
> > > > [http://gmail.com](http://gmail.com) ", thats ahy i am using wildcard there.
> > > > 
> > > > ```
> > > > Thanks in advanced.
> > > > 
> > > > Mohsin
> > > > 
> > > > --
> > > > 
> > > > > > > 
> > > > 
> > > > ```
> > > 
> > > ```
> > > --
> > > David Pilato
> > > http://www.scrutmydocs.org/ <http://www.scrutmydocs.org/>
> > > http://dev.david.pilato.fr/ <http://dev.david.pilato.fr/>
> > > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > > 
> > > ```
> > > 
> > > > >
> > 
> > --
> > 
> > >
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs

--

--  
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:** ![smhdiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smhdiu/32/1808_2.png) [@smhdiu](https://discuss.elastic.co/u/smhdiu)\
**Post date:** [November 27, 2012, 4:31pm UTC](https://discuss.elastic.co/t/search-api-help/9843/6 "2012-11-27T16:31:04Z")

</div>

Thanks a Ton !  
it is working fine as u said.  
i may be using other way around.

now problem is firstname/email may be blank !  
so wen it comes blank(either of them) no results are populated.  
i tried with should it is giving result(when one field is blank) but wen we  
search with firstname and email it has multiple record rather than one  
single record.

shall i use other stuffs like filters and all that to achieve this !

Thanks in advanced  
Mohsin H

On Tuesday, 27 November 2012 16:23:14 UTC, David Pilato wrote:

> FYI, I created a full test case here:  
> [GitHub - elasticsearchfr/elasticsearch-java-tests: Elasticsearch Java Tests](https://github.com/elasticsearchfr/elasticsearch-java-tests)
> 
> HTH  
> David.
> 
> Le 27 novembre 2012 à 16:49, David Pilato \<[da...@pilato.fr](mailto:da...@pilato.fr) \<javascript:\>\>  
> a écrit :
> 
> Have a look here: [Test case for must clause · GitHub](https://gist.github.com/4154982)
> 
> It works fine for me.
> 
> David.
> 
> Le 27 novembre 2012 à 16:24, mohsin husen \<[smh...@gmail.com](mailto:smh...@gmail.com) \<javascript:\>\>  
> a écrit :
> 
> Thanks David for reply 🙂
> 
> Let me repeat my use case.
> 
> # ==================================================== Case 1 : (which is working as expected)
> 
> email : [gmail.com](http://gmail.com)
> 
> # query
> 
> boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))
> 
> # result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)
> 
> [a...@gmail.com](mailto:a...@gmail.com) \<javascript:\>  
> [x...@gmail.com](mailto:x...@gmail.com) \<javascript:\>
> 
> # ================================================ Case 2 : (now here i have problem ! )
> 
> email : [gmail.com](http://gmail.com)  
> firstName : abc
> 
> # Query:
> 
> boolQuery()  
> .should(wildcardQuery("email",  
> "_"+criteria.getEmail()+"_"))  
> .should(termQuery("firstName", criteria.getFirstName()));
> 
> # Result
> 
> email : [a...@gmail.com](mailto:a...@gmail.com) \<javascript:\> , Firstname : abc  
> email : [x...@gmail.com](mailto:x...@gmail.com) \<javascript:\> , Firstname : xyz
> 
> problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as  
> abc so it should return only one result rather than two results.
> 
> David, I also tried must after your suggestion but it is not returning  
> any hit.
> 
> Please advice,  
> Thanks in advanced  
> Mohsin H
> 
> On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:
> 
> Not sure to fully understand but did you try with must clauses instead  
> of should?
> 
> David.
> 
> Le 27 novembre 2012 à 15:54, mohsin husen \< [smh...@gmail.com](mailto:smh...@gmail.com)\> a écrit :
> 
> I am searching using the JAVA API as follows :
> 
> # Test case 1 : (it should return user with \* [gmail.com](http://gmail.com) and have firstname as "abc"
> 
> email : " [gmail.com](http://gmail.com)"  
> firstname : "abc"
> 
> boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> .should(wildcardQuery("firstname",  
> "_"+criteria.getFirstName()+"_"));
> 
> # result :
> 
> email : [x...@gmail.com](mailto:x...@gmail.com)  
> firstname : xyz
> 
> email : [a...@gmail.com](mailto:a...@gmail.com)  
> firstname : abc
> 
> here i want only one result(2nd result) that is where both the field is  
> satisfied.  
> how to achieve that?
> 
> note that user can input in email partial values as " [gmail.com](http://gmail.com)", thats  
> ahy i am using wildcard there.
> 
> Thanks in advanced.
> 
> Mohsin
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> --
> 
> --  
> David Pilato  
> [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> 
> --
> 
> --  
> 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:** ![smhdiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smhdiu/32/1808_2.png) [@smhdiu](https://discuss.elastic.co/u/smhdiu)\
**Post date:** [November 27, 2012, 4:36pm UTC](https://discuss.elastic.co/t/search-api-help/9843/7 "2012-11-27T16:36:09Z")

</div>

it is working fine now.  
my silly mistake.  
really appreciated your help David !  
thanks a ton !  
ES looks quite interesting and innovative solution to search.  
Cheers !

On Tuesday, 27 November 2012 16:31:04 UTC, mohsin husen wrote:

> Thanks a Ton !  
> it is working fine as u said.  
> i may be using other way around.
> 
> now problem is firstname/email may be blank !  
> so wen it comes blank(either of them) no results are populated.  
> i tried with should it is giving result(when one field is blank) but wen  
> we search with firstname and email it has multiple record rather than one  
> single record.
> 
> shall i use other stuffs like filters and all that to achieve this !
> 
> Thanks in advanced  
> Mohsin H
> 
> On Tuesday, 27 November 2012 16:23:14 UTC, David Pilato wrote:
> 
> > FYI, I created a full test case here:  
> > [GitHub - elasticsearchfr/elasticsearch-java-tests: Elasticsearch Java Tests](https://github.com/elasticsearchfr/elasticsearch-java-tests)
> > 
> > HTH  
> > David.
> > 
> > Le 27 novembre 2012 à 16:49, David Pilato [da...@pilato.fr](mailto:da...@pilato.fr) a écrit :
> > 
> > Have a look here: [Test case for must clause · GitHub](https://gist.github.com/4154982)
> > 
> > It works fine for me.
> > 
> > David.
> > 
> > Le 27 novembre 2012 à 16:24, mohsin husen [smh...@gmail.com](mailto:smh...@gmail.com) a écrit :
> > 
> > Thanks David for reply 🙂
> > 
> > Let me repeat my use case.
> > 
> > # ==================================================== Case 1 : (which is working as expected)
> > 
> > email : [gmail.com](http://gmail.com)
> > 
> > # query
> > 
> > boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))
> > 
> > # result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)
> > 
> > [a...@gmail.com](mailto:a...@gmail.com)  
> > [x...@gmail.com](mailto:x...@gmail.com)
> > 
> > # ================================================ Case 2 : (now here i have problem ! )
> > 
> > email : [gmail.com](http://gmail.com)  
> > firstName : abc
> > 
> > # Query:
> > 
> > boolQuery()  
> > .should(wildcardQuery("email",  
> > "_"+criteria.getEmail()+"_"))  
> > .should(termQuery("firstName",  
> > criteria.getFirstName()));
> > 
> > # Result
> > 
> > email : [a...@gmail.com](mailto:a...@gmail.com) , Firstname : abc  
> > email : [x...@gmail.com](mailto:x...@gmail.com) , Firstname : xyz
> > 
> > problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as  
> > abc so it should return only one result rather than two results.
> > 
> > David, I also tried must after your suggestion but it is not returning  
> > any hit.
> > 
> > Please advice,  
> > Thanks in advanced  
> > Mohsin H
> > 
> > On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:
> > 
> > Not sure to fully understand but did you try with must clauses instead  
> > of should?
> > 
> > David.
> > 
> > Le 27 novembre 2012 à 15:54, mohsin husen \< [smh...@gmail.com](mailto:smh...@gmail.com)\> a écrit :
> > 
> > I am searching using the JAVA API as follows :
> > 
> > # Test case 1 : (it should return user with \* [gmail.com](http://gmail.com) and have firstname as "abc"
> > 
> > email : " [gmail.com](http://gmail.com)"  
> > firstname : "abc"
> > 
> > boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> > .should(wildcardQuery("firstname",  
> > "_"+criteria.getFirstName()+"_"));
> > 
> > # result :
> > 
> > email : [x...@gmail.com](mailto:x...@gmail.com)  
> > firstname : xyz
> > 
> > email : [a...@gmail.com](mailto:a...@gmail.com)  
> > firstname : abc
> > 
> > here i want only one result(2nd result) that is where both the field is  
> > satisfied.  
> > how to achieve that?
> > 
> > note that user can input in email partial values as " [gmail.com](http://gmail.com)", thats  
> > ahy i am using wildcard there.
> > 
> > Thanks in advanced.
> > 
> > Mohsin
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > --
> > 
> > --  
> > 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:** ![smhdiu](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/smhdiu/32/1808_2.png) [@smhdiu](https://discuss.elastic.co/u/smhdiu)\
**Post date:** [November 27, 2012, 4:39pm UTC](https://discuss.elastic.co/t/search-api-help/9843/8 "2012-11-27T16:39:37Z")

</div>

it still displays the multiple record with should.  
☹

On Tuesday, 27 November 2012 16:31:04 UTC, mohsin husen wrote:

> Thanks a Ton !  
> it is working fine as u said.  
> i may be using other way around.
> 
> now problem is firstname/email may be blank !  
> so wen it comes blank(either of them) no results are populated.  
> i tried with should it is giving result(when one field is blank) but wen  
> we search with firstname and email it has multiple record rather than one  
> single record.
> 
> shall i use other stuffs like filters and all that to achieve this !
> 
> Thanks in advanced  
> Mohsin H
> 
> On Tuesday, 27 November 2012 16:23:14 UTC, David Pilato wrote:
> 
> > FYI, I created a full test case here:  
> > [GitHub - elasticsearchfr/elasticsearch-java-tests: Elasticsearch Java Tests](https://github.com/elasticsearchfr/elasticsearch-java-tests)
> > 
> > HTH  
> > David.
> > 
> > Le 27 novembre 2012 à 16:49, David Pilato [da...@pilato.fr](mailto:da...@pilato.fr) a écrit :
> > 
> > Have a look here: [Test case for must clause · GitHub](https://gist.github.com/4154982)
> > 
> > It works fine for me.
> > 
> > David.
> > 
> > Le 27 novembre 2012 à 16:24, mohsin husen [smh...@gmail.com](mailto:smh...@gmail.com) a écrit :
> > 
> > Thanks David for reply 🙂
> > 
> > Let me repeat my use case.
> > 
> > # ==================================================== Case 1 : (which is working as expected)
> > 
> > email : [gmail.com](http://gmail.com)
> > 
> > # query
> > 
> > boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))
> > 
> > # result : (it gives all emails where we have _[gmail.com](http://gmail.com)_)
> > 
> > [a...@gmail.com](mailto:a...@gmail.com)  
> > [x...@gmail.com](mailto:x...@gmail.com)
> > 
> > # ================================================ Case 2 : (now here i have problem ! )
> > 
> > email : [gmail.com](http://gmail.com)  
> > firstName : abc
> > 
> > # Query:
> > 
> > boolQuery()  
> > .should(wildcardQuery("email",  
> > "_"+criteria.getEmail()+"_"))  
> > .should(termQuery("firstName",  
> > criteria.getFirstName()));
> > 
> > # Result
> > 
> > email : [a...@gmail.com](mailto:a...@gmail.com) , Firstname : abc  
> > email : [x...@gmail.com](mailto:x...@gmail.com) , Firstname : xyz
> > 
> > problem : i am searching a user who has email of [gmail.com](http://gmail.com) and name as  
> > abc so it should return only one result rather than two results.
> > 
> > David, I also tried must after your suggestion but it is not returning  
> > any hit.
> > 
> > Please advice,  
> > Thanks in advanced  
> > Mohsin H
> > 
> > On Tuesday, 27 November 2012 15:02:41 UTC, David Pilato wrote:
> > 
> > Not sure to fully understand but did you try with must clauses instead  
> > of should?
> > 
> > David.
> > 
> > Le 27 novembre 2012 à 15:54, mohsin husen \< [smh...@gmail.com](mailto:smh...@gmail.com)\> a écrit :
> > 
> > I am searching using the JAVA API as follows :
> > 
> > # Test case 1 : (it should return user with \* [gmail.com](http://gmail.com) and have firstname as "abc"
> > 
> > email : " [gmail.com](http://gmail.com)"  
> > firstname : "abc"
> > 
> > boolQuery().should(wildcardQuery("email", "_"+criteria.getEmail()+"_"))  
> > .should(wildcardQuery("firstname",  
> > "_"+criteria.getFirstName()+"_"));
> > 
> > # result :
> > 
> > email : [x...@gmail.com](mailto:x...@gmail.com)  
> > firstname : xyz
> > 
> > email : [a...@gmail.com](mailto:a...@gmail.com)  
> > firstname : abc
> > 
> > here i want only one result(2nd result) that is where both the field is  
> > satisfied.  
> > how to achieve that?
> > 
> > note that user can input in email partial values as " [gmail.com](http://gmail.com)", thats  
> > ahy i am using wildcard there.
> > 
> > Thanks in advanced.
> > 
> > Mohsin
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > --
> > 
> > --  
> > David Pilato  
> > [http://www.scrutmydocs.org/](http://www.scrutmydocs.org/)  
> > [http://dev.david.pilato.fr/](http://dev.david.pilato.fr/)  
> > Twitter : @dadoonet / @elasticsearchfr / @scrutmydocs
> > 
> > --
> > 
> > --  
> > 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:02am UTC](https://discuss.elastic.co/t/search-api-help/9843/9 "2017-07-06T03:02:40Z")

</div>


