Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NEST ElasticSearch Guid problems

I have got some problems to filter my queries on field Guid. Here a sample of my code. What did I miss?

public class myObject 
{
  public Guid Id {get;set}
  public String Field1 { get; set; }
  public String Field2 { get; set; }
  public String Fieldn { get; set; }
  public ReadingRightsEnum ReadingRights { get; set; }
  public Guid UserId { get; set; }
}

// Index fct example
public void IndexMyObject(obj)
{
   var result = await myClient.IndexAsync(obj, i => i
                              .Index("myIndexName")
                              .Type("myObject")
                              .Id(obj.Id.ToString())
                              .Refresh());
}

// Index fct example
public void SearchOnlyInMyUserObject(string userQuery, Guid userId)
{
    var searchResult = await myClient.SearchAsync<myObject>(body =>
        body.Query(q => 
            q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(userQuery))
            && q.Term(i => i.UserId, userId))
        .Fields(f => f.Id)
        .Size(200));
}

// Index fct example with filter
public void SearchOnlyInMyUserObject(string userQuery, Guid userId)
{
    var filters = new List<FilterContainer>
    {
         new FilterDescriptor<myObject>().Bool(b => b.Must(m => m.Term(i => i.UserId, userId)));
    };
    var searchResult = await myClient.SearchAsync<myObject>(body =>
        body
        .Filter(f => f.And(filters.ToArray()))
        .Query(q => 
            q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(userQuery)))
        .Fields(f => f.Id)
        .Size(200));
}

Both functions work fine if I filter on others parameters but return nothing when I filter on Guid. Should a convert my Guid to string when I index my object?

If i do http://xxxxxxx:9200/_search?q=userId:e4aec7b4-c400-4660-a09e-a0ce064f612e it's work fine.

Any ideas?

Thanks by advance

Edit 06/12 here a sample of myindex:

myIndexName":{
    "mappings":{
        "myObject":{
            "properties":{
                "readingrights":{
                    "type":"integer"
                },
                "id":{
                    "type":"string"
                },
                "field1":{
                    "type":"string"
                },      
                "field2":{
                    "type":"string"
                },                
                "userId":{
                    "type":"string"
                }
            }
        }
    }
}
like image 557
Cantinos Avatar asked Dec 12 '25 20:12

Cantinos


1 Answers

GUID field is tricky in Elastic. If you use analyze function of elastic client it will show you how it breaks up a GUID. AnalyzeRequest obj = new AnalyzeRequest(_index, item); _client.Analyze(obj);

When you create an entity, You need to define guid as not be analyzed. [String(Index = FieldIndexOption.NotAnalyzed)] public Guid TextAssetId

like image 99
fillic2002 Avatar answered Dec 16 '25 14:12

fillic2002