dar25
dar25

Reputation: 93

Why are no documents returned with this NEST wildcard search with C#/ElasticSearch?

This NEST wildcard search is returning zero (0) documents, even though it should because there is data in my index that has this criteria. It is a valid connection and there are no errors showing when setting breakpoints. Any Ideas?

public class Book_ES
{

    public string book_title { get; set; }
     public string book_author { get; set; }
}

 sr_detailed = new SearchDescriptor<Book_ES>()


                    .Index("books_3").Take(10000)
                   .Query(a => a
                          .Bool(b => b
                            .Should(c => c
                              .Wildcard(d => d
                                
 .Field(f=>f.book_title).Field(g=>g.book_author).Value("A*")
                              )
                            )
                          )
                        
                    ).Highlight(h => h.Fields(f => f.Field("*").PreTags("<span class='hit' style='background-color:yellow'>").PostTags("</span>")))
                   ;

 var results = Esclient.Search<Book_ES>(sr_detailed);

Upvotes: 1

Views: 164

Answers (1)

Rob
Rob

Reputation: 9979

You didn't share your mapping but I assume it looks like

{
  "books_3": {
    "mappings": {
      "properties": {
        "book_title": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        },
        "book_author": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }
      }
    }
  }
}

in this case, when you put A* as a value for your wildcard query you won't get any results back because both book_title and book_author use standard analyzer for the text field and all uppercased characters are removed from indexed tokens - for this configuration elasticsearch won't store any tokens starting with A, see below

GET _analyze
{
  "analyzer": "standard",
  "text": "Test"
}

{
  "tokens" : [
    {
      "token" : "test",
      "start_offset" : 0,
      "end_offset" : 4,
      "type" : "<ALPHANUM>",
      "position" : 0
    }
  ]
}

You can set case_insensitive to true in your wildcard query and that will fix your issue as docs say

case_insensitive [7.10.0]Added in 7.10.0. (Optional, Boolean) Allows case insensitive matching of the pattern with the indexed field values when set to true. Default is false which means the case sensitivity of matching depends on the underlying field’s mapping.

Upvotes: 1

Related Questions