Elasticsearch – sorting on string types with more than one value per doc, or more than one token per field

We were experiencing problems with sorting results with a recent version of elasticsearch.

Typical sort on name field (consisting of both first and last names) was coming back with exception:

“Can’t sort on string types with more than one value per doc, or more than one token per field”

Other users also experienced the problem:

http://elasticsearch-users.115913.n3.nabble.com/Sorting-failing-in-latest-master-td967979.html

The solution was to add multi type mapping on the field. Initially the mapping was plain string:

curl -s -XGET 'es:9200/users/journalist/_mapping' 

{ 
    "journalist": {
        "properties": {
            "name": {
                "type": "string"
            } 
        }
    }
} 

and the search queries like below , were failing.

curl -s -XGET es:9200/users/journalist/_search -d '
{
    "sort": [ { "name" : "asc" } ], 
    "fields": ["name"],
    "query" : {
        "query_string": {
            "query": "harry",
            "fields": [ "name" ] 
        }
    }
}'

Following the documentation I’ve replaced the mapping on “name” field to be a composite of standard string type and not analyzed version of it:



curl -s -XPUT es:9200/users/journalist/_mapping -d '
{ 
    "journalist" :  { 
        "properties": {
            "name" :  {
                "type": "multi_field", 
                    "fields" : { 
                        "name": { 
                            "type" : "string" 
                        },
                        "untouched" : {
                            "type": "string",
                            "index" : "not_analyzed" 
                        }
                    }  
            }
        }
    }
}'

To verify:

curl -s -XGET 'es:9200/users/journalist/_mapping' 
{
    "journalist": {
            "name": {
                "fields": {
                    "name": {
                        "type": "string"
                    }, 
                    "untouched": {
                        "include_in_all": false, 
                        "index": "not_analyzed", 
                        "type": "string"
                    }
                }, 
                "type": "multi_field"
            }
        }
    }
}

After reindexing and changing the sort query (note the “name.untouched” part), the search went fine.

curl -s -XGET es:9200/users/journalist/_search -d '
{
    "sort": [ { "name.untouched" : "asc" } ], 
        "fields": ["name"],
        "query" : {
            "query_string": {
                "query": "harry",
                "fields": [ "name" ] 
            }
        }
}'
Share
    • Paul Simmonds
    • September 12th, 2012 10:54am

    Worked great, thanks!

  1. That worked for me. Many Thanks for this great example.

  1. No trackbacks yet.