How to query in condition of length of string fields and the field ending with "99"


(Huangkai) #1

there is a error when using the query below:

GET trxdetail-fj/_search
{
"query": {
"script": {
"script": {
"source": "doc['OPPACC'].length()>10",
"lang": "painless"
}
}
}
}

errror message:

{
"error": {
"root_cause": [
{
"type": "query_shard_exception",
"reason": "failed to create query: {\n "script" : {\n "script" : {\n "source" : "doc['OPPACC'].length()>10",\n "lang" : "painless"\n },\n "boost" : 1.0\n }\n}",
"index_uuid": "n1UUebJaT7WJiomMDjbCXw",
"index": "trxdetail-fj"
}
],
"type": "search_phase_execution_exception",
"reason": "all shards failed",
"phase": "query",
"grouped": true,
"failed_shards": [
{
"shard": 0,
"index": "trxdetail-fj",
"node": "KNkvY1LsSQ-VA61GpNNWkA",
"reason": {
"type": "query_shard_exception",
"reason": "failed to create query: {\n "script" : {\n "script" : {\n "source" : "doc['OPPACC'].length()>10",\n "lang" : "painless"\n },\n "boost" : 1.0\n }\n}",
"index_uuid": "n1UUebJaT7WJiomMDjbCXw",
"index": "trxdetail-fj",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "cannot execute scripts using [filter] context"
}
}
}
]
},
"status": 400
}

and my config is :

# ---------------------------------- script -----------------------------------
script.allowed_types: inline
script.allowed_contexts: search,update,aggs

finally, i want to search in my index to find some record which is ending with '99' and the length of field is 12.

for example, one of records is "347897109899" in the field naming "oppacc" which match the condition.

how to do it ?


(Alexander Reelsen) #2

While you could solve this with a script, the way you currently wrote this implies, that every hit from your query will be run against a script. This might work with a low number of results, but will be painfully slow with a already medium number of documents. Also it will be linearly slower with the number of documents.

Another possible solution is to either take the prefix and suffix during indexing and store them in own fields, so that you just have to execute term queries. If you do not want to change the JSON, you should check out the ngram tokenizer which stores part of the terms.

Hope this helps!

--Alex


(Huangkai) #3

thanks for reply. actually, i just want to know how to use script in query, and how to use more complicate script for filtering result.


(system) closed #4

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