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


Logic behind search query in Laravel using Elasticsearch

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

我总共有5个搜索过滤器,用户可以使用它们进行搜索。

标题(字符串)-类型(布尔值)-州名称(int)-城市名称(int)-价格(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' => ,
                                    ]
                                ]
                            ]
                        ]
                    ]
                ],
            ],

您看过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
        }
    }
}