# Search on fields with Multi field mapping and Ngram Analyzer

**URL:** https://discuss.elastic.co/t/search-on-fields-with-multi-field-mapping-and-ngram-analyzer/14709
**Category:** Elasticsearch
**Created:** [December 4, 2013, 5:02pm UTC](https://discuss.elastic.co/t/search-on-fields-with-multi-field-mapping-and-ngram-analyzer/14709 "2013-12-04T17:02:14Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![sunshine](https://avatars.discourse-cdn.com/v4/letter/s/3ec8ea/32.png) [@sunshine](https://discuss.elastic.co/u/sunshine)
#### Post date: [December 4, 2013, 5:02pm UTC](https://discuss.elastic.co/t/search-on-fields-with-multi-field-mapping-and-ngram-analyzer/14709/1 "2013-12-04T17:02:14Z")

</div>

Multi field Mapping with Analyzer

{  
"settings" : {  
"analysis" : {  
"analyzer" : {  
"my\_ngram\_analyzer" : {  
"tokenizer" : "my\_ngram\_tokenizer",  
"filter":"lowercase"  
}  
},  
"tokenizer" : {  
"my\_ngram\_tokenizer" : {  
"type" : "nGram",  
"min\_gram" : "3",  
"max\_gram" : "50"  
}  
}  
}  
},  
"mappings": {  
"allpy": {  
"properties": {  
"id": {  
"type": long  
}  
"FIRSTNAME": {  
"path": "just\_name"  
"type": "multi\_field"  
"fields": {  
"FIRSTNAME": {  
"store": true  
"type": "string",  
"analyzer": "my\_ngram\_analyzer"  
}  
"COMBOFIELDS": {  
"include\_in\_all": false  
"store": true  
"type": "string",  
"analyzer": "my\_ngram\_analyzer"  
}  
}  
}  
"middlename": {  
"type": "string"  
}  
"LASTNAME": {  
"path": "just\_name"  
"type": "multi\_field"  
"fields": {  
"COMBOFIELDS": {  
"include\_in\_all": false  
"store": true  
"type": "string",  
"analyzer": "my\_ngram\_analyzer"  
}  
"LASTNAME": {  
"store": true  
"type": "string",  
"analyzer": "my\_ngram\_analyzer"  
}  
}  
}  
}  
}  
}  
}

Data indexed

{"id":1,"FIRSTNAME":"John","middlename":"clark","LASTNAME":"Doe"}  
{"id":2,"FIRSTNAME":"John","middlename":"clark","LASTNAME":"Dave"}  
{"id":3,"FIRSTNAME":"Jane","middlename":"clark","LASTNAME":"Dave"}

Query with query string gives expected results i.e. id = 2 for query below

Query 1  
{  
"query" : {  
"query\_string" : {  
"fields" : [  
"FIRSTNAME",  
"LASTNAME"  
],  
"query" : "john dave",  
"default\_operator" : "AND"  
}  
}  
}

But the same does not work with multi match

Query 2  
{  
"query" : {  
"filtered" : {  
"query" : {  
"multi\_match" : {  
"query" : "sangita saxena",  
"fields" : ["COMBOFIELDS"],  
"operator" : "OR",  
"minimum\_should\_match" : "100%"

```
    }
  }
}

```

}  
}

If I remove the analyzer it does give me the expected result

Explanation for Query 1  
explanation: +((+FIRSTNAME:joh +FIRSTNAME:john +FIRSTNAME:ohn) | (+LASTNAME:joh +LASTNAME:john +LASTNAME:ohn)) +((+FIRSTNAME:dav +FIRSTNAME:dave +FIRSTNAME:ave) | (+LASTNAME:dav +LASTNAME:dave +LASTNAME:ave))

Explanation for Query 2  
explanation: (COMBOFIELDS:joh COMBOFIELDS:john COMBOFIELDS:john COMBOFIELDS:john d COMBOFIELDS:john da COMBOFIELDS:john dav COMBOFIELDS:john dave COMBOFIELDS:ohn COMBOFIELDS:ohn COMBOFIELDS:ohn d COMBOFIELDS:ohn da COMBOFIELDS:ohn dav COMBOFIELDS:ohn dave COMBOFIELDS:hn COMBOFIELDS:hn d COMBOFIELDS:hn da COMBOFIELDS:hn dav COMBOFIELDS:hn dave COMBOFIELDS:n d COMBOFIELDS:n da COMBOFIELDS:n dav COMBOFIELDS:n dave COMBOFIELDS: da COMBOFIELDS: dav COMBOFIELDS: dave COMBOFIELDS:dav COMBOFIELDS:dave COMBOFIELDS:ave)~28

Whats the best way to design this query to get the expected results ?  
Either by using individual field names "fields" : ["FIRSTNAME", "LASTNAME"] or  
by using the multi-field name "fields" : ["COMBOFIELDS"]

Our requirement is not just for full words. i.e. search string "john dave" should work same as "joh dave" or "john dav"

Thanks much

---

<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, 2:03am UTC](https://discuss.elastic.co/t/search-on-fields-with-multi-field-mapping-and-ngram-analyzer/14709/2 "2017-07-06T02:03:16Z")

</div>


