# Filtering based on whether array passed into search query is empty

**URL:** https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275
**Category:** Elasticsearch
**Created:** [May 17, 2018, 7:56am UTC](https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275 "2018-05-17T07:56:18Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![yckoh](https://avatars.discourse-cdn.com/v4/letter/y/b19c9b/32.png) [@yckoh](https://discuss.elastic.co/u/yckoh)
#### Post date: [May 17, 2018, 7:56am UTC](https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275/1 "2018-05-17T07:56:18Z")

</div>

Hi. I would like to perform a search query based on passing in an array of values to the search function in php. This array may or may not be an empty array. I would like to set the filter in such a way that it compares each element in the array against the chosen parameter, but allows everything to pass if an empty array is passed in. Currently what i have here is searching for an empty string in the chosen parameter, which therefore returns me no results (since I have no items will color = null). May I know how this can be done?

```
    $a = array('');

$query = $client->search([
    'index' => 'fruits',
    'type' => 'fruit',
    'from' => 0, 'size' => 9999,
    'body' => [
        'query' => [
            'bool' => [
                'should' => [
                    ['match' => ['color' => ['query' => $q, 'boost' => 3]]],
                    ['match' => ['name' => ['query' => $q, 'boost' => 0.5]]],
                ],
                'filter' => [
                    ['terms' => ['color' => $a]]
                ]
                ]
                ],
                ]]
]);
```

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [May 17, 2018, 4:23pm UTC](https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275/2 "2018-05-17T16:23:38Z")

</div>

There may be a clever way to do this in Elasticsearch (I haven't thought about it very much), but honestly this is something best done in your application.

E.g. if the array is non-empty, add the filter:

```auto
$query = [
    'index' => 'fruits',
    'type' => 'fruit',
    'from' => 0, 'size' => 9999,
    'body' => [
        'query' => [
            'bool' => [
                'should' => [
                    ['match' => ['color' => ['query' => $q, 'boost' => 3]]],
                    ['match' => ['name' => ['query' => $q, 'boost' => 0.5]]],
                ],
                'filter' => []
                ]
                ],
                ]]
]);

if (count($a) > 0) {
  $query['body']['bool']['filter'][] = ['terms' => ['color' => $a]];
}

$response = = $client->search($query);

```

---

<div class="post-metadata">

### Author: ![yckoh](https://avatars.discourse-cdn.com/v4/letter/y/b19c9b/32.png) [@yckoh](https://discuss.elastic.co/u/yckoh)
#### Post date: [May 19, 2018, 6:37am UTC](https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275/3 "2018-05-19T06:37:35Z")

</div>

thanks, Zachary!

---

<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: [June 16, 2018, 6:37am UTC](https://discuss.elastic.co/t/filtering-based-on-whether-array-passed-into-search-query-is-empty/132275/4 "2018-06-16T06:37:42Z")

</div>

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