# PHP Search entries according to year indexed

**URL:** https://discuss.elastic.co/t/php-search-entries-according-to-year-indexed/122409
**Category:** Elasticsearch
**Created:** [March 4, 2018, 3:12pm UTC](https://discuss.elastic.co/t/php-search-entries-according-to-year-indexed/122409 "2018-03-04T15:12:37Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![syiannop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syiannop/32/26920_2.png) [@syiannop](https://discuss.elastic.co/u/syiannop)
#### Post date: [March 4, 2018, 3:12pm UTC](https://discuss.elastic.co/t/php-search-entries-according-to-year-indexed/122409/1 "2018-03-04T15:12:37Z")

</div>

I am using the PHP client to search and index documents into ES. I created a button with different values (2018, 2017, 2016) so that a user can find entries according to the date the entries were made (for example: view results for all entries in year 2016)

I separated the ES query using else-if statement, but it doesn't work. Here is the code:

```
if(isset($_GET['titel'], $_GET['beschreibung'], $_GET['thema'], $_GET['beitragstyp']/*, $_GET['veröffentlicht']*/, $_GET['date'])) {
      $titel = $_GET['titel'];
      $beschreibung = $_GET['beschreibung'];
      $thema = $_GET['thema'];
      $beitragstyp = $_GET['beitragstyp'];
      $date = $_GET['date'];

  if ($date === "2018") {
    $query = $client->search([
      'index' => 'my_index',
      'type' => 'default',
      'body' => [
        'query' => [
          'bool' => [
            'should' => [
              ['match_phrase' => ['Titel' => $titel]],
              ['match_phrase' => ['Beschreibung' => $beschreibung]],
              ['match_phrase' => ['Thema' => $thema]],
              ['match_phrase' => ['Beitragstyp' => $beitragstyp]],
              ['range' => [
                'Datum' => [
                  'gte' => '2018-01-01',
                  'lte' => '2018-12-31',
                  'format' => 'yyyy-MM-dd'
                ]
              ]]
            ]
          ]
        ]
      ]
    ]);
  } else if ($date === "2017") {
    $query = $client->search([
      'index' => 'my_index',
      'type' => 'default',
      'body' => [
        'query' => [
          'bool' => [
            'should' => [
              ['match_phrase' => ['Titel' => $titel]],
              ['match_phrase' => ['Beschreibung' => $beschreibung]],
              ['match_phrase' => ['Thema' => $thema]],
              ['match_phrase' => ['Beitragstyp' => $beitragstyp]],
              ['range' => [
                'Datum' => [
                  'gte' => '2017-01-01',
                  'lte' => '2017-12-31',
                  'format' => 'yyyy-MM-dd'
                ]
              ]]
            ]
          ]
        ]
      ]
    ]);
  } else if ($date === "2016") {
    $query = $client->search([
      'index' => 'my_index',
      'type' => 'default',
      'body' => [
        'query' => [
          'bool' => [
            'should' => [
              ['match_phrase' => ['Titel' => $titel]],
              ['match_phrase' => ['Beschreibung' => $beschreibung]],
              ['match_phrase' => ['Thema' => $thema]],
              ['match_phrase' => ['Beitragstyp' => $beitragstyp]],
              ['range' => [
                'Datum' => [
                  'gte' => '2016-01-01',
                  'lte' => '2016-12-31',
                  'format' => 'yyyy-MM-dd'
                ]
              ]]
            ]
          ]
        ]
      ]
    ]);
  } else {
    $query = $client->search([
      'index' => 'my_index',
      'type' => 'default',
      'body' => [
        'query' => [
          'bool' => [
            'should' => [
              ['match_phrase' => ['Titel' => $titel]],
              ['match_phrase' => ['Beschreibung' => $beschreibung]],
              ['match_phrase' => ['Thema' => $thema]],
              ['match_phrase' => ['Beitragstyp' => $beitragstyp]],
            ]
          ]
      ]
    ]
  ]);
  }

```

And this is how I am trying to get the different year values:

```
<select class="publishedDropdown" name="date">
    <option value="Empty">Date</option>
    <option value="2018">2018</option>
    <option value="2017">2017</option>
    <option value="2016">2016</option>
</select>
```

---

<div class="post-metadata">

### Author: ![syiannop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/syiannop/32/26920_2.png) [@syiannop](https://discuss.elastic.co/u/syiannop)
#### Post date: [March 11, 2018, 11:25am UTC](https://discuss.elastic.co/t/php-search-entries-according-to-year-indexed/122409/2 "2018-03-11T11:25:19Z")

</div>

**EDITED:** It was a simple mistake. I changed the "=" to "===" in the if statement and it works.

---

<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: [April 8, 2018, 11:25am UTC](https://discuss.elastic.co/t/php-search-entries-according-to-year-indexed/122409/3 "2018-04-08T11:25:20Z")

</div>

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