1
votes

Having noticed my sort on an indexed string field doesn't work properly, I've discovered that it's sorting analyzed strings so "bags of words" and if I want it to work properly I have to sort on the non-analyzed string. My plan was to just change the string field to a multi-field, using information I found in those two articles:

https://www.elastic.co/blog/changing-mapping-with-zero-downtime (Upgrade to a multi-field part) https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-put-mapping.html

Using Sense I've created this field mapping

PUT myindex/_mapping/type
{
   "properties": {
      "Title": {
         "type": "string",
         "fields": {
            "Raw": {
               "type": "string",
               "index": "not_analyzed"
            }
         }
      }
   }
}

And then I try to sort my search results using the newly made field. I've put all of the name variations I could think of after reading the articles:

POST myindex/_search
{
    "_source" : ["Title","titlemap.Raw","titlemap.Title","titlemap.Title.Raw","Title.Title","Raw","Title.Raw"],
    "size": 6,
    "query": {
        "multi_match": {
            "query": "title",
            "fields": ["Title^5"
            ],
            "fuzziness": "auto",
            "type": "best_fields"
        }
    },
    "sort": {
        "Title.Raw": "asc"
    }
}

And that's what I get in response:

{
    "_index": "myindex_2015_11_26_12_22_38",
    "_type": "type",
    "_id": "1205",
    "_score": null,
    "_source": {
       "Title": "The title of the item"
    },
    "sort": [
       null
    ]
 }

Only the Title field's value is shown in the response and the sort criterium is null for every result.

Am I doing something wrong, or is there another way to do that?

1
Yes, you simply need to reindex your data, since the new Raw subfield is not populated yet. - Val
@Val, I've tried that, but now my mapping is gone. The problem probably lies in the fact that myindex_2015_11_26_12_22_38 was the name of the index, while myindex is the alias. - Slowacki
You seem to have updated the titlemap type (see the URL myindex/_mapping/titlemap), but judging by the results you get the real type of your documents is type (see the _type field), so probably you haven't updated the right mapping. Your index/alias is myindex, your mapping type is type, your field you want to upgrade to a multi-field is Title. I don't know what titlemap is. - Val
My bad, I've changed the url to myindex/_mapping/type, however, this still doesn't help. When reindexing, I create a new index (myindex_current_date) and then assign the myindex alias to it, unassigning the alias from the last index. "reason": "No mapping found for [Title.Raw] in order to sort on" - Slowacki
After creating the new index and the mapping, you can reindex. No reason the mapping would be gone after reindexing. - Val

1 Answers

1
votes

The index name is not the same after re-indexing and thus the default mapping gets installed... that's probably why.

I suggest using an index template instead, so you don't have to care when to create the index and ES will do it for you. The idea is to create a template with the proper mapping you need and then ES will create every new index whenever it deems necessary, add the myindex alias and apply the proper mapping to it.

curl -XPUT localhost:9200/_template/myindex_template -d '{
  "template": "myindex_*",
  "settings": {
    "number_of_shards": 1
  },
  "aliases": {
     "myindex": {}
  },
  "mappings": {
    "type": {
      "properties": {
        "Title": {
          "type": "string",
          "fields": {
            "Raw": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      }
    }
  }
}'

Then whenever you launch your re-indexing process a new index with a new name will be created BUT with the proper mapping and the proper alias.