Searching multiple fields, types, and indexes within php

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);

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')))
)
)
);

}

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