Search code examples
elasticsearchelasticsearch-painless

Is there a way to compare string alphabetically in painless


I would like to execute this kind of operation in painless :

if (_value >= 'c)' { 
 return _value 
} else { 
 return '__BAD__' 
}

value is a string and I would like this following behaviour :

if value is foo I want to replace it with __BAD__ if the value is bar, I want to keep bar. only values alphabetically after 'c' should be set to __BAD__.

I got this exception :

"lang": "painless",
"caused_by": {
  "type": "class_cast_exception",
  "reason": "Cannot apply [>] operation to types [java.lang.String] and [java.lang.String]."
}

Is there a way to perform string alphabetical comparaison between string in painless ?

My documents are looking :

{
  "id": "doca",
  "categoryId": "aaa",
  "parentNames": "a$aa$aaa"
},
{
  "id": "docb",
  "categoryId": "bbb",
  "parentNames": "a$aa$bbb"
},
{
  "id": "docz",
  "categoryId": "zzz",
  "parentNames": "a$aa$zzz"
}

and my query is like :

{
  "query": {
    "bool": {
      "filter": []
    }
  },
  "size": 0,
  "aggs": {
    "catNames": {
      "terms": {
        "size": 10000,
        "order": {
          "_key": "asc"
        },
        "script": {
              "source": "if(doc['parentNames'].value < 'a$aa$ccc') {return doc['parentNames'].value} return '__BAD__'",
              "lang": "painless"
            }
      },
      "aggs": {
        "sort": {
          "bucket_sort": {
            "size": 2
          }
        },
        "catId": {
          "terms": {
            "field": "categoryId",
            "size": 1
          }
        }
      }
    }
  }
}

I am expecting the result :

{
  "took": 29,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "catNames": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "__BAD__",
          "doc_count": 1,
          "catId": {
            "buckets": [
              {
                "key": "aaa",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "a$aa$bbb",
          "doc_count": 1,
          "catId": {
            "buckets": [
              {
                "key": "bbb",
                "doc_count": 1
              }
            ]
          }
        },
        {
          "key": "a$aa$zzz",
          "doc_count": 1,
          "catId": {
            "buckets": [
              {
                "key": "zzz",
                "doc_count": 1
              }
            ]
          }
        }
      ]
    }
  }
}

Solution

  • In fact, I can use the compareTo function of java.lang.String.

    if (_value.compareTo('c') > 0) { 
     return _value 
    } else { 
     return '__BAD__' 
    }
    

    My query is becoming :

    {
      "query": {
        "bool": {
          "filter": []
        }
      },
      "size": 0,
      "aggs": {
        "catNames": {
          "terms": {
            "size": 10000,
            "order": {
              "_key": "asc"
            },
            "script": {
                  "source": "if(doc['parentNames'].value.compareTo('a$aa$ccc')) {return doc['parentNames'].value} return '__BAD__'",
                  "lang": "painless"
                }
          },
          "aggs": {
            "sort": {
              "bucket_sort": {
                "size": 2
              }
            },
            "catId": {
              "terms": {
                "field": "categoryId",
                "size": 1
              }
            }
          }
        }
      }
    }