Created
August 11, 2015 09:21
-
-
Save rsmarshall/6902fb0f19fb55f3784b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns an array of products IDs for products matching the search query | |
* | |
* @param string $query | |
* @return array | |
*/ | |
public function search($query) | |
{ | |
if (is_numeric($query)) { | |
$return = $this->_client->search( | |
array( | |
'index' => self::INDEX, | |
'type' => self::TYPE, | |
'body' => array( | |
'query' => array( | |
'multi_match' => array( | |
'query' => $query, | |
'fields' => array('id', 'barcodes'), | |
), | |
), | |
'size' => '5000', | |
), | |
) | |
); | |
} else { | |
$search = array( | |
'index' => self::INDEX, | |
'type' => self::TYPE, | |
); | |
$words = explode(' ', $query); | |
foreach ($words AS $word) { | |
$search['body']['query']['bool']['should'][] = array('wildcard' => array( | |
'name' => array( | |
'value' => $word.'*', | |
), | |
) | |
); | |
$search['body']['query']['bool']['should'][] = array('wildcard' => array( | |
'brand' => array( | |
'value' => $word.'*', | |
'boost' => '10', | |
), | |
) | |
); | |
} | |
$search['body']['size'] = 5000; | |
$return = $this->_client->search($search); | |
} | |
dd($return); | |
$productIds = array(); | |
if (!empty($return['hits']['hits'])) { | |
foreach ($return['hits']['hits'] as $hit) { | |
$productIds[] = $hit['_id']; | |
} | |
} | |
return $productIds; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment