0
votes

I'm new to elasticSearch and nest. In a c# project I'm using Nest to query data to an elasticSearch database. I have no control over the database (I cannot add multifield or specify analyzer here). I have a text field, which can have value like "Some-Thing_A" or "Some-Thing_B" or "Other" etc, but always keyword-like value. I want to query the exact string (given by the user) "Some-Thing_A" on it. I was using the following query :

var searchResponse = client.Search<LogData>(s =>
                s.Index("indexxx")
                .Size(50)
                .Query(q =>
                    q.Match(m => m.Field(f => f.Branche).Query("Some-Thing_A")))));

which may return "Some-Thing_A" or "Some-Thing_B"...

I've tried using "term", but it doesn't work. I tried using analyzer like that :

client.Indices.Create("indexxx", c => c.Map<LogData>(m => m.AutoMap()));

var searchResponse = client.Search<LogData>(s =>
                s.Index("indexxx")
                .Size(50)
                .Query(q => q
                   .Match(m => m.Field(f => f.Branche).Analyzer("keyword").Query("Some-Thing_A"))
                );

and in my LogData class :

 [Text(Name = "Branche", Analyzer = "keyword", SearchAnalyzer ="keyword", Index = true)]
 public string Branche { get; set; }

But it always return 0 hit. Did I miss something ? Is there a way to use the keyword analyzer without having access to the database to change it from there ?

1

1 Answers

0
votes

After trying some more solution, I managed to use "term". I wasn't aware that elastic dynamic mapping was mapping a text field with both keyword and text type.

var searchResponse = client.Search<LogData>(s =>
                 s.Index("indexxx")
                 .Size(5000)
                 .Query(q => q.Term(term => term
                        .Field(field => field.Branche.Suffix("keyword"))
                        .Value("Some-Thing_A")))
                 .Sort(srt => srt
                    .Ascending(p => p.Timestamp))
                 );