gpt4 book ai didi

php - FOSElasticaBundle 和 geo_distance 查找

转载 作者:可可西里 更新时间:2023-11-01 01:02:47 32 4
gpt4 key购买 nike

我正在尝试使用 elasticsearch 进行按邻近度排序的搜索。我安装了 FOSElasticaBundle 并且在我的 config.yml 中有这个配置:

fos_elastica:
clients:
default: { host: localhost, port: 9200 }
serializer:
callback_class: FOS\ElasticaBundle\Serializer\Callback
serializer: serializer
indexes:
productos:
client: default
settings:
index:
analysis:
analyzer:
default:
type: spanish
tokenizer: icu_tokenizer
filter: [ icu_folding, my_ngram ]
filter:
my_ngram:
type: "nGram"
min_gram: 2
max_gram: 25
types:
producto:
mappings:
nombre: { boost: 5 }
descripcion: { boost: 3 }
persistence:
driver: orm
model: Gupost\BackendBundle\Entity\Producto
listener:
provider: ~
finder: ~
comercios:
client: default
settings:
index:
analysis:
analyzer:
default:
type: spanish
tokenizer: icu_tokenizer
filter: [ icu_folding, my_ngram ]
filter:
my_ngram:
type: "nGram"
min_gram: 2
max_gram: 25
types:
comercio:
mappings:
nombrecomercio: { boost: 5 }
razonsocial: { boost: 5 }
location: {type: geo_point}

persistence:
driver: orm
model: Gupost\BackendBundle\Entity\Comercio
listener:
provider: ~
finder: ~

索引构建正确,如果我执行此查询:

curl -XGET 'http://localhost:9200/comercios/comercio/_search' -d '{
"query": {
"filtered" : {
"query" : {
"match_all" : {}
},
"filter" : {
"geo_distance" : {
"distance" : "100m",
"location" : {
"lat" : -2.1741771697998047,
"lon" : 43.28249657890983
}
}
}
}
}

}'

我得到了一些结果。问题是当我试图从我的 Controller 中获取结果时。
我怎样才能做到这一点?我试过这个:

<?php

namespace Acme\ApiBundle\Controller;

use Elastica\Query\Filtered;
use FOS\RestBundle\Controller\Annotations\NamePrefix; // NamePrefix Route annotation class @NamePrefix("bdk_core_user_userrest_")
use FOS\RestBundle\View\RouteRedirectView; // Route based redirect implementation
use FOS\RestBundle\View\View AS FOSView; // Default View implementation.
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\ConstraintViolation;
use FOS\RestBundle\Request\ParamFetcher;
use FOS\RestBundle\Controller\Annotations\RequestParam;
use Elastica\Filter\GeoDistance;
use Elastica\Query;
use Elastica\Query\MatchAll;
//use Elastica\Query\F
/**
* Controller that provides Restful sercies over the resource Users.
*
* @NamePrefix("api_rest_comercios")
*/
class ComerciosRestController extends Controller
{

public function getgeocomerciosAction() {
$view = FOSView::create();
$finder = $this->container->get('fos_elastica.finder.comercios.comercio');

$query = new Query();

$filter = new GeoDistance('location', array('lat' => -2.1741771697998047,
'lon' => 43.28249657890983), '100m');
$all = new Query(new MatchAll());
$query = new Filtered($all, $filter);

$data = $query->toArray();
ladybug_dump( $data );
$query->setRawQuery($data);
$entities = $finder->find($query);
// $results = $index->search($query);

if ($entities) {
$view->setStatusCode(200)->setData($entities);
}
return $view;
}
}

但它不起作用。我收到此错误:

Catchable Fatal Error: Argument 1 passed to Elastica\Query\Filtered::__construct() must be an instance of Elastica\Query\AbstractQuery, instance of Elastica\Query given, called in /Users/gitek/www/merkaklub/src/Gupost/ApiBundle/Controller/ComerciosRestController.php on line 92 and defined in /Users/gitek/www/merkaklub/vendor/ruflin/elastica/lib/Elastica/Query/Filtered.php line 36

有什么帮助或线索吗?

最佳答案

正确的代码是

$query = new Filtered(new MatchAll(), $filter);

因为 \Elastica\Query\MatchAll 扩展了 AbstractQuery\Elastica\Query 是一种名称相当困惑的客户端。

关于php - FOSElasticaBundle 和 geo_distance 查找,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18975381/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com