Elasticsearch substr()

Hi,

I want to use substr function for my field 'AlimTarihi'

for example ALimTarihi = 2009-01-02

I want to take onlly ALimTarihi = 2009

My codes:

GET titubb56k/ihale/_search 
 {
    "size":0,
            "query":{
                "match": {
                    "UrunNo": "+1782608550161J"
            }
    },
    "aggs": {
      "IstekliKoduCount": {  "terms":{ "field": "IstekliKodu"}},
       
          "TarihCount": {  "terms":{ "field": "AlimTarihi"}},
          
            "ToplamTutarSum": {"sum": {"field": "ToplamTutar"}},
            
             "my_terms": {"terms": {"script" : {"inline": "doc['AlimTarihi'].value.substring(0,2)"}}}
    }
 }

error:

{
  "took": 6,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 3,
    "failed": 2,
    "failures": [
      {
        "shard": 1,
        "index": "titubb56k",
        "node": "NchqMHFlRZqnHALL4pRzUA",
        "reason": {
          "type": "script_exception",
          "reason": "runtime error",
          "caused_by": {
            "type": "illegal_argument_exception",
            "reason": "Unable to find dynamic method [substring] with [2] arguments for class [java.lang.Long]."
          },
          "script_stack": [
            "doc['AlimTarihi'].value.substring(0,4)",
            "                       ^---- HERE"
          ],
          "script": "doc['AlimTarihi'].value.substring(0,4)",
          "lang": "painless"
        }
      }
    ]
  },
  "hits": {
    "total": 0,
    "max_score": 0,
    "hits": []
  },
  "aggregations": {
    "ToplamTutarSum": {
      "value": 0
    },
    "IstekliKoduCount": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    },
    "my_terms": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    },
    "TarihCount": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": []
    }
  }
}

What can I do?

Hi,

What is the version of Elasticsaerch you are running on ? In order to known the script to use Groovy or Painless.

EDIT :

            "reason": "Unable to find dynamic method [substring] with [2] arguments for class [java.lang.Long]."

The error says that there is no method for the type Long, Can you check your mapping ?

Bye,
Xavier

I am running on ES-5.0.0 and my mapping for AlimTarihi :

PUT titubb56k/ihale/_mapping
{
  "properties" : {
  "AlimTarihi" : {
    "type": "date",
    "format" : "yyyy-MM-dd"
    }  }  } 

Do I have to do a different mapping here?

Ok your field is Date (a long internaly) that's why you can't use a substring() method. Instead you can
look at the Date Histogramme if you need to group by year.

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html

Something like that :

{
"aggs" : {
    "your_agg_name" : {
	"date_histogram" : {
	    "field" : "AlimTarihi",
	    "interval" : "year",
	    "format" : "yyyy" 
	}
    }
}
}

Thank you.

It worked right.

1 Like

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