Search code examples
elasticsearchnest

How to do a guid match with ElasticSearch using NEST


I have ES documents where a field is a guid.

When I try to do a match, the dashes wreak havoc in my search and I get a bunch of unrelated partial matches.

Since this field is a guid, it always require an exact match.

In Kibana, I can wrap the guid in quotes and I get exact matches; with NEST, quotes don’t help. I have also tried to escape the dashes without any success.

I need to store the guid as is since in some cases I have to retrieve the document with the guid intact, so I can’t remove the dashes in there.

Is there a way to flag that field, with NEST, so that when I query it only exact matches are returned?


edit: following the answer below, here is what I did:

  • I added [Nest.Keyword] to the guid field

  • I made the query like this:

    var R = await _ElasticClient.SearchAsync<PictureObject>(Sr => Sr
        .Query(Q => Q.Term(Te => Te
        .Field(F => F.AlbumId)   <- the guid field
        .Value(IdString));  <- my value
    

But this doesn't return anything.

The elastic site's documentation gives an example (https://www.elastic.co/guide/en/elasticsearch/client/net-api/current/terms-query-usage.html):

q
.Terms(c => c
.Name("named_query")
.Boost(1.1)
.Field(p => p.Description)
.Terms("term1", "term2")
)

I can't find any documentation about the Name method and what I did seems similar, for the single field so I have no idea what impact it has in my case, if any.

The mapping of my field, seen from Kibana, is:

 "mapping": {
    "pictureobject": {
      "properties": {
        "AlbumId": {
          "type": "text",
          "fields": {
            "keyword": {
              "type": "keyword",
              "ignore_above": 256
            }
          }
        }

,


Solution

  • Map the Guid as a keyword type and use term level queries such as term query to find exact matches.

    var r = await _ElasticClient.SearchAsync<PictureObject>(sr => sr
        .Query(q => q
            .Term(te => te
                .Field(f => f.AlbumId.Suffix("keyword"))
                .Value(IdString)
            )
        )
    );