ASN
ASN

Reputation: 1863

Word matching in ElasticSearch

This is how I'm mapping my index.

 settings = new ConnectionSettings(pool)
              .DefaultIndex(defaultIndex)
              .MapDefaultTypeNames(m => m.Add(typeof(MyClass), "content"))
              .PrettyJson()
              .DisableDirectStreaming());

Lets say I have a document indexed to ES with

content: "Tim Cook revealed during the earnings call that iPhone sales in India grew by 56% on a yearly basis, despite the company's first global sales decline in 13 years."

Now if a user searches for a word using a match query

{
  "query": {
    "match": {
      "answer": {
        "query": "decline"
      }
    }
}}

say i get a score of 0.047.

But with the same query, if I search for the word "declining", I get a score of 0. I want to check if the word is partially present in the document or not. How can I do this?

Upvotes: 0

Views: 77

Answers (1)

Val
Val

Reputation: 217274

You need to define your field mapping by declaring a specific analyzer to analyze your content field. In this case, we could use the english language analyzer which will stem the tokens according to english grammatic and lexical rules.

PUT your_index
{
  "mappings": {
    "your_type": {
      "properties": {
        "content": {
          "type": "string",
          "analyzer": "english"
        }
      }
    }
  }
}

Then you can index your content

PUT /your_index/your_type/1
{
  "content": "Tim Cook revealed during the earnings call that iPhone sales in India grew by 56% on a yearly basis, despite the company's first global sales decline in 13 years."
}

And finally you can search both decline and declining and get the same score

POST /your_index/_search 
{
  "query": {
    "match": {
      "content": {
        "query": "decline"
      }
    }
  }
}

POST /your_index/_search 
{
  "query": {
    "match": {
      "content": {
        "query": "declining"
      }
    }
  }
}

Upvotes: 1

Related Questions