# Searching multiple fields, types, and indexes within php

**URL:** <https://discuss.elastic.co/t/searching-multiple-fields-types-and-indexes-within-php/204935>\
**Category:** Elasticsearch\
**Created:** [October 23, 2019, 6:19pm UTC](https://discuss.elastic.co/t/searching-multiple-fields-types-and-indexes-within-php/204935 "2019-10-23T18:19:33Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![prawsthorne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prawsthorne/32/50777_2.png) [@prawsthorne](https://discuss.elastic.co/u/prawsthorne)\
**Post date:** [October 23, 2019, 6:19pm UTC](https://discuss.elastic.co/t/searching-multiple-fields-types-and-indexes-within-php/204935/1 "2019-10-23T18:19:33Z")

</div>

I wanting to query one term across multiple indexes, types, and fields. I've been reading and experimenting with no working solution... yet.

It's time to ask.

It works until I un-comment the second match clause. Thinking I need an OR somewhere, don't know if this is even possible. The "post" type is for the wordpress index, the "oa\_basic" type is for the other three indexes. post\_content and content fields map to respective types and indexes. Hope this explanation makes sense...

What have I missed or where should I be reading? Thank-you

My php is code is as follows;

q = \_GET['q'];  
$params = array();  
$params['size'] = 50;  
$params['index'] = 'oa-assn-20oct2019,oa-prtnr-20oct2019,oa-mastr-21oct2019,oa-1382185244wordpress-post-1';  
$params['type'] = 'oa\_basic,post';  
$params['body']['query']['bool']['should']['match']['content'] = $q;  
// $params['body']['query']['bool']['should']['match']['post\_content'] = $q;  
}

print\_r(json\_encode($params['body']));  
$response = $es-\>search($params);

---

<div class="post-metadata">

**Author:** ![prawsthorne](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/prawsthorne/32/50777_2.png) [@prawsthorne](https://discuss.elastic.co/u/prawsthorne)\
**Post date:** [October 28, 2019, 12:41pm UTC](https://discuss.elastic.co/t/searching-multiple-fields-types-and-indexes-within-php/204935/2 "2019-10-28T12:41:52Z")

</div>

I answered this myself. A couple of issues with the code;

1. php would have been overwriting the contents of the first ['body'] array element with the second. just the way php works...
2. I built the query in two ways; one setting array elements directly and the second where I build the array as the query clauses required. (see query code below)
3. I also needed to add an AND to remove the trashed wordpress posts, you can see that with the 'must\_not' syntax (again, see query below)

So there you have it. The query gets results across fields, types, and indexes. And the relevancy score sets the order across these three... all good. Thanks for your eyes!

```
if(isset($_GET['q'])) {
    $q = $_GET['q'];
    }
if ($q != ''){

```

// Prepare to fetch the content of the oa\* indexes  
$params = array();  
$params['size'] = 50;  
$params['index'] = 'oa-assn-20oct2019,oa-prtnr-20oct2019,oa-mastr-21oct2019,oa-1382185244wordpress-post-1';  
$params['type'] = 'oa\_basic,post';  
$params['body'] = array('query' =\> array(  
'bool' =\> array(  
'should' =\> array(  
array('match' =\> array('content' =\> $q)),  
array('match' =\> array('post\_content' =\> $q))),  
'must\_not' =\> array(  
array('match' =\> array('post\_status' =\> 'trash')))  
)  
)  
);

```
}
```

---

<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:** [November 25, 2019, 12:55pm UTC](https://discuss.elastic.co/t/searching-multiple-fields-types-and-indexes-within-php/204935/3 "2019-11-25T12:55:10Z")

</div>

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