本文整理汇总了PHP中Elasticsearch\Client::search方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::search方法的具体用法?PHP Client::search怎么用?PHP Client::search使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Elasticsearch\Client
的用法示例。
在下文中一共展示了Client::search方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: main
public function main(Request $req, Application $app)
{
$client = new Client();
$params = array('index' => 'hrqls', 'type' => 'houseData', 'body' => ['from' => 0, 'size' => 100, 'filter' => array('range' => array('avgHomeValueIndex' => array('gte' => 0))), 'query' => ['match' => ['state' => 'Virginia']]]);
$results = $client->search($params)['hits']['hits'];
$responseObject = [];
$averageHouseValue = array('total' => 0, 'number' => 0);
$averageTurnover = array('total' => 0, 'number' => 0);
$maxHouseValue = 0;
$minHouseValue = 900000;
foreach ($results as $zip) {
$averageHouseValue['total'] += $zip['_source']['avgHomeValueIndex'];
$averageHouseValue['number']++;
$averageTurnover['total'] += $zip['_source']['turnoverWithinLastYear'];
$averageTurnover['number']++;
if ($zip['_source']['avgHomeValueIndex'] > $maxHouseValue) {
$maxHouseValue = $zip['_source']['averageHouseValue'];
}
if ($zip['_source']['averageHouseValue'] < $minHouseValue) {
$minHouseValue = $zip['_source']['averageHouseValue'];
}
}
$averageHouse = $averageHouseValue['total'] / $averageHouseValue['number'];
$averageTurn = $averageTurnover['total'] / $averageTurnover['number'];
$slidervalue = $req->get('slidervalue');
foreach ($results as $zip) {
$sliderInfo = $this->calculate($slidervalue);
$weight = $this->determineWeight($sliderInfo, $zip['_source']['avgHomeValueIndex'], $averageHouse, $maxHouseValue, $minHouseValue);
$responseObject[] = array('lat' => $zip['_source']['location']['lat'], 'lon' => $zip['_source']['location']['lon'], 'weight' => $weight);
}
return new Response(json_encode($responseObject), 200);
}
示例2: findProducts
public function findProducts(ListingFilter $filter)
{
$must = [];
if ($filter->getEshop() !== null) {
$must[] = ["term" => [ProductMeta::ESHOP_ID => (string) $filter->getEshop()->getId()]];
}
if ($filter->getCategory() !== null) {
/** @var Category[] $childrenCategories */
$childrenCategories = $this->categoryRepository->find([CategoryMeta::PATH => $filter->getCategory()->getId()]);
$must[] = ["terms" => [ProductMeta::CATEGORY_IDS => array_merge([(string) $filter->getCategory()->getId()], array_map(function (Category $category) {
return (string) $category->getId();
}, $childrenCategories))]];
}
$body = ["query" => ["filtered" => ["filter" => ["bool" => ["must" => $must]]]], "from" => $filter->getOffset(), "size" => $filter->getLimit(), "sort" => ["_score" => "desc"]];
if ($filter->getQ() !== null) {
$body["query"]["filtered"]["query"] = ["multi_match" => ["query" => $filter->getQ(), "fields" => [ProductMeta::NAME . "^5", ProductMeta::LONG_NAME . "^5", ProductMeta::DESCRIPTION, ProductMeta::MANUFACTURER . "^2", ProductMeta::BRAND . "^2", ProductMeta::ESHOP . "." . EshopMeta::NAME . "^2"]]];
}
// if (empty($body["query"]["filtered"]["filter"]["bool"]["must"])) {
// unset($body["query"]["filtered"]["filter"]);
// }
$response = $this->elasticsearch->search(["index" => $this->catalogIndexAliasName, "type" => ProductMeta::SHORT_NAME, "body" => $body]);
if (!isset($response["hits"]["hits"])) {
throw new \RuntimeException("Response does not have hits->hits. Got: " . json_encode($response));
}
$products = [];
foreach ($response["hits"]["hits"] as $hit) {
$products[] = ProductMeta::fromArray($hit["_source"], "json:");
}
return $products;
}
示例3: searchMarkdownDocuments
/**
* @param $searchString
* @return MarkdownSearchResult[]
*/
public function searchMarkdownDocuments($searchString)
{
$params = array('index' => $this->index, 'type' => self::MARKDOWN_DOCUMENT_TYPE, 'fields' => array('title'));
$searchStringParts = explode(' ', $searchString);
foreach ($searchStringParts as $searchStringPart) {
$params['body']['query']['bool']['should'][]['wildcard']['content'] = $searchStringPart . '*';
}
$result = $this->client->search($params);
$numHits = $result['hits']['total'];
if ($numHits == 0) {
return array();
}
$searchResults = array();
foreach ($result['hits']['hits'] as $hit) {
$searchResult = new MarkdownSearchResult();
$searchResult->setPath(FilePath::parse($hit['_id']));
$searchResult->setScore($hit['_score']);
if (isset($hit['fields'])) {
if (isset($hit['fields']['title'][0])) {
$searchResult->setTitle($hit['fields']['title'][0]);
}
}
$searchResults[] = $searchResult;
}
return $searchResults;
}
示例4: search
/**
* {@inheritdoc}
*/
public function search(Searchable $model, $query)
{
if (!is_array($query) && ($field = $model->getDefaultSearchField())) {
$query = [$field => $query];
}
return $this->client->search($this->buildSearchQuery($model, $query));
}
示例5: search
public function search($needle, $index, array $types, array $options = [])
{
$response = $this->client->search(['index' => $index, 'type' => $types, 'body' => ['query' => ['multi_match' => ['query' => $needle, 'type' => 'phrase_prefix', 'fields' => ['_all']]]]]);
$hits = array_map(function ($hit) {
return ['id' => $hit['_id'], 'index' => $hit['_index'], 'type' => $hit['_type'], 'data' => $hit['_source']];
}, $response['hits']['hits']);
return new SearchResult($hits, $response['hits']['total']);
}
示例6: search
/**
* 查询 结果
*
* @param string $index 索引
* @param string $type 类型
* @param string $body 查询字符串
* @param array $attrs 额外查询参数
*
* @return mixed
*/
public function search($index, $type, $body, $attrs = [])
{
$query = ['index' => $index, 'type' => $type, 'body' => $body];
if (!empty($attrs)) {
$query = array_merge($attrs, $query);
}
return $this->esClient->search($query);
}
示例7: getParameters
/**
*
* {@inheritdoc}
*
*/
public function getParameters(View $view, FormFactoryInterface $formFactoty, Request $request)
{
$searchQuery = ['index' => $view->getContentType()->getEnvironment()->getAlias(), 'type' => $view->getContentType()->getName(), 'search_type' => 'count', 'body' => $view->getOptions()['aggsQuery']];
$retDoc = $this->client->search($searchQuery);
foreach (explode('.', $view->getOptions()['pathToBuckets']) as $attribute) {
$retDoc = $retDoc[$attribute];
}
return ['keywords' => $retDoc, 'view' => $view, 'contentType' => $view->getContentType(), 'environment' => $view->getContentType()->getEnvironment()];
}
示例8: getFacet
/**
* load all options of a facet
*
* @param array $facet
* @return array
*/
public function getFacet($facet)
{
$params = array_replace($this->defaultParams, ['size' => 0, 'body' => ["aggs" => ["facet" => ["terms" => ["field" => $facet['field']]]]]]);
$results = $this->client->search($params);
$options = array();
foreach ($results['aggregations']['facet']['buckets'] as $bucket) {
$options[] = array('value' => $bucket['key'], 'count' => $bucket['doc_count']);
}
return $options;
}
示例9: search
/**
* @param array $terms
*
* @return array
*/
public function search(array $terms, $from = 0, $size = 25, $group = false)
{
$params = ['index' => env('ES_INDEX'), 'type' => $this->type, 'body' => ['from' => $from, 'size' => $size, 'query' => ['bool' => ['must' => [], 'should' => ['multi_match' => ['query' => $terms['keyword'], 'fields' => ["scanSpecimenSpeciesCommonName", "scanSpecimenSpeciesScientificName"]]]]]]];
foreach ($terms['filter'] as $key => $value) {
$params['body']['query']['bool']['must'][] = ['match' => [$key => $value]];
}
if ($group) {
$params['body']['aggs'] = ["top-scanIds" => ["terms" => ["field" => "scanScanId"], "aggs" => ["top_scanIds_hits" => ["top_hits" => ["sort" => [["_score" => ["order" => "desc"]]], "size" => 1]]]]];
}
return $this->client->search($params);
}
示例10: glass
/**
* @param $glassNamePartial
* @param int $limit
* @return array []
*/
public function glass($glassNamePartial, $limit = 10)
{
$params = ['index' => ElasticSearch::INDEX, 'type' => 'supply', 'body' => ['query' => ['match_phrase_prefix' => ['polishName' => ['query' => $glassNamePartial, 'max_expansions' => 10]]], 'filter' => ['prefix' => ['_id' => 'glass.']]]];
$results = $this->client->search($params);
if ($results['hits']['total'] === 0) {
return [];
}
$liquids = [];
foreach ($results['hits']['hits'] as $result) {
$liquids[] = new Supply($result['_id'], $result['_source']['polishName']);
}
return $liquids;
}
示例11: searchBlog
private function searchBlog(Criteria $criteria, Struct\ProductContextInterface $context)
{
/**@var $condition SearchTermCondition*/
$condition = $criteria->getCondition('search');
$query = $this->createMultiMatchQuery($condition);
$search = new Search();
$search->addQuery($query);
$search->setFrom(0)->setSize(5);
$index = $this->indexFactory->createShopIndex($context->getShop());
$params = ['index' => $index->getName(), 'type' => 'blog', 'body' => $search->toArray()];
$raw = $this->client->search($params);
return $this->createBlogStructs($raw);
}
示例12: query
/**
* {@inheritdoc}
*/
public function query($string, $offset, $perPage, SearchEngineOptions $options = null)
{
$options = $options ?: new SearchEngineOptions();
$context = $this->context_factory->createContext($options);
/** @var QueryCompiler $query_compiler */
$query_compiler = $this->app['query_compiler'];
$recordQuery = $query_compiler->compile($string, $context);
$params = $this->createRecordQueryParams($recordQuery, $options, null);
$params['body']['from'] = $offset;
$params['body']['size'] = $perPage;
if ($this->options->getHighlight()) {
$params['body']['highlight'] = $this->buildHighlightRules($context);
}
if ($aggs = $this->getAggregationQueryParams($options)) {
$params['body']['aggs'] = $aggs;
}
$res = $this->client->search($params);
$results = new ArrayCollection();
$n = 0;
foreach ($res['hits']['hits'] as $hit) {
$results[] = ElasticsearchRecordHydrator::hydrate($hit, $n++);
}
/** @var FacetsResponse $facets */
$facets = $this->facetsResponseFactory->__invoke($res);
$query['ast'] = $query_compiler->parse($string)->dump();
$query['query_main'] = $recordQuery;
$query['query'] = $params['body'];
$query['query_string'] = json_encode($params['body']);
return new SearchEngineResult($results, json_encode($query), $res['took'], $offset, $res['hits']['total'], $res['hits']['total'], null, null, $facets->getAsSuggestions(), [], $this->indexName, $facets);
}
示例13: hydrate
/**
* {@inheritdoc}
*/
public function hydrate(array $elasticResult, ProductNumberSearchResult $result, Criteria $criteria, ShopContextInterface $context)
{
if (!isset($elasticResult['aggregations'])) {
return;
}
if (!isset($elasticResult['aggregations']['agg_properties'])) {
return;
}
$data = $elasticResult['aggregations']['agg_properties']['buckets'];
$ids = array_column($data, 'key');
if (empty($ids)) {
return;
}
$groupIds = $this->getGroupIds($ids);
$search = new Search();
$search->addFilter(new IdsFilter($groupIds));
$search->addFilter(new TermFilter('filterable', 1));
$search->addSort(new FieldSort('name'));
$index = $this->indexFactory->createShopIndex($context->getShop());
$data = $this->client->search(['index' => $index->getName(), 'type' => PropertyMapping::TYPE, 'body' => $search->toArray()]);
$data = $data['hits']['hits'];
$properties = $this->hydrateProperties($data, $ids);
$actives = $this->getFilteredValues($criteria);
$criteriaPart = $this->createCollectionResult($properties, $actives);
$result->addFacet($criteriaPart);
}
示例14: search
/**
* @param Criteria $criteria
* @return SearchResultSlice
*/
public function search(Criteria $criteria)
{
$results = $this->client->search($this->parametersBuiler->createParameters($criteria));
if ($results['hits']['total'] === 0) {
return new SearchResultSlice($criteria, [], 0);
}
$recipes = [];
foreach ($results['hits']['hits'] as $result) {
$recipe = new SearchEngine\Result\Recipe($result['_id'], $result['_source']['name'], $result['_source']['description']['text']);
if (!is_null($result['_source']['publicationDate'])) {
$recipe->publishedAt(new \DateTimeImmutable($result['_source']['publicationDate']));
}
$recipes[] = $recipe;
}
return new SearchResultSlice($criteria, $recipes, $results['hits']['total']);
}
示例15: getParameters
/**
*
* {@inheritdoc}
*
*/
public function getParameters(View $view, FormFactoryInterface $formFactoty, Request $request)
{
try {
$renderQuery = $this->twig->createTemplate($view->getOptions()['body'])->render(['view' => $view, 'contentType' => $view->getContentType(), 'environment' => $view->getContentType()->getEnvironment()]);
} catch (\Exception $e) {
$renderQuery = "{}";
}
$searchQuery = ['index' => $view->getContentType()->getEnvironment()->getAlias(), 'type' => $view->getContentType()->getName(), 'body' => $renderQuery, 'size' => $view->getOptions()['size']];
$result = $this->client->search($searchQuery);
try {
$render = $this->twig->createTemplate($view->getOptions()['template'])->render(['view' => $view, 'contentType' => $view->getContentType(), 'environment' => $view->getContentType()->getEnvironment(), 'result' => $result]);
} catch (\Exception $e) {
$render = "Something went wrong with the template of the view " . $view->getName() . " for the content type " . $view->getContentType()->getName() . " (" . $e->getMessage() . ")";
}
return ['render' => $render, 'view' => $view, 'contentType' => $view->getContentType(), 'environment' => $view->getContentType()->getEnvironment()];
}