# Query DSL and scoring is not working

**URL:** <https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563>\
**Category:** Elasticsearch\
**Created:** [October 21, 2016, 4:10am UTC](https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563 "2016-10-21T04:10:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [October 21, 2016, 4:10am UTC](https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563/1 "2016-10-21T04:10:11Z")

</div>

My cluster id is `cabdbc`

Have been trying to write a DSL query, but not able to get it work as expected. I am trying to find those documents, which matched following criteria in my ElasticSearch 2.3 storage and sort high to low score:

1. `rents.start` = "2016-09"  
AND
2. `rents.end` = "2016-10"  
AND
3. `rents.abs` is between 200000 and 350000  
AND
4. `los.start` = "2016-09"  
AND
5. `los.max` \>= 4  
AND
6. `bedroom_count` \>= 2 and \<=20 `<---for more than 2 rooms, per extra room I want to cut (penalize) the relevance score by 8point. E.g. if a document has bedroom_count=5, then score will be reduced by (5-2)*8`  
AND
7. `parking` contains `street` word

I believe from #1 to #6 should be part of `filters` while #6 and #7 will be part of the query.

Document mapping is:

```
{
  "properties": {
    "bedroom_count": {"type": "integer"},
    "parking": {"type": "string"},
    "rents": {
      "type": "nested",
      "dynamic": "strict",
      "properties": {
        "start": { "type": "date", "format": "yyyy-MM" },
        "end": { "type": "date", "format": "yyyy-MM" },
        "abs": { "type": "integer" },                    
      },                      
    "los": {                      
      "type": "nested",                     
      "dynamic": "strict",                      
      "properties": {                     
        "start": { "type": "date", "format": "yyyy-MM" },
        "max": { "type": "integer" },
        "min": { "type": "integer" }
      }                     
    }
  }
}

```

A sample doc:

```
{
  "bedroom_count": 4,
  "parking": "on the street",
  "rents": [
    {
      "abs": 250000,
      "start": "2016-09",
      "end": "2016-09"
    },
    {
      "abs": 250000,
      "start": "2016-09",
      "end": "2016-10"
    },
    {
      "abs": 250000,
      "start": "2016-09",
      "end": "2016-11"
    }
  ],
  "los": [
    {
      "min": 1,
      "max": 12,
      "start": "2016-09"
    },
    {
      "min": 1,
      "max": 12,
      "start": "2016-10"
    },
    {
      "min": 1,
      "max": 12,
      "start": "2016-11"
    }
  ]
}

```

And this is what I tried: (please remember `filtered` is deprecated in 2.x ref. [https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0](https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0) )

```
"query": {
  "bool": {
    "filter": {
      "nested": {
        "path": "rents",
        "score_mode": "none",
        "query": {
          "bool": {
            "must": [
              {
                "range": {
                  "rents.start": {"gte": "2016-09", "lte": "2016-09", } 
                }
              },
              {
                "range": {
                  "rents.end": {"gte": "2016-10", "lte": "2016-10", } 
                }
              },
              {
                "range": {
                  "rents.abs": {"gte": 200000, "lte": 350000}
                }
              },
              {
                "range": {
                  "los.start": {"gte": "2016-09", "lte": "2016-09"}
                }
              },
              {
                "range": {
                  "los.max": {"gte": 4}
                }
              }
            ]
          }
        }
      },
      "bool": {
        "range": {
          "bedroom_count": {
            "gte": 2, "lte": 20, "boost": 8
          }
        }
      }
    },
    "must": [
      {
        "match": { "parking":"street"}
      },
      {
        "range": {
          "bedroom_count": {
            "gte": 2, "lte": 20, "boost": 8
          }
        }
      }
    ]
  }
}

```

As you expected I am doing something wrong here. Hence it is not working at all. Any idea how to modify above query to get it work?

I get this error

```auto
{
   "error": {
      "root_cause": [
         {
            "type": "parse_exception",
            "reason": "Failed to derive xcontent"
         }
      ],
      "type": "search_phase_execution_exception",
      "reason": "all shards failed",
      "phase": "query_fetch",
      "grouped": true,
      "failed_shards": [
         {
            "shard": 0,
            "index": "listings_v1",
            "node": "TopkjPF_S9ueyzpGbF6epg",
            "reason": {
               "type": "parse_exception",
               "reason": "Failed to derive xcontent"
            }
         }
      ]
   },
   "status": 400
}

```

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [October 21, 2016, 8:10am UTC](https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563/2 "2016-10-21T08:10:50Z")

</div>

Your nested clause is too high in the query tree.  
Once you declare a `nested` part of a query all subclauses must pertain to the same nested object.  
In your example you have a single nested clause but mix criteria from nested `rents` and `los` objects where clearly a single object cannot satisfy all of the clauses you have mixed up in there.  
It needs to be: (psuedo code)

```
bool
  must
    nested
       bool
          must
             rent criteria 1                
             rent criteria 2                
    nested
       bool
          must
             los criteria 1                
             los criteria 2
```

---

<div class="post-metadata">

**Author:** ![pungent](https://avatars.discourse-cdn.com/v4/letter/p/a698b9/32.png) [@pungent](https://discuss.elastic.co/u/pungent)\
**Post date:** [October 24, 2016, 12:20am UTC](https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563/3 "2016-10-24T00:20:50Z")

</div>

Thanks @Mark_Harwood . I got it worked, after implementing your suggestion!!

---

<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:** [October 27, 2016, 12:21am UTC](https://discuss.elastic.co/t/query-dsl-and-scoring-is-not-working/63563/4 "2016-10-27T00:21:01Z")

</div>

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