jjzjj

php - FOSElasticaBundle 和 geo_distance 查找

coder 2024-05-03 原文

我正在尝试使用 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/

有关php - FOSElasticaBundle 和 geo_distance 查找的更多相关文章

  1. ruby-on-rails - Rails 3 I18 : translation missing: da. datetime.distance_in_words.about_x_hours - 2

    我看到这个错误:translationmissing:da.datetime.distance_in_words.about_x_hours我的语言环境文件:http://pastie.org/2944890我的看法:我已将其添加到我的application.rb中:config.i18n.load_path+=Dir[Rails.root.join('my','locales','*.{rb,yml}').to_s]config.i18n.default_locale=:da如果我删除I18配置,帮助程序会处理英语。更新:我在config/enviorments/devolpment

  2. ruby - 当使用::指定模块时,为什么 Ruby 不在更高范围内查找类? - 2

    我刚刚被困在这个问题上一段时间了。以这个基地为例:moduleTopclassTestendmoduleFooendend稍后,我可以通过这样做在Foo中定义扩展Test的类:moduleTopmoduleFooclassSomeTest但是,如果我尝试通过使用::指定模块来最小化缩进:moduleTop::FooclassFailure这失败了:NameError:uninitializedconstantTop::Foo::Test这是一个错误,还是仅仅是Ruby解析变量名的方式的逻辑结果? 最佳答案 Isthisabug,or

  3. ruby - 查找字符串中的内容类型(数字、日期、时间、字符串等) - 2

    我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s

  4. ruby-on-rails - 在 Rails 中更高效地查找或创建多条记录 - 2

    我有一个应用需要发送用户事件邀请。当用户邀请friend(用户)参加事件时,如果尚不存在将用户连接到该事件的新记录,则会创建该记录。我的模型由用户、事件和events_user组成。classEventdefinvite(user_id,*args)user_id.eachdo|u|e=EventsUser.find_or_create_by_event_id_and_user_id(self.id,u)e.save!endendend用法Event.first.invite([1,2,3])我不认为以上是完成我的任务的最有效方法。我设想了一种方法,例如Model.find_or_cr

  5. ruby - 查找重叠的正则表达式匹配项 - 2

    我想找到给定字符串中的所有匹配项,包括重叠匹配项。我怎样才能实现它?#Example"a-b-c-d".???(/\w-\w/)#=>["a-b","b-c","c-d"]expected#Solutionwithoutoverlappedresults"a-b-c-d".scan(/\w-\w/)#=>["a-b","c-d"],but"b-c"ismissing 最佳答案 在积极的前瞻中使用捕获:"a-b-c-d".scan(/(?=(\w-\w))/).flatten#=>["a-b","b-c","c-d"]参见Rubyde

  6. ruby - 在 Ruby 中查找多个正则表达式匹配的模式和位置 - 2

    这应该是一个简单的问题,但我找不到任何相关信息。给定一个Ruby中的正则表达式,对于每个匹配项,我需要检索匹配的模式$1、$2,但我还需要匹配位置。我知道=~运算符为我提供了第一个匹配项的位置,而string.scan(/regex/)为我提供了所有匹配模式。如果可能,我需要在同一步骤中获得两个结果。 最佳答案 MatchDatastring.scan(regex)do$1#Patternatfirstposition$2#Patternatsecondposition$~.offset(1)#Startingandendingpo

  7. arrays - Ruby 可枚举 - 查找最多 n 次匹配元素 - 2

    我有以下数组:arr=[1,3,2,5,2,4,2,2,4,4,2,2,4,2,1,5]我想要一个包含前三个奇数元素的数组。我知道我可以做到:arr.select(&:odd?).take(3)但我想避免遍历整个数组,而是在找到第三个匹配项后返回。我想出了以下解决方案,我相信它可以满足我的要求:my_arr.each_with_object([])do|el,memo|memo但是有没有更简单/惯用的方法来做到这一点? 最佳答案 使用lazyenumerator与Enumerable#lazy:arr.lazy.select(&:o

  8. ruby - 使用指向 ruby​​ 可执行文件的符号链接(symbolic link)时查找相关库 - 2

    假设您有一个可执行文件foo.rb,其库bar.rb的布局如下:/bin/foo.rb/lib/bar.rb在foo.rb的header中放置以下要求以在bar.rb中引入功能:requireFile.dirname(__FILE__)+"../lib/bar.rb"只要对foo.rb的所有调用都是直接的,这就可以正常工作。如果你把$HOME/project和符号链接(symboliclink)foo.rb放入$HOME/usr/bin,然后__FILE__解析为$HOME/usr/bin/foo.rb,因此无法找到bar.rb关于foo.rb的目录名.我意识到像ruby​​gems这

  9. 对象的 Ruby 方法查找路径 - 2

    是否有内置的Ruby方法或众所周知的库可以返回对象的整个方法查找链?Ruby查看一系列令人困惑的类(如thisquestion中所讨论)以查找与消息对应的实例方法,如果没有类响应消息,则调用接收方的method_missing。我将以下代码放在一起,但我确信它遗漏了某些情况或者它是否100%正确。请指出任何缺陷并指导我找到一些更好的代码(如果存在)。defmethod_lookup_chain(obj,result=[obj.singleton_class])ifobj.instance_of?Classreturnadd_modules(result)ifresult.last==B

  10. arrays - 在两个数组中查找不相交元素的有效方法是什么? - 2

    我有以下数组:A=[1,2,3,4,5]B=[2,6,7,1]我想找到不相交的元素,如下:output=[3,4,5,6,7]我是这样实现的,output=A+B-(A&B)但它效率低下,因为我添加了两个数组,然后删除了公共(public)元素。它类似于查找不相交的元素。我能做得比这更好吗?如果是,怎么办? 最佳答案 如何只选择A中的元素而不是B中的元素以及B中的元素而不是A中的元素。(A-B)+(B-A) 关于arrays-在两个数组中查找不相交元素的有效方法是什么?,我们在Stack

随机推荐