我正在尝试按与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;
    }
}

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



任何帮助或线索?

最佳答案

正确的代码是

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

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

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

10-11 05:39