Я пытаюсь отфильтровать гостиничные номера по ценовому диапазону в Elastic Search.Для комнат предусмотрена ночная цена по умолчанию, а для определенных дней могут быть установлены нестандартные цены.
Я храню nightlyPrice
и вложенный объект по индивидуальным ценам вместе с датами.Отображение смт.как:
{
"adverts": {
"mappings": {
"advert": {
"properties": {
"nightlyPrice": {"type": "float"},
"customPrices": {
"type": "nested",
"properties": {
"date": {"type": "date"},
"price": {"type": "float"}
}
}
}
}
}
}
}
Например, я хочу получить номера в диапазоне цен от 100 до 200 $ между датами 1 и 7 июля.
Так что я придумал эту логику:
- Либо
customPrices.date
должно быть между 2019-07-01 и 2019-07-07 и customPrices.price
между 100 и 200. - или
nightlyPrice
должно бытьмежду 100 и 200, и не устанавливается customPrices.date
между 05 и 07 июля.
Однако я не смог применить эту логику к Elastic Search, я думаю, что вложенные объекты / запросы довольно хитры.
Это последний запрос, с которым я столкнулся:
{
"query": {
"bool": {
"filter": [
{
"term": {
"status": "active"
}
}
],
"must": [
{
"bool": {
"should": [
{
"nested": {
"path": "customPrices",
"query": {
"bool": {
"must": [
{
"range": {
"date": {
"from": "2019-07-01",
"to": "2019-07-07"
}
}
},
{
"range": {
"price": {
"from": 100,
"to": 200
}
}
}
]
}
}
}
},
{
"bool": {
"must": [
{
"range": {
"nightlyPrice": {
"from": 100,
"to": 200
}
}
}
],
"must_not": [
{
"nested": {
"path": "customPrices",
"query": {
"range": {
"customPrices.date": {
"from": "2019-07-01",
"to": "2019-07-07"
}
}
}
}
}
]
}
}
]
}
}
]
}
}
}
Проблема с этим запросом заключается в том, что customPrices.date соответствует диапазону дат, который никогда не соответствует документу, независимо от диапазона цен.является.Я экспериментировал с ценовым диапазоном 1 - 100000 $, и он все еще не совпадает.
Пытался использовать API объяснения, чтобы понять, почему конкретный документ не соответствует, но я не понимаю его, он говорит user requested match_none
запрос, но есть этот should
запрос, поэтому он должен соответствовать вложенному запросу (первый):
{
"_index": "adverts",
"_type": "advert",
"_id": "13867",
"matched": false,
"explanation": {
"value": 0.0,
"description": "Failure to meet condition(s) of required/prohibited clause(s)",
"details": [
{
"value": 0.0,
"description": "no match on required clause (+(ToParentBlockJoinQuery (MatchNoDocsQuery(\"User requested \"match_none\" query.\")) (+nightlyPrice:[100.0 TO 200.0] -ToParentBlockJoinQuery (customListingPrices.date:[1561939200000 TO 1562543999999]))) #status:active",
"details": [
{
"value": 0.0,
"description": "Failure to meet condition(s) of required/prohibited clause(s)",
"details": [
{
"value": 0.0,
"description": "no match on required clause (ToParentBlockJoinQuery (MatchNoDocsQuery(\"User requested \"match_none\" query.\")) (+nightlyPrice:[100.0 TO 200.0] -ToParentBlockJoinQuery (customListingPrices.date:[1561939200000 TO 1562543999999])))",
"details": [
{
"value": 0.0,
"description": "No matching clauses",
"details": []
}
]
},
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 1.0,
"description": "status:active",
"details": []
}
]
}
]
}
]
},
{
"value": 0.0,
"description": "match on required clause, product of:",
"details": [
{
"value": 0.0,
"description": "# clause",
"details": []
},
{
"value": 1.0,
"description": "DocValuesFieldExistsQuery [field=_primary_term]",
"details": []
}
]
}
]
}
}
Любая помощь или идея приветствуется ...