I have to delete a field from search results in Elasticsearch. I want to remove the detail
property if display_detail
is False.
the documents example :
{
"last_name" : "anna",
"first_name" : "bella",
"detail" : "descript their hobby",
"display_detail" : true
},
{
"last_name" : "anna",
"first_name" : "michelle",
"detail" : "another hobby",
"display_detail" : false
}
this is the query looks like :
indexname=indexname
query = {
"query_string" : {
"query" : anna,
"fields: : ["first_name","last_name"]
}
}
results = es.search(index=indexname, query=query , source = ["first_name","last_name","detail"])
what I expect:
{
"last_name" : "anna",
"first_name" : "bella",
"detail" : "descript their hobby",
},
{
"last_name" : "anna",
"first_name" : "michelle",
}
I can get the results above after get the search results like this :
for element in results['hits']['hits']:
if element["display_detail"] == "true":
del element['detail']
json.append(element)
is this a good way to deal with it? or is there any chance I can get the faster/cleanest way by using elastic query?
It is not possible to select/hide fields on a condition based.
However you can select/hide fields using fields options or source options from all the documents
It's always recommended to handle such micro operations on the client end the same way you handled.