# Full Text search in nested and normal Object

**URL:** <https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962>\
**Category:** Elasticsearch\
**Created:** [April 13, 2018, 10:59am UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962 "2018-04-13T10:59:38Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [April 13, 2018, 10:59am UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/1 "2018-04-13T10:59:38Z")

</div>

Hi  
My documents contain the normal and nested object and I need to perform a full-text search on it.  
So How I can perform a full-text search on such documents  
Can any one help me in this?

---

<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:** [April 13, 2018, 11:42am UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/2 "2018-04-13T11:42:07Z")

</div>

Could you provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

---

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [April 13, 2018, 1:16pm UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/4 "2018-04-13T13:16:11Z")

</div>

Hi David,  
I have created the index with the following mapping :

```auto
PUT test
{
  "mappings": {
    "documents": {
      "properties": {
        "title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            }
          }
        },
        "fields": {
          "type": "nested",
          "properties": {
            "uid": {
              "type": "keyword"
            },
            "value": {
              "type": "text"
            }
          }
        }
      }
    }
  }
}

```

I have created following two documents into above index

```auto
PUT test/documents/1
{
  "title": "hello world",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is john"
    },
    {
      "uid": "lname",
      "value": "My last name is test"
    }
  ]
}

PUT test/documents/2
{
  "title": "java demo",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is Ritesh"
    },
    {
      "uid": "lname",
      "value": "My last name is testing"
    }
  ]
}

```

After that I am performing a full-text search by using the following query :

```auto
POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "java"
    }
  }
}

```

Above query gives me a proper response because java is a part of the title. but when I am trying to search text which is part of fields array it is not giving me any response please check following query:

```auto
POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "john"
    }
  }
}

```

So my question is How I can perform a full-text search on the documents which have normal as well as nested type fields. like : the title as normal text and fields as nested type field.  
Can you please help me into this?

---

<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:** [April 13, 2018, 1:42pm UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/5 "2018-04-13T13:42:40Z")

</div>

I edited your post to have a better formatting and make that more readable.

Here is a way to fix that problem:

```auto
DELETE test
PUT test
{
  "mappings": {
    "documents": {
      "properties": {
        "fulltext": {
          "type": "text"
        },
        "title": {
          "type": "text",
          "fields": {
            "raw": {
              "type": "keyword"
            }
          }
        },
        "fields": {
          "type": "nested",
          "properties": {
            "uid": {
              "type": "keyword"
            },
            "value": {
              "type": "text",
              "copy_to": "fulltext"
            }
          }
        }
      }
    }
  }
}

PUT test/documents/1
{
  "title": "hello world",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is john"
    },
    {
      "uid": "lname",
      "value": "My last name is test"
    }
  ]
}

PUT test/documents/2
{
  "title": "java demo",
  "fields": [
    {
      "uid": "fname",
      "value": "My first name is Ritesh"
    },
    {
      "uid": "lname",
      "value": "My last name is testing"
    }
  ]
}
    
    
POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "java"
    }
  }
}

POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "john"
    }
  }
}

```

---

<div class="post-metadata">

**Author:** ![surajdalvi](https://avatars.discourse-cdn.com/v4/letter/s/85f322/32.png) [@surajdalvi](https://discuss.elastic.co/u/surajdalvi)\
**Post date:** [April 18, 2018, 9:11am UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/7 "2018-04-18T09:11:21Z")

</div>

HI David,

Thanks For the reply It is working now.  
Now I am using **highlight** query to highlight matching text.by using following way:

```
POST test/_search
{
  "query": {
    "simple_query_string": {
      "query": "john"
    }
  },
 "highlight" : {
    "fields" : {
        "*" : {}
    }
 }
}

```

Above request giving me following response:

```
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.2876821,
    "hits": [
      {
        "_index": "test",
        "_type": "documents",
        "_id": "1",
        "_score": 0.2876821,
        "_source": {
          "title": "hello world",
          "fields": [
            {
              "uid": "fname",
              "value": "My first name is john"
            },
            {
              "uid": "lname",
              "value": "My last name is test"
            }
          ]
        },
        "highlight": {
          "fields.value": [
            "My first name is <em>john</em>"
          ]
        }
      }
    ]
  }
}

```

But my question is "Can we highlight fields.uid also (in this case **fname** ) ?" by using above query  
It helps me to understand search results belongs to which uid(fields.uid)  
is any solution for this please help me into this?

---

<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:** [April 19, 2018, 1:19pm UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/8 "2018-04-19T13:19:21Z")

</div>

It's another question. As we solved the initial question I think it'd be better to create a new question with a related title about your new problem.

---

<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:** [May 17, 2018, 1:19pm UTC](https://discuss.elastic.co/t/full-text-search-in-nested-and-normal-object/127962/9 "2018-05-17T13:19:23Z")

</div>

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