# Elastic Search - Free Text Search - Nested fields - (Elastic 6.2)

**URL:** https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081
**Category:** Elasticsearch
**Created:** [May 16, 2018, 9:34am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081 "2018-05-16T09:34:06Z")
**Posts on this page:** 13
**Page:** 1

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 16, 2018, 9:34am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/1 "2018-05-16T09:34:06Z")

</div>

GET /companies\_v1/\_search  
{  
"query": {  
"simple\_query\_string": {  
"query": "so-well"  
}  
}  
}  
This works fine.

The same I need to search in Nested objects and in non nested at the same time. My full document contain more than 500 fields, most of them are in nested level. when I search non nested It's working fine, but not for nested fields.

I tried to create a sample .. like below, but no luck.. getting empty result.

{  
"query": {  
"bool": {  
"must": [{  
"nested": {  
"query": {  
"bool": {  
"must": [{  
"simple\_query\_string": {  
"query": "so-well"  
}  
}]  
}  
},  
"path": "companyName"  
}  
}, {  
"nested": {  
"query": {  
"bool": {  
"must": [{  
"simple\_query\_string": {  
"query": "so-well"  
}  
}]  
}  
},  
"path": "companyState"  
}  
}, {  
"simple\_query\_string": {  
"query": "so-well"  
}  
}]  
}  
}  
}  
Let me know if anything need to be add.

## Mapping

PUT /companies\_v1  
{  
"mappings": {  
"companies": {  
"properties": {  
"companyName": {  
"type": "nested"  
},  
"companyState": {  
"type": "nested"  
},  
"companySpecification": {  
"type": "nested"  
},  
"companyOperator": {  
"type": "nested"  
}  
}  
}  
},  
"settings" :{  
"index.mapping.total\_fields.limit": 2000,  
"number\_of\_shards" : 1,  
"number\_of\_replicas" : 0  
}  
}

I was searching for free Text in all my fields, not specifying any particular, and I have more than 500 fields in each document

---

<div class="post-metadata">

### Author: ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)
#### Post date: [May 16, 2018, 9:54am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/2 "2018-05-16T09:54:06Z")

</div>

Which version of Elasticsearch are you using ? Using a `must` in your boolean query makes the match in each nested level mandatory. Is it what you're expecting ? A document should match only if it matches a non-nested field and at least one field in each nested path clause ? I tried you recreation in 6.2 and it works as expected, what happens if you change the `must` to a `should` ?

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 16, 2018, 9:55am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/3 "2018-05-16T09:55:38Z")

</div>

FYI,  
the "so-well" value is matched one of the property in companyName nested object  
in companyName object I have Id, name and city... it matched with name.

GET companies/\_search  
{  
"query": {  
"bool": {  
"must": [{   
"nested": {  
"query": {  
"bool": {  
"must": [{  
"simple\_query\_string": {  
"query": "so-well",  
"fields": ["companyName.name"]  
}  
}]  
}  
},  
"path": "companyName"  
}  
}]  
}  
}  
}  
.. this is working for nested when I give exact match and field name. if I am not specifying field name then it is returning by default 10 records which matched 1 record and rest other 9 records

**But my requirement is to search the input("so-well") in all fields(including nested objects) in my document wherever it matches. for example if it matches in 10 records it should return matched records.**

---

<div class="post-metadata">

### Author: ![jimczi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jimczi/32/47985_2.png) [@jimczi](https://discuss.elastic.co/u/jimczi)
#### Post date: [May 16, 2018, 10:12pm UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/5 "2018-05-16T22:12:21Z")

</div>

Can you provide the version of Elasticsearch that you use in your tests ?  
Can you also post the output of the following command:

```auto
POST companies/_validate/query?explain
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "nested":{  
                  "query":{  
                     "query_string":{  
                        "query":"so-well"
                     }
                  },
                  "path":"companyName"
               }
            }
         ]
      }
   }
}

```

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 17, 2018, 4:49am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/6 "2018-05-17T04:49:53Z")

</div>

## **Version:**

