Multi fields, multi prefixes search

Hello All, hope someone can enlighten me on this one. Suppose I have the following data:

{ "index": { "_index": "courses_test", "_id": 1 } }
{ "Course Name": "Bachelor of Arts in Music", "Job Role": "Theatre & Media Director, Video Engineer" }
{ "index": { "_index": "courses_test", "_id": 2 } }
{ "Course Name": "Bachelor of Arts in Engineering", "Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant." }

My objective is to match "Bachelor" AND "Engineering" in their Course Name AND Job Role fields. With the query below, not quite sure why 2 courses are being returned but document ID 2 does not satisfy the condition.

It works as expected if I search in "Course Name" only. Searching in "Job Role" returns 0, which is correct too.

I am using query string and using * so that even if the user just typed in prefixes e.g. 'bach eng', it should still match.

Full query:

{
    "query": {
        "bool": {
            "must": [
                {
                    "query_string": {
                        "query": "Bachelor* AND Engineer*",
                        "fields": [
                            "Course Name",
                            "Job Role"
                        ]
                    }
                }
            ]
        }
    }
}

Response:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 2,
            "relation": "eq"
        },
        "max_score": 2.0,
        "hits": [
            {
                "_index": "courses_test",
                "_type": "_doc",
                "_id": "1",
                "_score": 2.0,
                "_source": {
                    "Course Name": "Bachelor of Arts in Music",
                    "Job Role": "Theatre & Media Director, Video Engineer"
                }
            },
            {
                "_index": "courses_test",
                "_type": "_doc",
                "_id": "2",
                "_score": 2.0,
                "_source": {
                    "Course Name": "Bachelor of Arts in Engineering",
                    "Job Role": "Graduate policy officer, editorial assistant, communications and campaigns assistant, assistant advocacy officer, employment consultant"
                }
            }
        ]
    }
}

Thank you for your help!

The way the query works is different than what you expect. It is sufficient to match both terms in one of the fields.

If you want to match both terms in both fields, you should go with two queries, one for each field.

bool:
  must:
    [
      query_string: field: course_name,
      query_string: field: job_role,
    ]

also, please take your time to read a little more on wildcard queries and if it makes sense not to use them on bigger datasets. The definitive guide is an old ebook, but still a very good introduction into full text search basics.

Hope that helps!

Thank you for you input. So what I did to meet my object was to separate the queries like in your reply but used 'should' instead of 'must' because I still want to match results in case terms are existing in both fields (OR)

good catch, I missed that! thx for answering here so others can see that as well

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.