# How to get only exact matches for GUID via Nest 6.5

**URL:** https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946
**Category:** Elasticsearch
**Created:** [February 26, 2019, 7:25am UTC](https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946 "2019-02-26T07:25:40Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![ArtVandelay101](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artvandelay101/32/41380_2.png) [@ArtVandelay101](https://discuss.elastic.co/u/ArtVandelay101)
#### Post date: [February 26, 2019, 7:25am UTC](https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946/1 "2019-02-26T07:25:40Z")

</div>

Hello,

I have following index, in which I labeled the 'Id' as 'Keyword':

```
[ElasticsearchType(IdProperty = nameof(Id))]
public class MyIndexModel
{
    [Keyword]
    public Guid Id { get; set; }

    [Text]
    public string SomeString{ get; set;}
 }

```

If I do following query on the index:

```
 var searchResponse = await this.elasticClient
            .SearchAsync<MyIndexModel>(
                s => s
                    .Index(MyIndexName)
                    .Size(1)
                    .PostFilter( pf => pf
                        .Bool( b => b.
                            Must( must => must
                            .Match(m => m
                                .Field(f => f.Id)
                                .Query(id.ToString()))))));

```

In which the 'id' in the last line is following GUID

`'3B9BAAEE-AC35-E911-8E9E-D89EF315843C'`

I get a result matching

`'b859d1ff-ac35-e911-8e9e-d89ef315843c'`.

The two Guids are similar but not the same. I thought that using the "Keyword" annotation **guarantees** that only exact matches are returned.

One further thing I've noticed is that the mapping for properties marked with "Keyword" and those marked with "Text" looks the same :

```
{
  "mapping": {
    "myindexmodel": {
      "properties": {
        "id": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "somestring": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

```

What am I missing? How do I get only exact matches for GUID via Nest 6.5?

Thanks,

Art

---

<div class="post-metadata">

### Author: ![ArtVandelay101](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/artvandelay101/32/41380_2.png) [@ArtVandelay101](https://discuss.elastic.co/u/ArtVandelay101)
#### Post date: [February 26, 2019, 8:22am UTC](https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946/2 "2019-02-26T08:22:07Z")

</div>

I found the solution: using "MatchPhrase" instead of "Match" does the trick 🙂

---

<div class="post-metadata">

### Author: ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)
#### Post date: [February 27, 2019, 2:00am UTC](https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946/3 "2019-02-27T02:00:19Z")

</div>

For a `keyword` field like `Id` where you are looking for an **exact match** , you should use [a `term` query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html), so that the query input does not undergo analysis.

Additionally, you should move the `bool` query out of `.PostFilter(...)` and into `.Query(...)`, since the search request has no aggregations, so there is no need to use `post_filter` in this instance.

One more thing, since the `bool` query contains only a single query, there's no need to wrap it in a `bool` query, so you can specify the `term` query directly in `.Query(q => q.Term(...))`.

---

<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: [March 27, 2019, 2:00am UTC](https://discuss.elastic.co/t/how-to-get-only-exact-matches-for-guid-via-nest-6-5/169946/4 "2019-03-27T02:00:22Z")

</div>

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