# How to search on multiple fields?

**URL:** <https://discuss.elastic.co/t/how-to-search-on-multiple-fields/8724>\
**Category:** Elasticsearch\
**Created:** [August 12, 2012, 6:05pm UTC](https://discuss.elastic.co/t/how-to-search-on-multiple-fields/8724 "2012-08-12T18:05:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Navvi](https://avatars.discourse-cdn.com/v4/letter/n/13edae/32.png) [@Navvi](https://discuss.elastic.co/u/Navvi)\
**Post date:** [August 12, 2012, 6:05pm UTC](https://discuss.elastic.co/t/how-to-search-on-multiple-fields/8724/1 "2012-08-12T18:05:05Z")

</div>

We have indexed all the records from our table to ES.  
After that we are searching for some fields on indexed records.

Here is the code snippet for indexing....

for (final Contact contact : contacts) {  
final XContentBuilder builder =  
XContentFactory.jsonBuilder()  
.startObject();  
builder.field("CONTACT\_ID", contact.getId());  
final Map\<String, Object\> attributes =  
contact.getAttributes();  
for (final String fieldName : attributes.keySet()) {  
builder.field(fieldName,  
attributes.get(fieldName));  
}  
builder.endObject();  
final IndexRequestBuilder prepareIndex =  
client.prepareIndex(  
indexName, type,  
StringUtils.lowerCase(String.valueOf(contact.getId())));  
prepareIndex.setSource(builder);  
bulkRequest.add(prepareIndex);  
}

BulkResponse bulkResponse = bulkRequest.execute().actionGet();

While indexing we haven't used any analyzers. What is the default analyzer  
it uses if we don't specify any thing ?  
If we wanted to use which is best Analyzer to use , I mean Keyword or  
standard and how to use that analyzer using java api while indexing ?

Here is the code snippet to search for multiple fields on ES data...

we will get a list of search fields from upper layer to ES layer

## SearchAttribute { private String attributeName; private String attributeValue;

```
setters & getters

```

* * *

}

from list of searchAttributes we are building query like...

for(SearchAttributeDo searchAttribute : searchAttributes){  
boolQueryBuilder.should(QueryBuilders.termQuery(searchAttribute.getAttributeName(),  
searchAttribute.getAttributeValue()));  
}

SearchRequestBuilder srb = client.prepareSearch()  
.setTypes(repoName)  
.setQuery(boolQueryBuilder);  
SearchResponse searchResponse = srb.execute().actionGet();

One strange thing we observed was when we give the attribute value in lower  
case then only it returning results, otherwise it is not returning results.  
For example if the "Lawrence" is indexed as FIELD\_NAME but while querying  
if we give "Lawrence" we are not getting any results. But when we give as  
"lawrence" we are getting response back.

I think that might be because lucene is internally converting everything in  
to lower case while indexing.

Please can you suggest us in following things...

1. what is best analyzers to use while indexing and searching ...  
we have values to be indexed and searched like... TELE\_PHONE  
--\>"510/782-1117" & EMAIL -- \>"elasticquery@help.com"

2. Is the way which we followed to search on multiple fields with each  
field with it's own value ? Or could you suggest any other better way ? we  
used bool query for that by iterating through list of search attribute  
objects.

Thanks,  
Navvi.

--

---

<div class="post-metadata">

**Author:** ![Runar\_Myklebust\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/runar_myklebust_2/32/2047_2.png) [@Runar\_Myklebust\_2](https://discuss.elastic.co/u/Runar_Myklebust_2)\
**Post date:** [August 14, 2012, 7:42am UTC](https://discuss.elastic.co/t/how-to-search-on-multiple-fields/8724/2 "2012-08-14T07:42:43Z")

</div>

On Sun, Aug 12, 2012 at 8:05 PM, Navvi [vnaveen@prokarmasoftech.com](mailto:vnaveen@prokarmasoftech.com) wrote:

> While indexing we haven't used any analyzers. What is the default analyzer  
> it uses if we don't specify any thing ?

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

> If we wanted to use which is best Analyzer to use , I mean Keyword or  
> standard and how to use that analyzer using java api while indexing ?

You can set analyzer when creating index, e.g:

CreateIndexRequest createIndexRequest = new CreateIndexRequest( indexName );  
createIndexRequest.settings(  
indexSettingBuilder.buildIndexSettings() );  
client.admin().indices().create( createIndexRequest ).actionGet();

```
public Settings buildIndexSettings()
{
    final ImmutableSettings.Builder settings =

```

ImmutableSettings.settingsBuilder();  
.....  
settings.loadFromSource( buildAnalyzerSettings() );

```
    return settings.build();
}

```

private String buildAnalyzerSettings()  
{  
try  
{  
return jsonBuilder()  
.startObject()  
.startObject( "analysis" )  
.startObject( "analyzer" )  
.startObject("whitespace\_analyzer" )  
.field( "type", "custom" )  
.field( "tokenizer", "whitespace" )  
.field( "filter", new String{"lowercase"}  
)  
.endObject().endObject()  
.endObject()  
.endObject()  
.string();  
}  
catch ( IOException e )  
{  
throw new ContentIndexException( "Not able to create analyzer  
settings for index", e );  
}

```
}

```

You can also apply directly in your mapping document.

> Here is the code snippet to search for multiple fields on ES data...
> 
> we will get a list of search fields from upper layer to ES layer
> 
> ## SearchAttribute { private String attributeName; private String attributeValue;
> 
> ```
> setters & getters
> 
> ```
> 
> * * *
> 
> }
> 
> from list of searchAttributes we are building query like...
> 
> for(SearchAttributeDo searchAttribute : searchAttributes){  
> boolQueryBuilder.should(\*\*QueryBuilders.termQuery(\*_searchAttribute._  
> \*getAttributeName(), searchAttribute.\*\*getAttributeValue()));  
> }
> 
> SearchRequestBuilder srb = client.prepareSearch()  
> .setTypes(repoName)  
> .setQuery(boolQueryBuilder);  
> SearchResponse searchResponse = srb.execute().actionGet();
> 
> One strange thing we observed was when we give the attribute value in  
> lower case then only it returning results, otherwise it is not returning  
> results. For example if the "Lawrence" is indexed as FIELD\_NAME but while  
> querying if we give "Lawrence" we are not getting any results. But when we  
> give as "lawrence" we are getting response back.

The default tokenizer is using lower case.

> I think that might be because lucene is internally converting everything  
> in to lower case while indexing.
> 
> Please can you suggest us in following things...
> 
> 1. what is best analyzers to use while indexing and searching ...  
> we have values to be indexed and searched like... TELE\_PHONE --\>"  
> 510/782-1117" & EMAIL -- \>"[elasticquery@help.com](mailto:elasticquery@help.com)"

Search and read the documentation, there are a lot of different  
alternatives depending on your particular need:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

> 1. Is the way which we followed to search on multiple fields with each  
> field with it's own value ? Or could you suggest any other better way ? we  
> used bool query for that by iterating through list of search attribute  
> objects.

I would certainly drop the bool query for cases with just one search value,  
but thats just a minor detail.

## greetings

Runar Myklebust  
Enonic AS  
Senior Developer

[http://enonic.com/download](http://enonic.com/download)

--

---

<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:16am UTC](https://discuss.elastic.co/t/how-to-search-on-multiple-fields/8724/3 "2017-07-06T03:16:28Z")

</div>


