Problem with must_not query please help

Hi All!

have bit problem with query - must_not do not works.. here is query:

(
    [bool] => Array
        (
            [must] => Array
                (
                    [0] => Array
                        (
                            [bool] => Array
                                (
                                    [should] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [addresses.zip] => Array
                                                                (
                                                                    [gte] => 1000
                                                                    [lte] => 1299
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                    [1] => Array
                        (
                            [bool] => Array
                                (
                                    [must] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [addresses.zip] => Array
                                                                (
                                                                    [gte] => 1000
                                                                    [lte] => 1299
                                                                )

                                                        )

                                                )

                                            [1] => Array
                                                (
                                                    [range] => Array
                                                        (
                                                            [start_date] => Array
                                                                (
                                                                    [gte] => 2019-08-01
                                                                    [lte] => 2019-08-31
                                                                )

                                                        )

                                                )

                                        )

                                )

                        )

                )

            [must_not] => Array
                (
                    [0] => Array
                        (
                            [terms] => Array
                                (
                                    [enterprise_number] => Array
                                        (
                                            [0] => BE0728665087
                                            [1] => BE0722851918
                                        )

                                )

                        )

                )

        )

)

but on my results i still see excludet items:

(
[_shard] => [enterprises][0]
[_node] => Ak_cDKp2QGeOC2SA0Xnbug
[_index] => enterprises
[_type] => text
[_id] => 3770628
[_score] => 
[_source] => Array
    (
        [enterprise_number] => BE0728665087
        [start_date] => 2019-08-03
      

    )

[sort] => Array
    (
        [0] => 1564790400000
        [1] => 3770628
    )

[_explanation] => Array
    (
        [value] => 3
        [description] => sum of:
        [details] => Array
            (
                [0] => Array
                    (
                        [value] => 1
                        [description] => addresses.zip:[1000 TO 1299]
                        [details] => Array
                            (
                            )

                    )

                [1] => Array
                    (
                        [value] => 2
                        [description] => sum of:
                        [details] => Array
                            (
                                [0] => Array
                                    (
                                        [value] => 1
                                        [description] => addresses.zip:[1000 TO 1299]
                                        [details] => Array
                                            (
                                            )

                                    )

                                [1] => Array
                                    (
                                        [value] => 1
                                        [description] => start_date:[1564617600000 TO 1567295999999]
                                        [details] => Array
                                            (
                                            )

                                    )

                            )

                    )

            )

    )

)

any ideas what wrong with my query?

Thanks!

Have you created an explicit mapping for the enterprises index? If not, then the enterprise_number is a text field, which is analyzed. That means that a value like BE0728665087 gets lowercased and is indexed as be0728665087, because of the standard analyzer.

However, the terms query is not analyzed, and as a result it will try to look for a value BE0728665087, which does not exist.

How to solve this? Try querying the enterprise_number.keyword field instead. This is a non-analyzed field that automatically gets created when you index a string field like enterprise_number. That's the one you should use with the terms query.

thank you so match! it works now like i need.