Elastic-search advanced search query issue

Dear All.

I have some use case:-

01:- i have index(index01) that contains 8 fields
:- fields like- a,b,c,d,e,f,g,h,i
:- each fields has diff value
02:- now search condition here is?
:- if i search for fields [a:"alpha"] then it will search all data matching
with field[a]
:- if search for fields [b:'data',c:'meta',d:'sata'] then it will search only
values but not other matched fields within [a,e,f,g,h]
03:- this is final case in, index fields[i] that contains integer value and we have to perform all above condition based on matching fields [i:1411]


What did you try so far?

Could you provide a full recreation script as described in About the Elasticsearch category. It will help to better understand what you are doing. Please, try to keep the example as simple as possible.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Hi @dadoonet.


i am trying to search the records based on below sql query condition :-1:
condition :- 01
i have 8-fields in current index use case and i want to search all fields value based on provided value with fields like-

in above search field i am not providing field value for field3,4,6,7
but we need this field for next search in current query and want exact matching result with previous -

when i used --and-- (MATCH IN ES)condition for

--then it fetch exact matching records but if we are not passing the value for any field this query is going to fail.

--if i used --OR--(should in ES) condition then it fetching all matching records with all fields that is wrong in my use case.

"query": "select * from dbdataindex_join_allcolumns_qa_modify_cm_28_08_2019 where  regionname='MSP 05S' and caremanager='HM CM' and patientname='Aaron' "


<{ "size" : 1000, "query" : { "bool" : { "must" : [ { "bool" : { "must" : [ { "term" : { "regionname.keyword" : { "value" : "MS5S", "boost" : 1.0 } } }, { "term" : { "caremanager.keyword" : { "value" : "HECM", "boost" : 1.0 } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, { "term" : { "patientname.keyword" : { "value" : "Aaa", "boost" : 1.0 } } } ], "adjust_pure_negative" : true, "boost" : 1.0 } }, "_source" : { "includes" : [ "@version", "active", "beneficiary_status", "care_program_name", "caremanager", "city", "dob1", "email", "medicare", "patientname", "pcp", "phone_home", "provider_type", "regionname", "sex" ], "excludes" : [ ] }, "docvalue_fields" : [ { "field" : "@timestamp", "format" : "epoch_millis" }, { "field" : "age" }, { "field" : "careprog_id" }, { "field" : "dob", "format" : "epoch_millis" }, { "field" : "log_date", "format" : "epoch_millis" }, { "field" : "patient_provider_hrk" }, { "field" : "pid" }, { "field" : "user_id" } ], "sort" : [ { "_doc" : { "order" : "asc" } } ] } />

Final Request :-

i want --all field-- in search search query but user can provide some time 3,4,5 fields only but result should come based on provided fields values.

Very thanks .

Hi @dadoonet.

please help :pray:

As discussed in the other thread you will need to construct the bool clauses based on what fields the user actually submits.

Hi @Christian_Dahlqvist

you are correct but i have an application that contains 8-search fields

condition here is user some time searching only 3,4,5 fields from current index

when i applied any --bool condition like ---should /must this ---is is not going to fix(as previous DSL ).

so please guide me...how can i implement all 8-fields exact match as user pass the values.

i know ,i am going something alter but this bool is not resolving this use case.

please understand my requirement i want all fields in search query but user may pass only 3,4 fields .


Hi @Christian_Dahlqvist

Please look at my issue and try to help.

You need to create separate queries for each combination of fields.

If this is not the answer I do tho think I understand your question.

Hi @Christian_Dahlqvist .

but how can we implement that as seperate query for each search fields .

Please give some clue ,you are getting my issue in correct way.


You need to build logic into your application that generates the query based on the users input.

Hi @Christian_Dahlqvist.


is there any possible way of design query based on user input please share some link or some idea.

as per current request we have to write 8-different query based on user inputs based on condition but how.


Hi @Christian_Dahlqvist and @dadoonet .

Please help me for this trouble/issue .still i am waiting for some response.

How can we write elastic query based on user input?


You need to provide a script that helps playing with your data as I asked in Elastic-search advanced search query issue.

Then explain what is the exact output you'd like to see.
From that we can have a chance to understand what you want to do and if it's feasible or not.

Hi @dadoonet.

as we discussed previous .

i am using sample query/script below:
<{ "size" : 1000, "query" : { "bool" : { "must" : [ { "bool" : { "must" : [ { "bool" : { "must" : [ { "term" : { "patientname.keyword" : { "value" : "Aadra", "boost" : 1.0 } } }, { "term" : { "sex.keyword" : { "value" : "F", "boost" : 1.0 } } } ] } }, { "term" : { "dob1.keyword" : { "value" : "04/7/1942", "boost" : 1.0 } } } ] } }, { "term" : { "caremanager.keyword" : { "value" : "HEM", "boost" : 1.0 } } } ] } } />

but problem i am facing here :
i have to pass each doc:values if i will not pass value in this query this is fetching null value
but i want to fetch those doc:value what i passed only
this index contains 8-fields/doc and query should be capable to search all 8-fields
but may be some time user can pass only 3,4 doc:value and based on passed value we want exact data.

crying like any thing please help

Very Thanks

I don't understand. And I cannot reproduce anything.

Hi @Christian_Dahlqvist and @dadoonet.

More details :

here is index data contains all(fields/doc):

"patientname":"Aan ra",
"pcp":" ",

but i want to search only below based fields/doc:values and result should be exact match:

"patientname":"Aan ra",

Note : i passed any above value then my query should fetch exact matched data only no other data like or and like operator.

but my query should be able to search only provided doc:value data.

Please continue ...


Hi @dadoonet.

i think you are taking more complex . only problem is that when i use --bool-must-- in my query for search all fields/doc in current .its force to pass fields/doc:'value'.

if i don;t pass the value with search fields/doc:'' " it give nothing/null.

@Christian_Dahlqvist i think you got my problem already please suggest some idea.


Hi @Christian_Dahlqvist and @dadoonet.

this is my final request from forum Team.
please look at this pic and each search fields:

i given GUI view to all forum please try to understand this issue.

case 01 :

i provide patient Name ='ramaya' and gender='M' then fetch all matched ramaya as patientname and M as gender.

case 02:

if i provide patienName ='ramaya' and gender='M' and dob='15/02/1933' then fetch all matched data with given value

case 03:

all search fields will be there in query as shown in GUI. for searching purpose but user can pass only : ----patientname ,gender or dob. not all fields.

case 04:

result should be come only exact matched what user has given on GUI.


As I said before you probably need to write a piece of code that creates the query dynamically based on the fields the user have specified.

If I have understood correctly a template based approach will not work, which seems to be what you are looking for.

Hi @Christian_Dahlqvist.

as you are saying how can this possible in current use case.

for example i have 10- search fields in GUI so we have to write 10*10 means 100-condition here and based on that condition we have to run/execute 100-elastic search query.

correct me if i am wrong here ?

but one more issue how can we handle here multi condition request here with elastic-search.