{  
"name" : "",  
"cluster\_name" : "",  
"cluster\_uuid" : "",  
"version" : {  
"number" : "6.2.2",  
"build\_hash" : "10b1edd",  
"build\_date" : "2018-02-28T15:42:08.616107Z",  
"build\_snapshot" : false,  
"lucene\_version" : "7.2.1",  
"minimum\_wire\_compatibility\_version" : "5.6.0",  
"minimum\_index\_compatibility\_version" : "5.0.0"  
},  
"tagline" : "You Know, for Search"  
}

## **Output of the above Query**

Document is too big Just I am pasting only few which ever is required

{  
"valid": true,  
"\_shards": {  
"total": 1,  
"successful": 1,  
"failed": 0  
},  
"explanations": [  
{  
"index": "companies",  
"valid": true,  
"explanation": """+ToParentBlockJoinQuery (+  
companyName.companyName:so-well  
}]}

**for few fields by default it is appending .keyword**

**for date fields below**  
MatchNoDocsQuery("failed [county.effectiveDate] query, caused by number\_format\_exception:[For input string: "so-well"]")

---

<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: [May 17, 2018, 8:58am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/7 "2018-05-17T08:58:06Z")

</div>

> we are using aws managed Elasticsearch

Not related to your question but did you look at [cloud.elastic.co](http://cloud.elastic.co) and [AWS Marketplace: Elastic Cloud (Elasticsearch Service)](https://aws.amazon.com/marketplace/pp/B01N6YCISK) ?

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 17, 2018, 10:25am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/8 "2018-05-17T10:25:11Z")

</div>

Same I am testing with local elastic search, getting same error

---

<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: [May 17, 2018, 11:40am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/9 "2018-05-17T11:40:43Z")

</div>

Yeah. As i said it's unrelated to your question. I was just saying that if you are looking for an hosted elasticsearch as a service solution you should checkout this page: [https://www.elastic.co/cloud](https://www.elastic.co/cloud)

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 18, 2018, 5:34am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/10 "2018-05-18T05:34:14Z")

</div>

Sorry, I didn't read your post properly,  
Yeah, We are using elastic cloud(14 days trial version) and aws elastic cloud to evaluate.  
Later we will take a call on which to go.

---

<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: [May 18, 2018, 8:12am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/11 "2018-05-18T08:12:02Z")

</div>

Great that you are aware of it!

(To me the choice is pretty obvious as cloud by elastic is the only way to have access to X-Pack. Think about what is there like Security, Monitoring, Reporting and what is coming like Canvas, SQL... But I might be biased though 😄 )

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 19, 2018, 4:27pm UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/12 "2018-05-19T16:27:38Z")

</div>

Hi,  
Below is working for me....  
is this fine to get nested and non-nested values with same query, if this is correct, what about performance (500 fields), is there any issue while searching more than 2 million records.

Or any other way to get, if yes, could you please provide sample mapping and query for nested or non nested

PUT sarath\_test?pretty  
{  
"mappings": {  
"sarath": {  
"properties": {  
"all\_fields": {  
"type": "keyword"  
},  
"xyz": {  
"type": "keyword"  
},  
"test": {  
"type": "nested",  
"properties": {  
"abc": {  
"type": "keyword",  
"copy\_to": "all\_fields"  
}  
}  
}  
}  
}  
}  
}

--

PUT sarath\_test/sarath/1  
{  
"xyz": "John",  
"test":[  
{  
"abc": "Smith"  
}  
]  
}

* * *

GET sarath\_test/\_search  
{  
"query": {  
"simple\_query\_string": {  
"query": "Smith"  
}  
}  
}

GET sarath\_test/\_search  
{  
"query": {  
"simple\_query\_string": {  
"query": "John"  
}  
}  
}

---

<div class="post-metadata">

### Author: ![Sarathmskr](https://avatars.discourse-cdn.com/v4/letter/s/a87d85/32.png) [@Sarathmskr](https://discuss.elastic.co/u/Sarathmskr)
#### Post date: [May 24, 2018, 10:23am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/13 "2018-05-24T10:23:42Z")

</div>

Hi,  
Could you please update

---

<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: [June 21, 2018, 10:23am UTC](https://discuss.elastic.co/t/elastic-search-free-text-search-nested-fields-elastic-6-2/132081/14 "2018-06-21T10:23:50Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
