0

I have maintained one array of objects field which contains several objects, having two keys each but different values for those keys in each object and i want to run a range query along with a match_phrase query on each object but what is happening right now is that if a key is matched via match_phrase in 1st object and an other key is matched via the range query of 2nd object then both appear in result but i want to run both the queries respectively for each object.

1st POST request:

POST test/_doc
{
 "name": "yash",
 "score": [
{
  "model" : "swift",
  "score" : 5
},
{
  "model" : "alto",
  "score" : 6
},
{
  "model" : "xuv",
  "score" : 9
}
]
} 

Search Query:

 GET test/_search
 {
  "query": {
  "bool": {
   "must": [
    {
      "match_phrase": {
        "score.model": "swift"
      }
    },
    {
      "range": {
        "score.score": {
          "gte": 6,
          "lte": 9
        }
      }
    }
  ]
}
}
}

Actual Result:

"_index" : "test",
    "_type" : "_doc",
    "_id" : "g1LA12wBeamdnjKY5k-N",
    "_score" : 1.287682,
    "_source" : {
      "name" : "yash",
      "score" : [
        {
          "model" : "swift",
          "score" : 5
        },
        {
          "model" : "alto",
          "score" : 6
        },
        {
          "model" : "xuv",
          "score" : 9
        }
      ]
    }

Expected Result:

Nothing as the range of score in swift is not according to the specified value.

Yash Tandon
  • 345
  • 5
  • 18
  • this answer should help: https://stackoverflow.com/a/56730434/4604579 (hint: use `nested` field/query) – Val Aug 28 '19 at 11:20
  • this does not help as they are querying multiple fields but of different objects in an array, but i want to query multiple fields of a same object inside the array. so if u can suggest a different approach. – Yash Tandon Aug 28 '19 at 12:28
  • It works exactly the same way, the approach would work also for your needs. – Val Aug 28 '19 at 12:31
  • but can i create nested type dynamically as my use case does not allow me to have static mappings. – Yash Tandon Aug 28 '19 at 12:36
  • See my answer below – Val Aug 28 '19 at 12:39
  • I would request you to answer one more query of mine related to ES. link: https://stackoverflow.com/q/57704269/11432290 – Yash Tandon Aug 29 '19 at 09:48

1 Answers1

2

You need to define your score field as nested:

PUT test
{
  "mappings": {
    "properties": {
      "score": {
        "type": "nested"
      }
    }
  }
}

You can also create the nested field dynamically, by using a dynamic mapping that would transform all objects into nested type:

PUT test
{
  "mappings": {
    "dynamic_templates": [
      {
        "nested_objects": {
          "match_mapping_type": "object",
          "mapping": {
            "type": "nested"
          }
        }
      }
    ]
  }
}

Then you'll be able to query that field using a nested query and get the result you expect:

POST test/_search
{
  "query": {
    "nested": {
      "path": "score",
      "query": {
        "bool": {
          "must": [
            {
              "match_phrase": {
                "score.model": "swift"
              }
            },
            {
              "range": {
                "score.score": {
                  "gte": 6,
                  "lte": 9
                }
              }
            }
          ]
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360