How to write a conditional query on multiple fields?
Explain: I have an index let say "case" and the fields are "title", "secondTitle(contain nested obj)", "source(contain nested obj)". Now I want to search on title and want number of document inside of secondTitle, which also contains in source field of another document.
PUT /case_indx_tmp_tmp
{
"mappings": {
"properties": {
"title":{
"type": "text",
"fields": {
"title":{
"type":"keyword"
}
}
},
"secondTitle":{
"type": "nested",
"properties": {
"second_title":{
"type":"text",
"fields": {
"secondtitle":{
"type":"keyword"
}
}
}
}
},
"source":{
"type": "nested",
"properties": {
"source_title":{
"type":"text",
"fields": {
"sourcetitle":{
"type":"keyword"
}
}
}
}
}
}
}
}
PUT /case_indx_tmp_tmp/_doc/1
{
"title" : "Case 1",
"secondTitle" : [
{
"case_title" : "Case 2"
}
],
"source":[
{
"source_title":"Case 3"
},
{
"source_title":"Case 4"
}
]
}
PUT /case_indx_tmp_tmp/_doc/2
{
"title" : "Case 2",
"secondTitle" : [
{
"case_title" : "Case 3"
},
{
"case_title" : "Case 4"
},
{
"case_title" : "Case 1"
}
],
"source":[
{
"source_title":"Case 1"
}
]
}
PUT /case_indx_tmp_tmp/_doc/3
{
"title" : "Case 3",
"secondTitle" : [
{
"case_title" : "Case 5"
},
{
"case_title" : "Case 4"
},
{
"case_title" : "Case 1"
}
],
"source":[
{
"source_title":"Case 2"
},
{
"source_title":"Case 5"
}
]
}
PUT /case_indx_tmp_tmp/_doc/4
{
"title" : "Case 5",
"source":[
{
"source_title":"Case 3"
},
{
"source_title":"Case 4"
},
{
"source_title":"Case 2"
}
]
}
If search term is "Case" then it will return
title: Case 1
secondTitle_count: 1title: Case 2
secondTitle_count: 3title: Case 3
secondTitle_count: 2title: Case 5
secondTitle_count: 0
My solution was, to iterate all documents which return after search query and make new bool query object for the "source" field where values are from secondTitle.
Thank You.