This is my elasticsearch query. I am trying to get all the documents which meets the condition where if job_id
is 1 then get documents which has sourced = 0
else if job_id
is not 1 then get documents with sourced = 1
.
But it returns all the documents.
[
'script' =>
['script' =>
[
'lang' => 'painless',
'source' => "(doc['job_id'].size() > 0 && doc['job_id'].value !== params.jid) || (doc['job_id'].size() > 0 && doc['job_id'].value == params.jid && doc['sourced'].value == 0)",
'params' => ['jid' => 1]
]
]
]
Mapping for referred fields
"job_id" : {
"type" : "long"
},
"sourced" : {
"type" : "byte"
},
Each document is evaluated by the inline script.
An example (base on your mappings):
Insert documents
PUT my_index/_doc/1
{
"job_id": 1,
"sourced": 0
}
PUT my_index/_doc/2
{
"job_id": 0,
"sourced": 2
}
Search Query
GET my_index/_search (Kibana usage)
{
"query": {
"script": {
"script": {
"lang": "painless",
"source": "doc['job_id'].value == params.jid ? doc['sourced'].value == 0 : doc['sourced'].value == 1",
"params": {
"jid": 1
}
}
}
}
}
Your usage
[
'script' =>
['script' =>
[
'lang' => 'painless',
'source' => "doc['job_id'].value == params.jid ? doc['sourced'].value == 0 : doc['sourced'].value == 1",
'params' => ['jid' => 1]
]
]
]
Results
"hits" : [
{
"_index" : "my_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"job_id" : 1,
"sourced" : 0
}
}
]
Hope this helps