Hi everyone, I have a index example above:
PUT target_index
{
"mappings": {
"properties": {
"targetoperator": { "type": "keyword" },
"targetvalue": {
"type": "float"
}
}
}
}
PUT target_index/_doc/1
{
"targetoperator": [">", "=="],
"targetvalue": [4, 5]
}
PUT target_index/_doc/2
{
"targetoperator": ["<", "<="],
"targetvalue": [6, 5.5]
}
PUT target_index/_doc/3
{
"targetoperator": [">=", ">", "<", "<="],
"targetvalue": [2, 4, 8, 8.5]
}
PUT target_index/_doc/4
{
"targetoperator": [">"],
"targetvalue": [2.5]
}
PUT target_index/_doc/5
{
"targetoperator": ["!="],
"targetvalue": [3.5]
}
I run a script query above:
GET target_index/_search
{
"query": {
"script": {
"script": {
"source": """
double userCriteria = params.userTarget.seniority;
def targetoperator = doc['targetoperator'];
def targetvalue = doc['targetvalue'];
if (targetoperator.size() == targetvalue.size()) {
for (int i = 0; i < targetoperator.size(); i++) {
def operaVal = targetoperator[i];
def valueVal = targetvalue[i];
if (operaVal == "<=") {
if (valueVal <= userCriteria) {
return true;
}
}
}
}
return false;
""",
"params": {
"userTarget": {
"gender": "male",
"seniority": 6
}
}
}
}
},
"_source": [
"targetoperator",
"targetvalue"
]
}
My target want check value of field params.userTarget.seniority <=
value of field targetvalue
?
But, search result return about document id = 2, and id = 3.
{
"took": 18,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": 1,
"hits": [
{
"_index": "target_index",
"_id": "2",
"_score": 1,
"_source": {
"targetoperator": [
"<",
"<="
],
"targetvalue": [
6,
5.5
]
}
},
{
"_index": "target_index",
"_id": "3",
"_score": 1,
"_source": {
"targetoperator": [
">=",
">",
"<",
"<="
],
"targetvalue": [
2,
4,
8,
8.5
]
}
}
]
}
}
If correct, the result return only document id = 3.