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

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.

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