Cantinos
Cantinos

Reputation: 282

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

Upvotes: 3

Views: 1499

Answers (2)

fillic2002
fillic2002

Reputation: 11

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

Upvotes: 1

Cantinos
Cantinos

Reputation: 282

I didn't fine for now why Guid got problems... but I fine a poor alternative way for now: Instead of :

q.QueryString(qs => qs.MinimumShouldMatchPercentage(98).Query(userQuery))
&& q.Term(i => i.UserId, userId)

I do a double QueryString:

q.QueryString(qs => qs.MinimumShouldMatchPercentage(98).Query(userQuery))
&& q.QueryString(qs => qs.MinimumShouldMatchPercentage(100).Query(" \"" + userId+ "\""))

Upvotes: 0

Related Questions