Help with Elasticsearch Query


#1

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.


(Gordon Brown) #2

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 objectfields, 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 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 is extremely similar to what you need, in that it combines a match query with a range query on a nested object field.


(system) closed #3

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