php – How to get documents from elasticsearch index

Question:

Please help me get data from the elastic index.

The situation is as follows – in the availability array, we have the availability of certain hotel rooms for every day (in the example, for 2 weeks).

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

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

The 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
                    )
            )

        )

     )

This is the request I was trying to send using the PHP client:

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)
                  }
                }
              }
            }
          }
        }
      }
    }
  }
}

Answer:

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

Better to use the following structure

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

In available_dates, write the dates on which the number is available.

Then you can get all hotels that have available rooms for certain dates like this:

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