Search code examples
c#.netelasticsearchnest

C# nest elasticsearch converting guid into array


enter image description hereI am doing an aggregation on a field with the a guid value like c4b0c9ae-345c-4247-87e3-e9d9de67c01b but when elastic search returns the aggregations it is becoming an array ["c4b0c9ae","345c","4247","87e3","e9d9de67c01b"]. How do you handle this on nest?


Solution

  • The field has been mapped as a text datatype which undergoes analysis at index time, by default using the Standard Analyzer. The result is that the GUID is tokenized into the constituent parts by splitting on hyphens. You can see this for yourself using the Analyze API (in Kibana Console)

    GET _analyze
    {
        "analyzer": "standard",
        "text": ["c4b0c9ae-345c-4247-87e3-e9d9de67c01b"]
    }
    

    yields

    {
       "tokens": [
          {
             "token": "c4b0c9ae",
             "start_offset": 0,
             "end_offset": 8,
             "type": "<ALPHANUM>",
             "position": 0
          },
          {
             "token": "345c",
             "start_offset": 9,
             "end_offset": 13,
             "type": "<ALPHANUM>",
             "position": 1
          },
          {
             "token": "4247",
             "start_offset": 14,
             "end_offset": 18,
             "type": "<NUM>",
             "position": 2
          },
          {
             "token": "87e3",
             "start_offset": 19,
             "end_offset": 23,
             "type": "<ALPHANUM>",
             "position": 3
          },
          {
             "token": "e9d9de67c01b",
             "start_offset": 24,
             "end_offset": 36,
             "type": "<ALPHANUM>",
             "position": 4
          }
       ]
    }
    

    You probably don't want to analyze GUIDs at index time, so you should explicitly map them as keyword datatype.