【问题标题】:Logic behind search query in Laravel using Elasticsearch使用 Elasticsearch 在 Laravel 中搜索查询背后的逻辑
【发布时间】:2015-04-25 19:50:56
【问题描述】:

我正在使用 Laravel 和 Elasticsearch 来搜索表/类型。

我共有 5 个搜索过滤器可供用户使用以进行搜索。

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

所以查询可以有 31 种不同的组合。

因为我不能在这里使用像 Eloquent ORM 这样的东西,所以我需要为 ES 编写每个查询。

有没有更好的方法来做到这一点?

或者是否有一些 Laravel 包可以让我做这样的事情 - 将一些搜索参数留空,只让 ES 挑选那些不为空的。

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

【问题讨论】:

  • 您可能想查看github.com/adamfairholm/Elasticquent ...它为 Eloquent 模型添加了一个特征,以使用 Elasticsearch 添加索引和搜索
  • 除此之外,查询语言需要与 Eloquent 稍有不同(毕竟它不是 SQL)......你希望这个类看起来如何?

标签: php mysql search laravel elasticsearch


【解决方案1】:

你见过 Elastica 吗?

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

这可能是您最接近为 Elasticsearch 提供类似 Eloquent 接口的东西。

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");

最终会构建如下查询:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-12-03
    • 1970-01-01
    • 2016-04-20
    • 2017-04-02
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多