How to search for separate key and value fields in an array in ElasticSearch?


How to search for separate key and value fields in an array in ElasticSearch?



My ElasticSearch documents contain a nested collection of form fields. Each field has a name and a value and the mapping is as follows:


form: {
properties: {
id: { type: 'integer' },
name: { type: 'text' },
form_data: {
type: 'nested',
properties: {
'name': { type: 'keyword' },
'value': { type: 'text', analyzer: 'full_text_analyzer' }
}
}
}
}



I need to allow the user to search for multiple form fields to refine their search. They can choose which fields to search by and assign a value to each. For example


applicant_name = 'Joe'
pet_type = 'dog'



This would find all documents that contained a field named applicant_name which had a value fuzzy matching Joe as well as a field named pet_type and a value fuzzy matching dog.


applicant_name


Joe


pet_type


dog



The query I'm trying to do this with is as follows.:


{
"query": {
"bool": {
"must": [{
"nested": {
"path": "form_data",
"query": {
"filter": {
"bool": {
"must": [
{
"bool": {
"must": [
{ "term": { "form_data.name": "applicant_name" } },
{ "match": { "form_data.value": "Joe" } }
]
}
},
{
"bool": {
"must": [
{ "term": { "form_data.name": "pet_type" } },
{ "match": { "form_data.value": "dog" } }
]
}
}
]
}
}
}
}
}]
}
}
}



However, I get 0 results.




1 Answer
1



Try using a nested query per condition in your initial "must" clause:


{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "form_data",
"query": {
"bool": {
"must": [
{ "term": { "form_data.name": "applicant_name" } },
{ "match": { "form_data.value": "Joe" } }
]
}
}
}
},
{
"nested": {
"path": "form_data",
"query": {
"bool": {
"must": [
{ "term": { "form_data.name": "pet_type" } },
{ "match": { "form_data.value": "dog" } }
]
}
}
}
}
]
}
}
}





That did it. It would be great if you could include an explanation of why this approach worked while the original one did not.
– Soviut
2 days ago





Glad it worked! Unfortunately my understanding of ES is still pretty shallow so i'm not sure why the original query did not work.
– lta
2 days ago






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Comments

Popular posts from this blog

paramiko-expect timeout is happening after executing the command

Opening a url is failing in Swift

Export result set on Dbeaver to CSV