简体   繁体   中英

ElasticSearch sort array size incoherent results

I am trying to sort by array size in ElasticSearch 7.1.

I indexed the following data without creating any custom mapping:

{
  "myarray": [{
    "field": {
      "value": "test"
    }
  }]
}

When I look at the mapping, it is giving me:

{
  "properties": {
    "myarray": {
      "properties": {
        "field": {
          "properties": {
            "value": {
              "type": "text",
              "fields": {
                "keyword": {
                  "type": "keyword",
                  "ignore_above": 256
                }
              }
            }
          }
        }
      }
    }
  }
}

Now I want to query the index and sort by the highest number of elements in myarray . I have tried doing:

{
  "sort": {
    "_script": {
        "type": "number",
        "order": "desc",
        "script": "doc.containsKey('myarray.field.value') ? doc['myarray.field.value'].values.size() : 0"
    }
  }
}

which gives me an error like Fielddata is disabled on text fields by default.[...] Alternatively use a keyword field instead . So I try with

{
  "sort": {
    "_script": {
        "type": "number",
        "order": "desc",
        "script": "doc.containsKey('myarray.field.value.keyword') ? doc['myarray.field.value.keyword'].values.size() : 0"
    }
  }
}

which gives me the error Illegal list shortcut value [values]. . So then I tried with (removing the values keyword):

{
  "sort": {
    "_script": {
        "type": "number",
        "order": "desc",
        "script": "doc.containsKey('myarray.field.value.keyword') ? doc['myarray.field.value.keyword'].size() : 0"
    }
  }
}

and it works, however I have some results that are sorted nicely and suddenly an element that should be at the top appears in the middle. Is that because it is sorting by the length of the value as a string and not the length of myarray ?

This is because text type mapping does not provide sorting, to add sorting you must map the array field with keyword type. For more info and syntax please refer this: https://www.elastic.co/guide/en/elasticsearch/reference/6.8/search-request-sort.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM