# Newbie: 2 Different Queries on 1 PHP Page-Multi Search?

**URL:** <https://discuss.elastic.co/t/newbie-2-different-queries-on-1-php-page-multi-search/252725>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [October 20, 2020, 6:18pm UTC](https://discuss.elastic.co/t/newbie-2-different-queries-on-1-php-page-multi-search/252725 "2020-10-20T18:18:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![zi94sm65](https://avatars.discourse-cdn.com/v4/letter/z/6f9a4e/32.png) [@zi94sm65](https://discuss.elastic.co/u/zi94sm65)\
**Post date:** [October 20, 2020, 6:18pm UTC](https://discuss.elastic.co/t/newbie-2-different-queries-on-1-php-page-multi-search/252725/1 "2020-10-20T18:18:24Z")

</div>

I am running elasticsearch by itself on localhost (no graphical user interface)  
`"number" : "7.9.2", "lucene_version" : "8.6.2", "minimum_wire_compatibility_version" : "6.8.0", "minimum_index_compatibility_version" : "6.0.0-beta1"`

I have a PHP page with a search box where user can type in a word to search on one particular field (catby), or click a button to just return all (match\_all) records. Searching same index.

I've implemented the searches separately with the following PHP code:

```
/* *********************************************************************** */
/* If user clicks show all button run this search: */
/* *********************************************************************** */
if(isset($_GET['q']) && ($_GET['q']=='match_all') ) {
$params = [
  'index' => 'ppcatalogs',
  'track_total_hits' => true,
  'size' => $items_per_page,
  'from' => $offset,
  'body' => [
  'query' => [ 
  'bool' => [
  'must' => ['match_all' => (object)[] // this cast is necessary!
  ]
  ]
  ]
  ]
];
$results = $client->search($params);

$totalsearchhits = $results['hits']['total']['value'];
$totalPages = ceil($totalsearchhits / $items_per_page);

if($results['hits']['total'] >=1 ) {
$list = $results['hits']['hits'];
$q='match_all';
}
}
/* *********************************************************************** */
/* If user types something in search box run this search: */
/* *********************************************************************** */
if(isset($_GET['q']) && ($_GET['q']!=='match_all') ) {
$q = $_GET['q'];
$query = $client->search([
'track_total_hits' => true,
'size' => $items_per_page,
'from' => $offset,
'body' => [
'query' => [ 
'bool' => [
'should' => [
'match' => ['catby' => $q],
]
]
]
]
]);

$totalsearchhits = $query['hits']['total']['value'];
$totalPages = ceil($totalsearchhits / $items_per_page);

if($query['hits']['total'] >=1 ) {
$list = $query['hits']['hits'];
}
}

```

This works, but probably not the best approach. I believe (correct me if i'm wrong) that i should be able to use multi search?

However, i can't find any elasticsearch&PHP tutorials on how you would write the query, and then access the correct data in a for loop for the separate searches after it is returned.

I'd appreciate some guidance, or a link to a good beginners tutorial, on proper ways to implement multiple searches.

---

<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 17, 2020, 6:18pm UTC](https://discuss.elastic.co/t/newbie-2-different-queries-on-1-php-page-multi-search/252725/2 "2020-11-17T18:18:39Z")

</div>

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