Elasticsearch bool过滤查询返回结果

时间:2014-09-09 08:02:07

标签: elasticsearch

我有一个带映射的索引:

[test] => Array
    (
        [mappings] => Array
            (
                [testype] => Array
                    (
                        [properties] => Array
                            (
                                [brand_id] => Array
                                    (
                                        [type] => integer
                                    )

                                [color] => Array
                                    (
                                        [type] => string
                                        [index] => not_analyzed
                                    )

                                [description] => Array
                                    (
                                        [type] => string
                                    )

                                [discount] => Array
                                    (
                                        [type] => float
                                    )

                                [newprice] => Array
                                    (
                                        [type] => float
                                    )

                                [oldprice] => Array
                                    (
                                        [type] => float
                                    )

                                [sex] => Array
                                    (
                                        [type] => string
                                        [index] => not_analyzed
                                    )

                                [store_id] => Array
                                    (
                                        [type] => integer
                                    )

                                [title] => Array
                                    (
                                        [type] => string
                                    )

                            )

                    )

            )

    )

尝试使用此查询过滤我的数据:

Array
(
[index] => test
[type] => testype
[size] => 100
[body] => Array
    (
        [query] => Array
            (
                [filtered] => Array
                    (
                        [filter] => Array
                            (
                                [bool] => Array
                                    (
                                        [must] => Array
                                            (
                                                [term] => Array
                                                    (
                                                        [brand_id] => 53
                                                        [color] => red
                                                    )

                                            )

                                    )

                            )

                    )

            )

    )

)

期望得到像SELECT ... WHERE brand_id = 53 AND color = red的结果,但是有像brand_id = 53或color = red的结果。我错过了什么吗?

Actualy,我希望有一个类似SELECT的过滤器...... WHERE aaaa ='a'和bbbb ='b'和cccc IN(1,2,6,9)和ddd IN(xxx,yyy)

1 个答案:

答案 0 :(得分:1)

您必须将这些术语放在不同的数组中。

免责声明:我猜这个语法是PHP语法,我不熟悉它,请原谅任何语法错误。

试试这个:

[must] => Array
    (
        [term] => Array
            (
                [brand_id] => 53
            )
        [term] => Array
            (
                [color] => red
            )
    )

您的上一个查询应如下所示:

[must] => Array
    (
        [term] => Array
            (
               [aaaa] => 'a'
            )
        [term] => Array
            (
                [bbbb] => 'b'
            )
        [terms] => Array
            (
                [cccc] => Array(1, 2, 6, 9)
                [minimum_should_match] => 1
            )
        [terms] => Array
            (
                [dddd] => Array('xxx', 'yyy')
                [minimum_should_match] => 1
            )
    )