简体   繁体   中英

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
            }
          }
        }

,

Guid映射为keyword类型并使用术语级别查询(例如term查询)来查找完全匹配项。

I am late to the party but You can use MatchPhrase Methode like this sample:

await _elasticClient.SearchAsync<Employee>(
                s => s.Index(IndexName).Query(
                    q => q.MatchPhrase(tq =>
                        tq.Field(x => x.DepartmentId).Query(dep.Id.ToString())))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM