No mapping found for field in order to sort on in ElasticSearch

后端 未结 9 1527
天命终不由人
天命终不由人 2020-12-12 18:59

Elasticsearch throws a SearchParseException while parsing query if there are some documents found not containing field used in sort criteria.

相关标签:
9条回答
  • 2020-12-12 19:04

    Elasticsearch 6.4

    simply specify the index and that's it in Kibana

    BEFORE

    GET /_search
    {
     
      "query": {
        "exists": {
          "field": "document_id"
        }
      },
      "sort": [
        {
          "document_id": { "order": "asc"  },
          "created_at":  { "order": "desc" }
        }
      ]
    }

    AFTER

    GET /document-index/contact/_search  (here)
    {
    
      "query": {
        "exists": {
          "field": "document_id"
        }
      },
      "sort": [
        {
          "document_id": { "order": "asc"  },
          "created_at":  { "order": "desc" }
        }
      ]
    }
    
    0 讨论(0)
  • 2020-12-12 19:09

    I experienced the same problem (sorta; would get some errors, but some results), but in my case my search was being issued at the root (no index specified), and the errors I was getting were because the search/order was also looking to a Kibana index.

    Stupid error, but maybe this'll help someone else who ends up here.

    0 讨论(0)
  • 2020-12-12 19:12

    You could also use script which gives you some flexibility:

    "sort" : {
        "_script" : {
            "type" : "number",
            "script" : {
                "lang": "painless",
                "source": "return !doc['price'].empty ? doc['price'].value : 0"
            },
            "order" : "desc"
        }
    }
    
    0 讨论(0)
  • 2020-12-12 19:15

    After digging more, I found the solution as given below. ignore_unmapped should be explicitly set to true in the sort clause.

    "sort" : [
           { "rating": {"order" : "desc" , "ignore_unmapped" : true} },
           { "price": {"order" : "asc" , "missing" : "_last" , "ignore_unmapped" : true} }
    ]
    

    For further information have a look at the Elasticsearch references for:

    • missing values
    • ignoring unmapped fields
    0 讨论(0)
  • 2020-12-12 19:19

    For those looking for an example of both ignore_unmapped and unmapped_type please see my response here.

    Note that "ignore_unmapped" is now deprecated in favor of "unmapped_type". This was done as part of #7039

    From documentation: Before 1.4.0 there was the ignore_unmapped boolean parameter, which was not enough information to decide on the sort values to emit, and didn’t work for cross-index search. It is still supported but users are encouraged to migrate to the new unmapped_type instead.

    By default, the search request will fail if there is no mapping associated with a field. The unmapped_type option allows to ignore fields that have no mapping and not sort by them. The value of this parameter is used to determine what sort values to emit. Here is an example of how it can be used:

    {
        "sort" : [
            { "price" : {"unmapped_type" : "long"} },
        ],
        "query" : {
            "term" : { "user" : "kimchy" }
        }
    }
    

    If any of the indices that are queried doesn’t have a mapping for price then Elasticsearch will handle it as if there was a mapping of type long, with all documents in this index having no value for this field.

    0 讨论(0)
  • 2020-12-12 19:23

    if you are using es 6.7

    try this one

    sort : ["title.keyword:desc"]
    
    0 讨论(0)
提交回复
热议问题