# Query String query with multiple terms

**URL:** <https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723>\
**Category:** Elasticsearch\
**Created:** [June 3, 2024, 2:29pm UTC](https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723 "2024-06-03T14:29:22Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![darrylds](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darrylds/32/135000_2.png) [@darrylds](https://discuss.elastic.co/u/darrylds)\
**Post date:** [June 3, 2024, 2:29pm UTC](https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723/1 "2024-06-03T14:29:22Z")

</div>

I have a quite large index, well at least to me, 3 million records and adds between 500 to 2000 more a day.

There are 150 categories, I want to be able to search for query strings within those categories. There are also youtube videos that are available for each categories (sources, source\_id). The search I have tried several different ways to achieve this. The example below, is searching category "source\_id: 5, and youtube videos true, sorted descending, top 10, and searching on term NFL.

How do you create a query that does a query string search with multiple exact terms?

```auto
{
    "size": 10,
"query" : {
  "bool" : {
    "should" : [
      { "term": { "source_id": 5}},
      { "term": { "youtube": true }}
    ],
    "minimum_should_match": 2
  },
  "query_string": {
    "query": "NFL",
    "default_field": "title"
}
},
    "sort":
        {"created_at": "desc"}

}

```

---

<div class="post-metadata">

**Author:** ![Artem\_Shelkovnikov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artem_shelkovnikov/32/134322_2.png) [@Artem\_Shelkovnikov](https://discuss.elastic.co/u/Artem_Shelkovnikov)\
**Post date:** [June 3, 2024, 3:37pm UTC](https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723/2 "2024-06-03T15:37:32Z")

</div>

Hi Darryl,

Have you tried using [filter context](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-filter-context.html)?

It would make sense to put `youtube: true` and `source_id: 5` as filter and have your query be a regular query.

---

<div class="post-metadata">

**Author:** ![darrylds](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/darrylds/32/135000_2.png) [@darrylds](https://discuss.elastic.co/u/darrylds)\
**Post date:** [June 3, 2024, 4:13pm UTC](https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723/3 "2024-06-03T16:13:16Z")

</div>

I actually have a solution, and I used filter for the terms and query\_string.

```auto

{
    "size": 10,
  "query": {
    "bool": { 
      "must": [
        {
          "query_string": {
            "query": "Commander",
            "default_field": "title"
          }
        }
      ],
      "filter": [
        {"term": { "youtube" : true }},
        {"term": { "source_id": 14 }}
      ]
    }
  }
}

```

Thanks....

---

<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 1, 2024, 4:13pm UTC](https://discuss.elastic.co/t/query-string-query-with-multiple-terms/360723/4 "2024-07-01T16:13:24Z")

</div>

Closed on July 1, 2024, 4:13pm UTC

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