Skip to content

Instantly share code, notes, and snippets.

@dushujun
Created May 4, 2017 02:57
Show Gist options
  • Save dushujun/ffd604c3dfea7c277505e721b3447e2d to your computer and use it in GitHub Desktop.
Save dushujun/ffd604c3dfea7c277505e721b3447e2d to your computer and use it in GitHub Desktop.
elasticsearch query by string length
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['被执行人姓名/名称.raw'].getValue().length() < 4 ",
"lang": "painless"
}
}
}
}
}
}
@amir2b
Copy link

amir2b commented Dec 14, 2022

That query is wrong!
Error: A document doesn't have a value for a field! Use doc[].size()==0 to check if a document is missing a field!

The correct query is:

{
  "query": {
    "bool": {
      "must": {
        "script": {
          "script": {
            "inline": "doc['被执行人姓名/名称.raw'].size() > 0 && doc['被执行人姓名/名称.raw'].value.length() < 4",
            "lang": "painless"
          }
        }
      }
    }
  }
}

@confiq
Copy link

confiq commented Jun 30, 2023

am I only one that gets

oc['log_processed.request_id'].size() > 0 && doc['log_processed.request_id'].value.length() < 4
    ^---- HERE
    

error ?

@amir2b
Copy link

amir2b commented Jul 1, 2023

am I only one that gets

oc['log_processed.request_id'].size() > 0 && doc['log_processed.request_id'].value.length() < 4
    ^---- HERE
    

error ?

If a record dose not have the log_processed field, it will gave you this error.
I think the correct query is:

doc['log_processed'] != null && doc['log_processed.request_id'].size() > 0 && doc['log_processed.request_id'].value.length() < 4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment