Dmitry
Dmitry

Reputation: 281

Elasticsearch doesn't give output I expect

My search query is:

query: {
  match: {
    name: "le sul"
  }
},

I expect to see the output as:

.-------------------------------------------------.
|  ID   |            Name             |   Score   |
|-------|-----------------------------|-----------|
| 9     | le sultan                   | ...       |
| 467   | le sultan                   | ...       |
| 23742 | LE DUONG                    | 1.1602057 |
| 11767 | LE VICTORIA                 | 0.9554229 |
| 11758 | LE CANONNIER                | 0.9554229 |
| 23762 | PHA LE XANH                 | 0.9281646 |
| 15795 | LE SURCOUF HOTEL & SPA      | 0.9281646 |
| 33066 | LE CORAL HIDEAWAY BEYOND    | 0.8695703 |
| 11761 | LE MERIDIEN MAURITIUS       | 0.8682439 |
| 11871 | LE RELAX HOTEL & RESTAURANT | 0.8682439 |
'-------------------------------------------------'

But what I see is:

.-------------------------------------------------.
|  ID   |            Name             |   Score   |
|-------|-----------------------------|-----------|
| 23742 | LE DUONG                    | 1.1602057 |
| 9     | le sultan                   | 1.0869629 | <----
| 11767 | LE VICTORIA                 | 0.9554229 |
| 11758 | LE CANONNIER                | 0.9554229 |
| 467   | le sultan                   | 0.9554229 | <----
| 23762 | PHA LE XANH                 | 0.9281646 |
| 15795 | LE SURCOUF HOTEL & SPA      | 0.9281646 |
| 33066 | LE CORAL HIDEAWAY BEYOND    | 0.8695703 |
| 11761 | LE MERIDIEN MAURITIUS       | 0.8682439 |
| 11871 | LE RELAX HOTEL & RESTAURANT | 0.8682439 |
'-------------------------------------------------'

As you can see, "le sultan" not the first element of the result set.

Where am I going wrong?

Upvotes: 0

Views: 53

Answers (2)

Bharat
Bharat

Reputation: 162

Your query result is not match because elasticsearch search via _score.

In your case you want to search in an analyzed search and get result in not analyzed.

So you should put your mapping like given  below 


Put your_index_name
{
   "mappings": {
      "your_type_name": {
           "properties": {
         "name": {
            "type": "string",
            "analyzer": "english",
            "fields": {
               "your_temporary_sort_filed_name": {
                  "type": "string",
                  "index": "not_analyzed"
               }
            }
         }
      }
    }
   }
}

And then

GET /your_index_name/your_type_name/_search
{
  "sort": [
     {
        "name.your_temporary_sort_filed_name":{
            "order": "desc"
        }
     }
  ], 
    "query": {
        "match": {
           "name": "le sul"
        }
    }
}

Upvotes: 2

Richa
Richa

Reputation: 7649

If you want to get le sultan as use following query:

{

"query": {
  "query_string": {
     "default_field": "name",
      "query": "le sul*"
     }
   }
}

Upvotes: 1

Related Questions