# Combine two queries

**URL:** <https://discuss.elastic.co/t/combine-two-queries/126637>\
**Category:** Elasticsearch\
**Created:** [April 3, 2018, 8:17pm UTC](https://discuss.elastic.co/t/combine-two-queries/126637 "2018-04-03T20:17:48Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![subrato](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@subrato](https://discuss.elastic.co/u/subrato)\
**Post date:** [April 3, 2018, 8:17pm UTC](https://discuss.elastic.co/t/combine-two-queries/126637/1 "2018-04-03T20:17:48Z")

</div>

I need to combine 2 queries and I am not sure how to do it. I have one field which has site names (we have few properties that I am scraping and putting cleaned html from it into Elastic Search) and few other fields (content, description, etc). So when I search I would like to search for specific site and then search for keywords in other fields. How would I get around doing this?

Here is the mapping

```
{
  "media": {
    "aliases": {},
    "mappings": {
      "articles": {
        "properties": {
          "author": {
            "type": "keyword"
          },
          "cmskeywords": {
            "type": "text"
          },
          "content": {
            "type": "text",
            "analyzer": "lowercasespaceanalyzer"
          },
          "description": {
            "type": "text"
          },
          "imageUrl": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "imageurl": {
            "type": "keyword"
          },
          "partNumbers": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "partnumbers": {
            "type": "keyword"
          },
          "pubdate": {
            "type": "date"
          },
          "relatedcontentwords": {
            "type": "text"
          },
          "site": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "title": {
            "type": "text"
          },
          "url": {
            "type": "keyword"
          }
        }
      }
    },
    "settings": {
      "index": {
        "number_of_shards": "5",
        "provided_name": "media",
        "creation_date": "1522284975992",
        "analysis": {
          "analyzer": {
            "lowercasespaceanalyzer": {
              "filter": [
                "lowercase"
              ],
              "type": "custom",
              "tokenizer": "whitespace"
            }
          }
        },
        "number_of_replicas": "1",
        "uuid": "Qz8JbotxQee6Dihll3uBSQ",
        "version": {
          "created": "6020299"
        }
      }
    }
  }
}

Here are 2 DSL queries 

{
  "query": {
    "match": {
      "site": {
        "query": "EBN",
        "type": "phrase"
      }
    }
  }

{
  "query": {
    "match": {
      "site": {
        "query": "EBN",
        "type": "phrase"
      }
    }
  }
```

---

<div class="post-metadata">

**Author:** ![Sravanthi\_N\_S\_CH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sravanthi_n_s_ch/32/50473_2.png) [@Sravanthi\_N\_S\_CH](https://discuss.elastic.co/u/Sravanthi_N_S_CH)\
**Post date:** [April 4, 2018, 6:36am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/2 "2018-04-04T06:36:33Z")

</div>

You can combine the queries using bool query. Based on your requirement you can use 'should' or 'must' inside the bool clauses.

---

<div class="post-metadata">

**Author:** ![Varan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/varan/32/32120_2.png) [@Varan](https://discuss.elastic.co/u/Varan)\
**Post date:** [April 4, 2018, 7:33am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/3 "2018-04-04T07:33:47Z")

</div>

You may want to schearch for both the field you want, and then aggregate by the most important field.

---

<div class="post-metadata">

**Author:** ![subrato](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@subrato](https://discuss.elastic.co/u/subrato)\
**Post date:** [April 4, 2018, 1:54pm UTC](https://discuss.elastic.co/t/combine-two-queries/126637/4 "2018-04-04T13:54:44Z")

</div>

Sravanthi\_N\_S\_CH,

I am not sure how I would combine those. I wrote one of those but I don't think it's working. Here is what I wrote.

```
 mediaResponse = elastic.Search<Articles>(s => s
            .Pretty(true)
            .Human(true)
            .Index(mediaIndexName)
            .Type(mediaTypeName)
            .From(DefaultPageNumber)
            .Size(numResults)
            .Query(q => q
                .Bool(
                    sm => sm.Should(
                        m => m.Match(f => f.Field(fn => fn.Site)
                                            .Query(SiteName))
                        ).Filter(fc => fc.MultiMatch(mm => mm.Fields(
                                 f => f
                                         .Field(fn => fn.Title)
                                         .Field(fn => fn.Content)
                                         .Field(fn => fn.Description)
                                         .Field(fn => fn.PartNumbers)
                                          .Field(fn => fn.Site)
                             ).Query(queryTxt))))
                && q.DateRange(dr => dr.Field(f => f.Pubdate).GreaterThanOrEquals(StartDate).LessThanOrEquals(EndDate))
             ).Sort(x => x.Descending(y => y.Pubdate)));
```

---

<div class="post-metadata">

**Author:** ![Sravanthi\_N\_S\_CH](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sravanthi_n_s_ch/32/50473_2.png) [@Sravanthi\_N\_S\_CH](https://discuss.elastic.co/u/Sravanthi_N_S_CH)\
**Post date:** [April 6, 2018, 7:17am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/5 "2018-04-06T07:17:20Z")

</div>

If your use case is to filter the content based on Sitename, title and content etc..  
you can try something like

```
{
  "query": {
    "constant_score": {
      "filter": {
        "bool": {
           "must": [
             {
               "term": {
          "SITENAME.keyword": "true"
        }
             },
             {
               "term": {
            "title.keyword": {
              "value": "true"
            }
          }
             }
             ]
        }
      }
    }
  }
}

```

If this is not what you are looking for, can you please give more details about what your requirements are

---

<div class="post-metadata">

**Author:** ![subrato](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@subrato](https://discuss.elastic.co/u/subrato)\
**Post date:** [April 15, 2018, 12:11am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/6 "2018-04-15T00:11:59Z")

</div>

I will give this a try and see the NEST converted query works.

Thanks

---

<div class="post-metadata">

**Author:** ![subrato](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@subrato](https://discuss.elastic.co/u/subrato)\
**Post date:** [April 17, 2018, 3:25am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/7 "2018-04-17T03:25:00Z")

</div>

Hi @Sravanthi_N_S_CH how does the sitename.keyword (or for that matter any field.keyword) translate to NEST?

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [April 17, 2018, 4:15am UTC](https://discuss.elastic.co/t/combine-two-queries/126637/8 "2018-04-17T04:15:54Z")

</div>

> [@subrato](#):
>
> Hi @Sravanthi_N_S_CH how does the sitename.keyword (or for that matter any field.keyword) translate to NEST?

@subrato You can use either a string

```auto
.Field("sitename.keyword")

```

or an expression

```auto
.Field(f => f.Sitename.Suffix("keyword"))

```

Take a look at the [documentation on field inference.](https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/field-inference.html)

---

<div class="post-metadata">

**Author:** ![subrato](https://avatars.discourse-cdn.com/v4/letter/s/c4cdca/32.png) [@subrato](https://discuss.elastic.co/u/subrato)\
**Post date:** [April 30, 2018, 6:23pm UTC](https://discuss.elastic.co/t/combine-two-queries/126637/9 "2018-04-30T18:23:53Z")

</div>

Hi I dont think I understood what you were trying to say and was wondering if you take a look at the code and see what is missing (I know something is missing, one of them obviously me not assigning the values but not sure where to assign them in the query format you mentioned)

```
 var mediaResponse = elastic.Search<Articles>(s => s
            .Pretty(true)
            .Human(true)
            .Index(mediaIndexName)
            .Type(mediaTypeName)
            .From(pageNumber)
            .Size(numResults)
            .Query(
                q => q.ConstantScore(
                    filter => filter.Filter(b => b.Bool(
                          m => m.Must(t => t.Term(f => f.Field(fn => fn.Site.Suffix("keyword")
                     ).Field(fn => fn.Content))))))));

```

Where do I assign the values that I want to match to for site & content.

---

<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:** [May 28, 2018, 6:23pm UTC](https://discuss.elastic.co/t/combine-two-queries/126637/10 "2018-05-28T18:23:54Z")

</div>

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