Full Text search in nested and normal Object


(Suraj) #1

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?


(David Pilato) #2

Could you provide a full recreation script as described in About the Elasticsearch category. 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.


(Suraj) #4

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

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

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 :

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:

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?


(David Pilato) #5

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

Here is a way to fix that problem:

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"
    }
  }
}

(Suraj) #7

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?


(David Pilato) #8

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.


(system) closed #9

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