0

I'm trying make a search and sort the results. However, I'm getting a error dont know why.

EDIT - I'll provide my full mappings.

 "myindex": {
      "mappings": {
         "mytype": {
            "dynamic_templates": [
               {
                  // Dynamic templates here!
               }
            ],
            "properties": {
               "fieldid": {
                  "type": "keyword",
                  "store": true
               },
               "fields": {
                  "properties": {
                     "myfield": {
                        "type": "text",
                        "fields": {
                           "sort": {
                              "type": "keyword",
                              "ignore_above": 256
                           }
                        },
                        "analyzer": "myanalyzer"
                     }                     
                  }
               },
               "isDirty": {
                  "type": "boolean"
               }
            }
         }
      }
   }
}

When I performed a search with sorting, like this:

POST /index/_search
{

  "sort": [
    { "myfield.sort" : {"order" : "asc"}}
  ]
} 

I get the following error:

{
   "error": {
      "root_cause": [
         {
            "type": "query_shard_exception",
            "reason": "No mapping found for [myfield.sort] in order to sort on",
            "index_uuid": "VxyKnppiRJCrrnXfaGAEfA",
            "index": "index"
         }
      ]
   "status": 400
}

I'm following the documentation on elasticsearch. DOCUMENTATION

I also check this link: DOCUMENTATION

Can someone provided me help?

NatsuDragonEye
  • 109
  • 5
  • 18

1 Answers1

1

Hmm it might be your mapping isn't set properly. I followed along using the following:

PUT /your-index/
{
    "settings": {
        "number_of_replicas": "1",
        "number_of_shards": "3",

        "analysis": {
            "customanalyzer": {
                "ID": {
                    "type": "custom",
                    "tokenizer": "keyword",
                    "filter": ["lowercase"]
                }
            }
        }
    },
    "mappings": {
        "thingy": {
            "properties": {
                "myfield": {
                    "type": "text",
                    "analyzer": "ID",

                     "fields": {
                        "sort": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        }
    }
}

To double check if the index actually has myfield.sort field look at

GET /your-index/thingy/_mapping

Then, upload some document (no need to specify the sub-field(s), elasticsearch will do it for you)

POST /your-index/thingy/
{
    "myfield": "some-value"
}

Now I can search with the following:

POST /your-index/thingy/_search
{
    "sort": [
        { "myfield.sort": { "order": "asc" } } 
    ]
}

So be sure to check:

  • Naming/typo's (you never know)
  • Your mapping (does it have the "myfield.sort" field)
  • Are you searching in the correct index?

Hopefully this helps

Tessmore
  • 1,054
  • 1
  • 9
  • 23
  • I'd call it a `multi_field` or sub field, rather than a _nested field_, as that means something different already :) – Russ Cam Jul 27 '17 at 22:37
  • Good point, I changed it a bit, but multi_fields seem to be ... https://www.elastic.co/guide/en/elasticsearch/reference/1.4/_multi_fields.html gone? :D – Tessmore Jul 27 '17 at 22:50
  • `multi_field` _types_ are gone i.e. no longer need to specify `"type": "multi_field"`, but the principle of being able to analyze and index a string in a number of different ways is still there. People still refer to this as multi fields or sub fields (is there a better name?) :) – Russ Cam Jul 28 '17 at 01:33
  • I think that the problem is not the mappings. I edit the answer and provide my full mappings. If you want i can provide my settings too. I dont see any problem on them. – NatsuDragonEye Jul 28 '17 at 08:32
  • Well, I figure it out the problem. As you can see, my mappings have my "sort" field inside a property called "fields". So I need to do some like this: POST /myindex/mytype/_search { "sort": [ { "fields.myfield.sort": { "order": "asc" } } ] } – NatsuDragonEye Jul 28 '17 at 08:49
  • Glad it helped! :) – Tessmore Jul 28 '17 at 09:54