How to sory array type field using the length

Hi,

I want to create an index having with one array field.. Then, how can I give mapping for that array field which is available to sort using the length of that array. One more thing that it supports that the array having more that n length. Can anybody share the example mapping and queries for this conditions.

Thank you,

Hi @Anand_Konagala,

To confirm, are you looking to sort the documents in the query using the length of array fields? You should be able to do that using sort context with a Painless script using the Array length operator.

Hope that helps!

Hi @carly.richmond ,

thanks for the response, And I have one more doubt that what kind of field type is suitable for this query.

Thank you,

Hi @Anand_Konagala,

By field type do you mean strings versus integers versus objects? There isn't technically a dedicated array type as fields accept 0 or more values. The only condition would be all elements need to be the same type.

I would recommend reading the arrays section of the documentation located here for a more complete answer.

Hope that helps!

Hi @carly.richmond,

I gave a mapping for one of the field as type "nested"

PUT mr-profiles
{
"mapping": {
    "properties": {
        "locations": {
          "type": "nested"
        }
      }
  }
}

And I ran one query on that,

GET mr-profiles/_search
{
  "sort": {
  "_script": {
    "type": "number",
    "script": {
      "lang": "painless",
      "source": "doc['locations'].values.size() * params.factor",
      "params": {
        "factor": 1.1
      }
    },
    "order": "asc"
  }
}

}

But, It throughs an Error

"type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "mr-profiles",
        "node": "9PF_FW5aQcmLIxDothVTOg",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:148)",
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:191)",
            "org.elasticsearch.server@8.8.0/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            "doc['locations'].values.size() * params.factor",
            "    ^---- HERE"
          ],
          "script": "doc['locations'].values.size() * params.factor",
          "lang": "painless",
          "position": {
            "offset": 4,
            "start": 0,
            "end": 61
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [locations] in mapping"
          }
        }
      }
    ]

I gave a mapping, But Why it shows mapping not found error..
Could you tell me that Why I am getting This error

I'm not sure this is the issue, as I would recommending double checking the mapping. But should it not be doc['locations'].length as per this example?

Hii @carly.richmond

I tried a lot of queries.. but, It not works.. It through an error.. Can you try this
Here is my mapping,

PUT array_sort
{
  "mappings": {
    "properties": {
      "Skills": {
        "type": "nested",
        "properties": {
          "text": {
            "type": "keyword"
          }
        }
      }
    }
  }
}

This is the data

POST array_sort/_doc/1
{
  "Skills": [
    {"text": "java"},
    {"text": "elasticsearch"}
  ]
}

POST array_sort/_doc/2
{
  "Skills": [
    {"text": "python"},
    {"text": "ruby"},
    {"text": "javascript"}
  ]
}

POST array_sort/_doc/3
{
  "Skills": [
    {"text": "sql"},
    {"text": "html"},
    {"text": "css"}
  ]
}

Here is the query for finding that array size more then two,

GET array_sort/_search
{
  "query": {
    "nested": {
      "path": "Skills",
      "query": {
        "script": {
          "script": {
            "source": "doc['Skills'].length > 3",
            "lang": "painless"
          }
        }
      }
    }
  }
}

I tried a lot of queries But, I am getting an error.. Here is an error

{
  "error": {
    "root_cause": [
      {
        "type": "script_exception",
        "reason": "runtime error",
        "script_stack": [
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
          "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
          """if (doc['Skills'].length() > 2) {
                """,
          "        ^---- HERE"
        ],
        "script": " ...",
        "lang": "painless",
        "position": {
          "offset": 23,
          "start": 15,
          "end": 65
        }
      }
    ],
    "type": "search_phase_execution_exception",
    "reason": "all shards failed",
    "phase": "query",
    "grouped": true,
    "failed_shards": [
      {
        "shard": 0,
        "index": "array_sort",
        "node": "x4tf2IsOSUu4VVsm2zVwGA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "script_stack": [
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.getFactoryForDoc(LeafDocLookup.java:127)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:170)",
            "org.elasticsearch.server@8.7.1/org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:32)",
            """if (doc['Skills'].length() > 2) {
                """,
            "        ^---- HERE"
          ],
          "script": " ...",
          "lang": "painless",
          "position": {
            "offset": 23,
            "start": 15,
            "end": 65
          },
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "No field found for [Skills] in mapping"
          }
        }
      }
    ]
  },
  "status": 400
}

Can you try this and inform.. Why I am encountering this Error,

Thank you,

Hi @Anand_Konagala,

Looking at your mapping I think you need to check the skills.text field:

GET array_sort/_search
{
  "query": {
    "bool": {
      "filter": {
        "script": {
          "script": """
            return doc['skills.text'].length > 2
          """
        }
      }
    }
  }
}

Your example query also had 2 levels of the script tagv when there should be only one.

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.