# Multi fields, multi prefixes search

**URL:** <https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504>\
**Category:** Elasticsearch\
**Created:** [June 21, 2021, 8:12am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504 "2021-06-21T08:12:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![janlmpn](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@janlmpn](https://discuss.elastic.co/u/janlmpn)\
**Post date:** [June 21, 2021, 8:12am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504/1 "2021-06-21T08:12:10Z")

</div>

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

```auto
{ "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:

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

```

Response:

```auto
{
    "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!

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 21, 2021, 8:40am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504/2 "2021-06-21T08:40:49Z")

</div>

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.

```auto
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](https://www.elastic.co/guide/en/elasticsearch/guide/current/index.html) is an old ebook, but still a very good introduction into full text search basics.

Hope that helps!

---

<div class="post-metadata">

**Author:** ![janlmpn](https://avatars.discourse-cdn.com/v4/letter/j/f4b2a3/32.png) [@janlmpn](https://discuss.elastic.co/u/janlmpn)\
**Post date:** [June 23, 2021, 2:59am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504/3 "2021-06-23T02:59:04Z")

</div>

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)

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [June 23, 2021, 11:49am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504/4 "2021-06-23T11:49:01Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 21, 2021, 11:49am UTC](https://discuss.elastic.co/t/multi-fields-multi-prefixes-search/276504/5 "2021-07-21T11:49:46Z")

</div>

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