filtering elastic search results by fields and values

Viewed 117

I have a sample data that includes data from univerities, schools and dormitories. I want to use this data in Elasticsearch. I send the body as follows when making filteration:

Sample Data:

[
    {
        name: 'Maisha school of sience',
        school_type: 'public',
        location: 'A',
    },
    {
        name: 'Maisha elementry school',
        school_type: 'private',
        location: 'B',
    },
    {
        name: 'istanbul university',
        university_type: 'public',
        location: 'C',
    },
    {
        name: 'Maisha university',
        university_type: 'private',
        location: 'D',
    },
    {
        name: 'kbb center of Maisha',
        dormitory_type: 'public',
        location: 'E',
    },
    {
        name: 'high Maisha dorm',
        dormitory_type: 'private',
        location: 'F',
    },
]

My Query:

"query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }

Result:

{
   name: 'Maisha school of sience',
   school_type: 'public',
   location: 'A',
}

This result actually makes sence because it gives the exact result but dormitory and university data are not shown which is the main problem. In other words, I only want the school_type to be public but why it filters out and not showing university and dormitory data?

1 Answers

You are almost correct, you just need to change must in bool to should. I just indexed your sample data and using below query.

Sample documents

{
        "name": "Maisha school of sience",  // doc-1
        "school_type": "public",
        "location": "A"
}
{
        "name": "istanbul university", // doc-2
        "university_type": "public",
        "location": "C"
}
{
        "name": "Maisha university", // so on
        "university_type": "private",
        "location": "D"
}
{
        "name": "kbb center of Maisha",
        "dormitory_type": "public",
        "location": "E"
}
{
        "name": "high Maisha dorm",
        "dormitory_type": "private",
        "location": "F"
}
{
    "query": {
        "bool": {
            "should": [. ->> notice should clause here
                {
                    "query_string": {
                        "query": "*Maisha*",
                        "fields": [
                            "name"
                        ]
                    }
                },
                {
                    "match": {
                        "school_type": "public"
                    }
                }
            ]
        }
    }
}

it returns below result, hope you are looking for the same output.

{
    "took": 12,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 4,
            "relation": "eq"
        },
        "max_score": 1.287682,
        "hits": [
            {
                "_index": "71296703",
                "_id": "1",
                "_score": 1.287682,
                "_source": {
                    "name": "Maisha school of sience",
                    "school_type": "public",
                    "location": "A"
                }
            },
            {
                "_index": "71296703",
                "_id": "3",
                "_score": 1.0,
                "_source": {
                    "name": "Maisha university",
                    "university_type": "private",
                    "location": "D"
                }
            },
            {
                "_index": "71296703",
                "_id": "4",
                "_score": 1.0,
                "_source": {
                    "name": "kbb center of Maisha",
                    "dormitory_type": "public",
                    "location": "E"
                }
            },
            {
                "_index": "71296703",
                "_id": "5",
                "_score": 1.0,
                "_source": {
                    "name": "high Maisha dorm",
                    "dormitory_type": "private",
                    "location": "F"
                }
            }
        ]
    }
}
Related