# Not getting any results for a field that requires exact match

**URL:** <https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102>\
**Category:** Elasticsearch\
**Tags:** language-clients\
**Created:** [April 9, 2024, 7:44pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102 "2024-04-09T19:44:32Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sujinthan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sujinthan/32/133403_2.png) [@Sujinthan](https://discuss.elastic.co/u/Sujinthan)\
**Post date:** [April 9, 2024, 7:44pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/1 "2024-04-09T19:44:32Z")

</div>

I'm building a search feature that allows users to filter by certain field. One of the field is DOI. Filtering by DOI should only return results with exact match.

I'm trying to index the following object:

```
public class PaperElasticSearchModel
    {
        public string Title { get; set; }
        public string Abstract { get; set; }
        public string Keywords { get; set; }
        public string DOI { get; set; }
        public int Year { get; set; }
        public string[] Author { get; set; }
    }

```

The field `DOI` is will have a similar format to this: `10.1523/JNEUROSCI.18-04-01622.1998`.

Below is the setting for the index

```
Client.Indices.Create(index, pub => pub
.Settings(s => s
 .Analysis(descriptor => descriptor
     .Analyzers(analyzer => analyzer
         .Custom("custom_analyzer", ca => ca
             .Filters("lowercase", "stop", "classic", "word_delimiter")
             )
         )
         
    .TokenFilters(bases => bases
         .EdgeNGram("custom_analyzer", td => td
         .MinGram(2)
         .MaxGram(25))
         )
    )
 .Setting(UpdatableIndexSettings.MaxNGramDiff, 23)
 )
.Map<PubScreenSearch>(m => m
 .AutoMap()
 .Properties(p => p
     .Text(t => t
         .Name(f => f.Author)
         .Analyzer("custom_analyzer"))
     .Text(t => t
         .Name(f => f.Keywords)
         .Analyzer("custom_analyzer"))
     .Text(t => t
         .Name(f => f.Title)
         .Analyzer("custom_analyzer"))
     .Keyword(t => t
         .Name(f => f.DOI)
     )
     
 )));

```

Here is the filter query that is used:

```
else if (pi.Name == "DOI")
  {
      filterQuery.Add(fq => fq
                  .Term(new Nest.Field(pi.Name.ToLower()), value)
              );
  }

```

What Analyzer should I use such that if I were to search by DOI it would return for exact match?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 9, 2024, 9:01pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/2 "2024-04-09T21:01:19Z")

</div>

Hi @Sujinthan

If the DOI field is a keyword, you are already indexing without applying any analyzer and to search for the exact term, [Term Query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html) is recommended.

---

<div class="post-metadata">

**Author:** ![Sujinthan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sujinthan/32/133403_2.png) [@Sujinthan](https://discuss.elastic.co/u/Sujinthan)\
**Post date:** [April 10, 2024, 5:50pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/3 "2024-04-10T17:50:51Z")

</div>

Hi @RabBit_BR  
Unfortunately that did not work. I think there is something wrong with the way I index my object.

Here I'm searching for a document where the `title` is `mouse`. I get the following results:

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/8/4/846518e21c8e7c68c0d29c3001271213474101f8.png)

If I search for the DOI (underlined in red) I don't see any results:

```auto
POST /pubscreen/_search
  {
    "query": {

                "term": {
                    "dOI": {
                        "value": "10.1177/0301006615614440"
                    }
                }
    }
  }

```

```auto
{
  "took": 0,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 0,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  }
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 10, 2024, 6:16pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/4 "2024-04-10T18:16:27Z")

</div>

provide your mapping/settings:

`GET pubscreen`

Field "dOI" should be:

```auto
 "properties": {
      "dOI": {
        "type": "keyword"
      }
    }

```

---

<div class="post-metadata">

**Author:** ![Sujinthan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sujinthan/32/133403_2.png) [@Sujinthan](https://discuss.elastic.co/u/Sujinthan)\
**Post date:** [April 10, 2024, 7:03pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/5 "2024-04-10T19:03:22Z")

</div>

It is not...

```auto
"dOI": {
  "type": "text",
  "fields": {
    "keyword": {
      "type": "keyword",
      "ignore_above": 256
    }
  }
},

```

Here is how I'm creating the Index

```auto
 Client.Indices.Create(index, pub => pub
 .Settings(s => s
     .Analysis(descriptor => descriptor
         .Analyzers(analyzer => analyzer
             .Custom("custom_analyzer", ca => ca
                 .Filters("lowercase", "stop", "classic", "word_delimiter")
                 )
             )
        .TokenFilters(bases => bases
             .EdgeNGram("custom_analyzer", td => td
             .MinGram(2)
             .MaxGram(25))
             )
        )
     )
 .Map<PubScreenSearch>(m => m
     .AutoMap()
     .Properties(p => p
         .Text(t => t
             .Name(f => f.Author)
             .Analyzer("custom_analyzer"))
         .Text(t => t
             .Name(f => f.Keywords)
             .Analyzer("custom_analyzer"))
         .Text(t => t
             .Name(f => f.Title)
             .Analyzer("custom_analyzer"))
         .Keyword(t => t
             .Name(f => f.DOI))
         )
     )
 );

```

I'm following [this documentation](https://www.elastic.co/guide/en/elasticsearch/client/net-api/7.17/fluent-mapping.html) for Elasticsearch v7.17, however I'm using Elasticsearch v8.12, I don't think this is an issue.

---

<div class="post-metadata">

**Author:** ![Christian\_Dahlqvist](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/christian_dahlqvist/32/4617_2.png) [@Christian\_Dahlqvist](https://discuss.elastic.co/u/Christian_Dahlqvist)\
**Post date:** [April 10, 2024, 7:06pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/6 "2024-04-10T19:06:58Z")

</div>

It looks like that field was mapped using dynamic mapping. This means it has a [multi field](https://www.elastic.co/guide/en/elasticsearch/reference/8.13/multi-fields.html) `keyword` that is mapped as `keyword`, so use this instead as follows:

> [@Sujinthan](#):
>
> ```auto
> POST /pubscreen/_search
> {
> "query": {
> 
> "term": {
> "dOI.keyword": {
> "value": "10.1177/0301006615614440"
> }
> }
> }
> }
> 
> ```

---

<div class="post-metadata">

**Author:** ![Sujinthan](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sujinthan/32/133403_2.png) [@Sujinthan](https://discuss.elastic.co/u/Sujinthan)\
**Post date:** [April 10, 2024, 7:37pm UTC](https://discuss.elastic.co/t/not-getting-any-results-for-a-field-that-requires-exact-match/357102/7 "2024-04-10T19:37:08Z")

</div>

Thank you @RabBit_BR and @Christian_Dahlqvist for helping me with this.  
After following @Christian_Dahlqvist advice I was able to resolve this issue.

Since this is mapped dynamically, I needed my query to be written as the following:

```auto
filterQuery.Add(fq => fq
            .Bool(f => f
                .Must(boolSHould => boolSHould
                    .Term(t => t
                        .Field(feild => feild.DOI.Suffix("keyword"))
                        .Value(value))
                        ))
            
        );

```
