Help please get data from the elastic index.

The situation is the following - in the availability array we have the availability of certain hotel rooms for each day (in the example for 2 weeks).

The keys in the array are id numbers. The array values ​​are nested arrays with key-date and availability (1 - available for booking, 0 - not available).

It is necessary to send a request so that the hotels, which have certain days, at least in one of the rooms, have the value 1 (available).

Documents have the following mapping:

[_index] => placement [_type] => hotel [_id] => 26 [_version] => 15 [found] => 1 [_source] => Array ( [id] => 26 [name] => Гостиница 1 [type_id] => 1 [review_count] => 65 [order_count] => 429 [spa_id] => 3 [object_id] => 4 [spa] => 3 [availability] => Array ( [258] => Array ( [2016-03-30] => 0 [2016-03-31] => 1 [2016-04-01] => 0 [2016-04-02] => 0 [2016-04-03] => 1 [2016-04-04] => 1 [2016-04-05] => 0 [2016-04-06] => 0 [2016-04-07] => 0 [2016-04-08] => 1 [2016-04-09] => 0 [2016-04-10] => 1 [2016-04-11] => 1 [2016-04-12] => 1 [2016-04-13] => 1 ) [259] => Array ( [2016-03-30] => 0 [2016-03-31] => 1 [2016-04-01] => 0 [2016-04-02] => 0 [2016-04-03] => 0 [2016-04-04] => 1 [2016-04-05] => 0 [2016-04-06] => 0 [2016-04-07] => 0 [2016-04-08] => 1 [2016-04-09] => 1 [2016-04-10] => 1 [2016-04-11] => 1 [2016-04-12] => 1 [2016-04-13] => 1 ) [260] => Array ( [2016-03-30] => 0 [2016-03-31] => 0 [2016-04-01] => 0 [2016-04-02] => 0 [2016-04-03] => 0 [2016-04-04] => 0 [2016-04-05] => 0 [2016-04-06] => 0 [2016-04-07] => 0 [2016-04-08] => 1 [2016-04-09] => 1 [2016-04-10] => 1 [2016-04-11] => 1 [2016-04-12] => 1 [2016-04-13] => 1 ) ) ) ) 

Here is the query I tried to send using the client for PHP:

 array(4) { ["index"]=> string(9) "placement" ["type"]=> string(5) "hotel" ["size"]=> int(1000) ["body"]=> array(1) { ["query"]=> array(1) { ["bool"]=> array(1) { ["must"]=> array(3) { [0]=> array(1) { ["term"]=> array(1) { ["spa_id"]=> int(3) } } [2]=> array(1) { ["bool"]=> array(1) { ["must"]=> array(5) { [0]=> array(1) { ["term"]=> array(1) { ["availability.2016-04-09"]=> int(1) } } [1]=> array(1) { ["term"]=> array(1) { ["availability.2016-04-10"]=> int(1) } } [2]=> array(1) { ["term"]=> array(1) { ["availability.2016-04-11"]=> int(1) } } [3]=> array(1) { ["term"]=> array(1) { ["availability.2016-04-12"]=> int(1) } } [4]=> array(1) { ["term"]=> array(1) { ["availability.2016-04-13"]=> int(1) } } } } } } } } } } 
  • There is a suspicion that, pointing out several dates in the block, you want ALL conditions to be fulfilled. And at least in one - this should be for me. In general, they wrote down the mapping and data here: found.no/play Then you can see. Mapping is important: what fields do you index and how. You brought the data and request, and mapping is not - Ololo

1 answer 1

Without mapping it is difficult to give a uniquely correct solution. Apparently the field availability is used as an object ({"type": "object"}). In this case, you will have to list all possible numbers in bool → should. Those. need to know all the numbers, and this is bad.

It is better to use the following structure.

 "availability": { "type": "nested", "properties": { "room_number": { "type": "integer" }, "available_dates": { "type": "array" }, } } 

In available_dates record the dates on which the number is available.

Then get all the hotels in which there are rooms available on certain dates, you can:

 "nested":{ "path":"availability", "query":{ "bool":{ "filter":[ { "terms": { "availability.available_dates": [ "2016-04-10", "2016-04-11" ] } } ] } } }