Hi @bender268,
based on what I see here everything is fine. Maybe there is some problem in your data?
Here is a complete example based on Elasticsearch 5.1.1 (just replace the keyword
field with a not_analyzed
string field if you use ES 2.x):
DELETE /immo
PUT /immo
{
"mappings": {
"flat": {
"properties": {
"address": {
"type": "keyword"
},
"rooms": {
"type": "integer"
},
"price": {
"type": "float"
}
}
}
}
}
# Create a few matching (and also non-matching flats)
POST /immo/flat
{
"address": "Munich",
"rooms": 3,
"price": "500000",
"zhk_id": 42
}
POST /immo/flat
{
"address": "Munich",
"rooms": 1,
"price": "200000",
"zhk_id": 43
}
POST /immo/flat
{
"address": "Munich",
"rooms": 3,
"price": "700000"
}
POST /immo/flat
{
"address": "Munich",
"rooms": 3,
"price": "800000"
}
POST /immo/flat
{
"address": "Munich",
"rooms": 3,
"price": "700000",
"zhk_id": 48
}
POST /immo/flat
{
"address": "Munich",
"rooms": 3,
"price": "336000",
"zhk_id": 88
}
If I run a slightly modified version of your query (adapted to the simplified example and the 5.x syntax):
GET /immo/flat/_search
{
"query": {
"dis_max": {
"queries": [
{
"function_score": {
"query": {
"bool": {
"filter": [
{
"term": {
"rooms": 3
}
},
{
"term": {
"address": "Munich"
}
},
{
"bool": {
"must_not": {
"exists": {
"field": "zhk_id"
}
}
}
}
]
}
},
"script_score": {
"script": "doc['price'].value / 100000"
},
"boost_mode": "replace"
}
},
{
"function_score": {
"query": {
"bool": {
"filter": [
{
"term": {
"rooms": 3
}
},
{
"term": {
"address": "Munich"
}
},
{
"exists": {
"field": "zhk_id"
}
}
]
}
},
"script_score": {
"script": "doc['price'].value / 100000"
},
"boost_mode": "replace"
}
}
]
}
}
}
I get reasonable scores:
{
"took": 6,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 5,
"max_score": 8,
"hits": [
{
"_index": "immo",
"_type": "flat",
"_id": "AVlvVc1FYSS4vzD5IS5l",
"_score": 8,
"_source": {
"address": "Munich",
"rooms": 3,
"price": "800000"
}
},
{
"_index": "immo",
"_type": "flat",
"_id": "AVlvVdtgYSS4vzD5IS5m",
"_score": 7,
"_source": {
"address": "Munich",
"rooms": 3,
"price": "700000",
"zhk_id": 48
}
},
{
"_index": "immo",
"_type": "flat",
"_id": "AVlvVcIuYSS4vzD5IS5k",
"_score": 7,
"_source": {
"address": "Munich",
"rooms": 3,
"price": "700000"
}
},
{
"_index": "immo",
"_type": "flat",
"_id": "AVlvVaXZYSS4vzD5IS5i",
"_score": 5,
"_source": {
"address": "Munich",
"rooms": 3,
"price": "500000",
"zhk_id": 42
}
},
{
"_index": "immo",
"_type": "flat",
"_id": "AVlvViw9YSS4vzD5IS5n",
"_score": 3.36,
"_source": {
"address": "Munich",
"rooms": 3,
"price": "336000",
"zhk_id": 88
}
}
]
}
}
By the way, I don't think you need dis_max for the query as you posted it but I guess the original one is more complex which is why you want to use it.
Daniel