doupu2722 2015-02-24 08:58
浏览 247

使用Elasticsearch在Laravel中搜索查询的逻辑

I am using Laravel together with Elasticsearch in order to search an table/type.

I have a total of 5 search filters that a user can use in order to search.

Title(string) - type(boolean) - state name(int) - city name(int) - price(int)

So the query can have 31 different combinations.

Since I cant use something like Eloquent ORM here I need to write each query for ES.

Is there a better way to do this?

Or is there some Laravel package that would let me do something like this - leave some search parameters empty and only let ES pick up those who arent empty.

'filtered' => [
                'query' => [
                    'match' => ['title' => Input::get('query')]
                ],
                'filter'=> [
                    'bool' => [
                        'must' => [
                            ['term' => [ 'type' =>  1] ],
                            ['term' => [ 'state' =>  22] ],
                            ['term' => [ 'city' => ] ],
                            [ 'range' => [
                                    'price' => [
                                        'gte' => ,
                                        'lte' => ,
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ],
  • 写回答

1条回答 默认 最新

  • dsadasd12132 2015-05-06 04:05
    关注

    Have you seen Elastica?

    http://elastica.io/example/aggregations/terms.html

    This might be the closest you will get to something that provides an Eloquent-like interface to Elasticsearch.

    use Elastica\Aggregation\Terms;
    use Elastica\Query;
    
    // set up the aggregation
    $termsAgg = new Terms("genders");
    $termsAgg->setField("gender");
    $termsAgg->setSize(10);
    
    // add the aggregation to a Query object
    $query = new Query();
    $query->addAggregation($termsAgg);
    
    // retrieve the results
    $index = $elasticaClient->getIndex('someindex');
    $buckets = $index->search($query)->getAggregation("genders");
    

    Which will end up building a query like:

    {
        "aggs" : {
            "genders" : {
                "terms" : { "field" : "gender" },
                "size": 10
            }
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c