PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, October 25, 2022

[FIXED] How to do a guid match with ElasticSearch using NEST

 October 25, 2022     elasticsearch, nest     No comments   

Issue

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)
        )
    )
); 


Answered By - Russ Cam
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing