弹性搜索PHP中的多个范围 [英] Elastic Search multiple ranges in PHP

查看:135
本文介绍了弹性搜索PHP中的多个范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直盯着我的显示器太久不问:如何使用弹性搜索PHP库执行多范围查询

I have been staring at my monitor for too long not to ask: how do I perform multiple range queries using Elastic Search PHP library?

我正在根据价格标准(小于或等于)和产品数量查询我的索引(最少2个产品,2个产品等)。

I am trying to query my index based on price criteria (less and more than) and product quantity within its category (minimum 2 products, 2 products only etc).

虽然 $ searchParams ['body'] ['query'] ['range'] ['number_of_products' ] = ['gte'=> $ products + 1];
自己的工作正常,结合

While $searchParams['body']['query']['range']['number_of_products'] = ['gte' => $products + 1]; on its own works fine, combined with

$searchParams['body']['query']['range']['price'] = [
    'gte' => $price['min'],
    'lte' => $price['max'],
];

我正在努力寻找一个体面的最近的例子,涵盖这个场景ES v0.9,这在我的v1.4.2失败。

I am struggling to find a decent recent example covering this scenario post ES v0.9, which on my v1.4.2 fails.

推荐答案

你的答案是正确的。另外,如果您使用 bool 过滤器而不是和/或/ not 设置,性能将会更好的过滤器。并不总是这样,但通常。

Your answer is correct. As an aside, performance will tend to be better if you use the bool filter rather than and/or/not set of filters. It isn't always the case, but usually.

和/或/不适用于leapfrog iterator模式。这将在第一个过滤器中找到第一个匹配的文档,然后尝试跨越其余的过滤器迭代器,直到它们都在同一个文档上对齐。这使得它对于稀疏过滤器是有效的。

And/or/not works in a "leapfrog iterator" pattern. This finds the first matching doc in the first filter, then attempts to "leapfrog" the rest of the filter iterators until they all align on the same doc. This makes it efficient for sparse filters.

相比之下,Bool过滤器按位图组合(表示匹配的文档),这对于密集的过滤器来说更有效率。您的过滤器看起来相对密集:

In contrast, the Bool filter does bitwise combination of bitmaps (representing matching docs), which tends to be more efficient for denser filters. Your filters look relatively dense:


  • number_of_products 过滤器全部为 values> = $ products ['min'] ,可能是大量匹配的文档...全部在连续的范围内

  • 密度价格过滤器取决于范围的大小,但也可能相当密集(和连续)。如果没有$ price ['max'],它也无限制到无穷大。

  • The number_of_products filter is all values >= $products['min'], which could be a large number of matching docs...all in a contiguous range
  • The density of price filter depends on the size of the range, but could also be quite dense (and contiguous). If there is no $price['max'], it is also unbounded to infinity.

在实践中,bool往往会更好性能比和/或/不,并且在这个例子中可能是YMMV。 :)

In practice, bool tends to give better performance than and/or/not, and probably will in this example, but YMMV. :)

$searchParams = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'filtered' => [
                'filter' => [
                    'bool' => [
                        'must' => [
                            [
                                'range' => [
                                    'number_of_products' => [
                                        'gte' => $products['min']
                                    ]
                                ]
                            ],
                            [
                                'range' => [
                                    'price' => [
                                        'gt' => $price['min'],
                                        'lt' => ($price['max'] ? : null)
                                    ]
                                ]
                            ]
                        ]
                    ]
                ]
            ]
        ]
    ]
];

这篇关于弹性搜索PHP中的多个范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