# Help with Elasticsearch Query

**URL:** <https://discuss.elastic.co/t/help-with-elasticsearch-query/168489>\
**Category:** Elasticsearch\
**Created:** [February 14, 2019, 9:01pm UTC](https://discuss.elastic.co/t/help-with-elasticsearch-query/168489 "2019-02-14T21:01:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkrone72](https://avatars.discourse-cdn.com/v4/letter/j/34f0e0/32.png) [@jkrone72](https://discuss.elastic.co/u/jkrone72)\
**Post date:** [February 14, 2019, 9:01pm UTC](https://discuss.elastic.co/t/help-with-elasticsearch-query/168489/1 "2019-02-14T21:01:40Z")

</div>

I have a data structure that looks like this:

```
"dictionaryProximities": [
     {
       "proximity": 0.32427,
       "topicDictionaryName": "Electrical",
     },
     {
       "proximity": 0.32141,
       "topicDictionaryName": "Indoor Air Quality",
     },
     {
       "proximity": 0.7321,
       "topicDictionaryName": "Smart Home Technology",
     },

```

I want to create a filter/query that will return all files where the proximity of "Smart Home Technology" is greater than a value, say 0.7.

Right now I have the query:

```
{
  "query": {
    "nested": {
      "path": "dictionaryProximities",
      "query": {
        "match": {
          "dictionaryProximities.topicDictionaryName": {
            "query": "Smart Home Technology",
            "type": "phrase"
          }
        }
      }
    }
  }
} 

```

stacked with the query:

```
{
  "query": {
    "nested": {
      "path": "dictionaryProximities",
      "query": {
        "range": {
          "dictionaryProximities.proximity": {
            "gte": 0.7,
            "lt": 1
          }
        }
      }
    }
  }
}

```

This returns all files with proximity above 0.7 and contain the name Smart Home Technology. This is very different from what I want, which is all files that have a proximity above 0.7 ONLY for Smart Home Technology.

My question is, is there a way to combine these two queries in a way that achieves the result I want? Is this task even possible since proximity and Name are nested in the same level? Any help would be super appreciated.

---

<div class="post-metadata">

**Author:** ![gbrown](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/gbrown/32/34482_2.png) [@gbrown](https://discuss.elastic.co/u/gbrown)\
**Post date:** [February 15, 2019, 1:12am UTC](https://discuss.elastic.co/t/help-with-elasticsearch-query/168489/2 "2019-02-15T01:12:13Z")

</div>

I can tell from your queries that you're already using `nested` fields and queries, which is great, because that makes this possible - if your data was indexed as an array of `object`fields, rather than specifically as `nested`, this would not be possible.

Since you're already using a `nested` query, you'll just need to use a [`bool` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html) in the inner query with a `must` clause containing both the `match` and `range` queries. In fact, the example query on the [`nested` query documentation page](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html) is extremely similar to what you need, in that it combines a `match` query with a `range` query on a nested object field.

---

<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:** [March 15, 2019, 1:12am UTC](https://discuss.elastic.co/t/help-with-elasticsearch-query/168489/3 "2019-03-15T01:12:17Z")

</div>

Closed on March 15, 2019, 1:12am UTC

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