Exact search results not shown at the top

I am using Elasticsearch version 5.2. When I try to query a combination of two words that are stored in two different attributes, I get the search result but the record with the exact search words are not displayed at the top.
For example I have two names saved in ES
Document 1: "firstName" : "Joe"
"lastName" : "Root"
Document 2: "firstName" : "Joe"
"lastName" : "Martin"
If my search word is "Joe Root", I get "Joe Martin" as the first search result and "Joe Root" as the second search result. But I want "Joe Root" to be the first one.
I tried with the combination of bool and term query then with multi match query. I also tried by boosting the score of the field. But nothing worked.
Can I get assistance on how to resolve the issue?

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.

Also make sure in your tests that you are using one single shard instead of the default 5 ones.

I tested my sample in only one shard but it didn't work.
Here is my script

**Mapping:**
{
  "users": {
"dynamic": "false",
"_all": {
  "enabled": false
},
"properties": {
  "creationDate": {
    "type": "date",
    "format": "yyyy-MM-dd HH:mm:ss"
  },
  "firstName": {
    "type": "string",
    "fields": {
      "indexed": {
        "type": "string"
      },
      "sortable": {
        "type": "string"
      },
      "untouched": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  },
  "lastName": {
    "type": "string",
    "fields": {
      "indexed": {
        "type": "string"
      },
      "sortable": {
        "type": "string"
      },
      "untouched": {
        "type": "string",
        "index": "not_analyzed"
      }
    }
  }
}
  }
}

**Search Query:**
GET /users/users/_search
{
"query": {
    "multi_match" : {
      "query":    "Joe Martin", 
      "fields": [ "firstName", "lastName" ] 
    }
}
}

**Search Results:**
{
  "took": 6,
  "timed_out": false,
  "_shards": {
"total": 1,
"successful": 1,
"failed": 0
  },
  "hits": {
"total": 3,
"max_score": 0.6931472,
"hits": [
  {
    "_index": "users",
    "_type": "users",
    "_id": "1",
    "_score": 0.6931472,
    "_source": {
      "firstName": "Joe",
      "lastName": "Root"
    }
  },
  {
    "_index": "users",
    "_type": "users",
    "_id": "2",
    "_score": 0.6931472,
    "_source": {
      "firstName": "Joe",
      "lastName": "Martin"
    }
  },
  {
    "_index": "users",
    "_type": "users",
    "_id": "3",
    "_score": 0.6931472,
    "_source": {
      "firstName": "Anna",
      "lastName": "Martin"
    }
  }
]
  }
}

My search text "Joe Martin" should be my first record but I get it as the second result.

As per the docs, the default matching behaviour assumes you are looking for all terms in a single field:

"Finds documents which match any field, but uses the _score from the best field"

So the match on the lastname in this case is worthless.
Searches like your example tend to work best with the "cross-fields" type (although in this particular case you have picked a lastname "Martin" that is likely to be a popular firstname so will be lowly scored)

1 Like

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