# Exact search

**URL:** <https://discuss.elastic.co/t/exact-search/144093>\
**Category:** Elasticsearch\
**Created:** [August 13, 2018, 7:34am UTC](https://discuss.elastic.co/t/exact-search/144093 "2018-08-13T07:34:25Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ole](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@Ole](https://discuss.elastic.co/u/Ole)\
**Post date:** [August 13, 2018, 7:34am UTC](https://discuss.elastic.co/t/exact-search/144093/1 "2018-08-13T07:34:25Z")

</div>

I have a PHP script that performs multiple queries that are mostly identical except the words being queried. They get the query words from a variable in PHP. They look like this:

```
$q = $_GET['q'];
$query = $es->search([
    'index' => 'aksjeregisteret2017',
    'size' => 20,
    'body' => [
        'query' => [
            'bool' => [
                'must' => [
                    'multi_match' => [
                        'query' => $q,
		    'type' => "phrase_prefix",
                        'fields' => ['message', 'Navn Aksjoner', 'Orgnr', 'Selskap',
                            'Aksjeklasse', 'Navn Aksjoner', 'Fodselar/Orgnr', 'Postnr',
                            'Antall Aksjer', 'Antall Aksjer Selskap'],
                        "minimum_should_match" => "100%"
                    ]
                ]
            ]
        ]
    ]
]);

```

}

Now this works fine. It returns the results and I'm able to do whatever I want with it.

The problem is that it also returns documents that does not contain exactly what I'm searching. For example when searching for "Hopland AS" I find the documents containing "Hopland AS" but I also find documents containing that does not contain it, but instead contains words such as "Hopland Geir Atle" and "Geir Hopland AS".

What I want is the query to not return the documents that does not contain exactly "Hopland AS" in any of their fields. Is this feasible? Any other information I can provide to help find a solution?

I'm using Elasticsearch 6.3.2. The PHP is running on Apache2 with PHP 7.2.

---

<div class="post-metadata">

**Author:** ![Ethan-Zhang](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/ethan-zhang/32/54812_2.png) [@Ethan-Zhang](https://discuss.elastic.co/u/Ethan-Zhang)\
**Post date:** [August 13, 2018, 9:30am UTC](https://discuss.elastic.co/t/exact-search/144093/2 "2018-08-13T09:30:03Z")

</div>

You may rewrite the query as ("Hopland" AND "AS"). ES will search the record  
the contain both the words of "Hopland" and "AS"

---

<div class="post-metadata">

**Author:** ![Ole](https://avatars.discourse-cdn.com/v4/letter/o/fbc32d/32.png) [@Ole](https://discuss.elastic.co/u/Ole)\
**Post date:** [August 13, 2018, 10:43am UTC](https://discuss.elastic.co/t/exact-search/144093/3 "2018-08-13T10:43:40Z")

</div>

By doing this, would it also return results that contain fields with a value of "Hopland", "AS", and more? So would it return "Hopland Kraft AS"? Because we only want "Hopland AS" and not "Hopland Kraft AS".

---

<div class="post-metadata">

**Author:** ![Abhilash\_B](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abhilash_b/32/40270_2.png) [@Abhilash\_B](https://discuss.elastic.co/u/Abhilash_B)\
**Post date:** [August 13, 2018, 11:03am UTC](https://discuss.elastic.co/t/exact-search/144093/4 "2018-08-13T11:03:30Z")

</div>

You need to do a match\_phrase query with slop 0. This will ensure that position offsets of "AS" succeed "Hopland" and the slop 0 will ensure that there are no tokens/words between them.

---

<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:** [September 10, 2018, 11:03am UTC](https://discuss.elastic.co/t/exact-search/144093/5 "2018-09-10T11:03:41Z")

</div>

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