# Problem with must\_not query please help

**URL:** <https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340>\
**Category:** Elasticsearch\
**Created:** [November 11, 2019, 11:23am UTC](https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340 "2019-11-11T11:23:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndriyImp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andriyimp/32/57480_2.png) [@AndriyImp](https://discuss.elastic.co/u/AndriyImp)\
**Post date:** [November 11, 2019, 11:23am UTC](https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340/1 "2019-11-11T11:23:05Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [November 11, 2019, 12:19pm UTC](https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340/2 "2019-11-11T12:19:27Z")

</div>

Have you created an explicit [mapping](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping.html) for the `enterprises` index? If not, then the `enterprise_number` is a text field, which is [analyzed](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis.html). That means that a value like `BE0728665087 ` gets lowercased and is indexed as `be0728665087`, because of the [standard analyzer](https://www.elastic.co/guide/en/elasticsearch/reference/current/analysis-standard-analyzer.html).

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.

---

<div class="post-metadata">

**Author:** ![AndriyImp](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/andriyimp/32/57480_2.png) [@AndriyImp](https://discuss.elastic.co/u/AndriyImp)\
**Post date:** [November 11, 2019, 1:30pm UTC](https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340/3 "2019-11-11T13:30:46Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 9, 2019, 1:37pm UTC](https://discuss.elastic.co/t/problem-with-must-not-query-please-help/207340/4 "2019-12-09T13:37:43Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
