C# elastic search exact text match with nest - c#

I am using latest c# elastic search NEST library.
I am trying to search with exact text match, but currently it is working searching
for subset match. I want to do exact match.
Following is my code snippet:
public User GetUserByUsername(string username)
{
var client = new ElasticConnectionManager(this.configuration).GetClient(Constant.IndexUsers);
var searchResponse = client.Search<User>(s => s
.Query(q => q
.Bool(bq => bq
.Filter(f => f.Term(t => t.Username, username))
.Must(mt=>mt.Term(t2=> t2.Username, username)))));
//.Must(bs => bs.Term(t => t.Username, username))
if (searchResponse.Documents.Count > 0)
return searchResponse.Documents.First();
else
return null;
}
}

Try using the match_phrase query for exact text match. Your query should be similar to the following:
var searchResponse = client.Search<User>(s => s
.Query(q => q
.MatchPhrase(m => m
.Field(f => f.Username)
.Query(username))));

Related

elasticsearch NEST get nested document

situation is this. I have in elastic a group. each of these groups have a nested list of items.
Both group and items have an attribute named serial, which are unique.
I get a serial for the group and a serial for item, and with those 2 items i'm supposed to return the item.
Currently i'm doing it the following way:
public item findItem(string groupSerial, string itemSerial)
{
var searchResponse = _elasticClient.Search<Group>(s => s
.Index(_config.groupIndexName)
.Query(q => q
.ConstantScore(cs => cs
.Filter(f => f
.Term(t => t
.Field(fi => fi.serial)
.Value(groupSerial)
)
)
)
).Query(q => q
.Nested(c => c
.InnerHits(i => i.Explain())
.Path(p => p.items)
.Query(nq => nq.Term(t => t
.Field(field => field.items.First().serial)
.Value(itemSerial)))))
);
var result = searchResponse.Documents.FirstOrDefault();
return result?.items.Find(item => item.serial == itemSerial);
}
I get the feeling that there is supposed to be a more efficient way. Like getting the item straight from the search in elastic. Does anyone know how?

Exact search with ElasticSearch using EdgeNGram

I am using elastic for searching with greek and latin characters.My main problem is that I can't do exact searches.I am using edgeNgram filter on indexing, but I would like to control its min and max at search time according to my word length.For example if I type "titanox" I will firstly get "ΤΙΤΑΝΙΟΥ" and secondly "TITANOX".Here is my index creation :
var response = client.CreateIndex(index, s => s
.Settings(s1 => s1
.NumberOfShards(5)
.NumberOfReplicas(5)
.Analysis(a => a.TokenFilters(t => t.IcuTransform("greeklatin", it => it.Id("Greek-Latin; NFD; [:Nonspacing Mark:] Remove; NFC")//
.Direction(IcuTransformDirection.Forward)) //
.IcuTransform("latingreek", lg => lg.Id("Greek-Latin; NFD; [:Nonspacing Mark:] Remove; NFC")
.Direction(IcuTransformDirection.Reverse))
.EdgeNGram("greekedge", ed => ed.MaxGram(7)
.MinGram(1)
.Side(EdgeNGramSide.Front))
.Stop("greekstop", sw => sw.StopWords())
.Lowercase("greeklowercase", gl => gl.Language(Language.Greek.ToString()))
.KeywordMarker("greekkeywords", gk => gk.Keywords(""))
.Stemmer("greekstemmer", gs => gs.Language(Language.Greek.ToString())))
.Analyzers(a1 => a1
.Custom("greek", t => t.Tokenizer("standard")
.Filters("greekedge", "greekstop", "greeklowercase", "greekkeywords", "greekstemmer", "greeklatin")))))
.Mappings(m => m.Map(type, mt => mt.Properties(c => c.Text(c1 => c1.Name("id").Analyzer("greek"))
.Text(c2 => c2.Name("brand").Analyzer("greek"))
.Text(c3 => c3.Name("service").Analyzer("greek"))
.Text(c4 => c4.Name("servicegroupdesc").Analyzer("greek"))
.Text(c5 => c5.Name("servicecategorydesc).Analyzer("greek"))
.Text(c6 => c6.Name("partscategory").Analyzer("greek"))
.Text(c7 => c7.Name("partsid").Analyzer("greek"))
.Text(c8 => c8.Name("partsdesc").Analyzer("greek"))))));
and here my search
var response = client.Search<Cars>(n => n
.Index(index)
.Type(type)
.Query(m => m.MultiMatch(q => q
.Analyzer(analyzername)
//.MinimumShouldMatch("100%")
.Query("*" + searchWord + "*")
.Fields(f=>f.Field(fieldsForSearchList[0]))
.Fuzziness(Fuzziness.EditDistance(0))))
.Size(searchSize)
.From(0)
.TrackScores(true)
);
A solution that may can help is adding to this query a new query for boosting the word that users type.This can achieve more exact searches.

Highlight All fields Nest ElasticSearch

everybody
I'm working with Nest driver of elasticsearch for c#. In my project I don't have any document mapping so, if I want to highlight matching fields I should use this Json part in my query which highlights all fields:
"highlight":{
"fields":{
"*":{}
}
}
but I want to do it with nest. I use this code:
client.Search<dynamic>(s => s
.Index('my index name')
.Type('my doc type name')
.From(page*PageSize)
.Size(PageSize)
.Query(q => q
.QueryString(qs => qs.Query('my query')))
.Highlight(h => h
.OnFields(f => f
.OnAll()
.PreTags("<b style='color:black'>")
.PostTags("</b>")
)));
and it is not working for me, returned result contains hits, but doesn't contain any highlight :(
I guess you are looking for
client.Search<dynamic>(s => s
.Index('my index name')
.Type('my doc type name')
.From(page*PageSize)
.Size(PageSize)
.Query(q => q
.QueryString(qs => qs.Query('my query')))
.Highlight(h => h
.OnFields(f => f
.OnField("*")
.PreTags("<b style='color:black'>")
.PostTags("</b>")
)));
because .OnAll() means .OnField("_all").
Take a look
UPDATE: object initializer syntax(NEST 5.x)
var searchRequest = new SearchRequest
{
Query = ..
Highlight = new Highlight
{
PostTags = new[] {"<a>"},
PreTags = new[] {"</a>"},
Fields = new FluentDictionary<Field, IHighlightField>().Add("*", new HighlightField())
}
};

Elastic search using NEST with multiple parameters

I have an sql table with columns Name, Category, Location. I am using Elastic Search with NEST. my query like this:
var result = client.Search<Models.Search.Poll>(s => s.Query(q => q.Fuzzy(f => f.OnField(p => p.Name).Value(query))))));
So if there is a record with name = "We are here" and user search "are" , it returns result.
Now I need to add two more parameters category and location to this query:
so I made it like this:
var result = client.Search<Models.Search.Poll>(s => s.Query(q => q.Fuzzy(f => f.OnField(p => p.Name).Value(query).OnField(r => r.Category).Value(category))));
but it is not working with query field now. but it works with category now. here is what I get when I type name but dont select category:
StatusCode: OK,
Method: POST,
Url: http://server.abc.com:9200/pollit-dev/polls/_search,
Request: {
"query": {
"fuzzy": {
"category": {
"value": "Select a Category"
}
}
}
},
Response: {"took":2892,"timed_out":false,"_shards":{"total":5,"successful":5,"failed":0},"hits":{"total":0,"max_score":null,"hits":[]}}
I tried this one well:
var result = client.Search<Models.Search.Poll>(s => s.MatchAll().Query(q => q.Term(p => p.Name, query) || q.Term(p => p.Category,category) || q.Term(p => p.Location, Location)
but no luck so far.
Regards,
Asif Hameed
You have multiple options for that.
First one is almost like yours but you have to use a Bool condition there.
var result = client.Search<Models.Search.Poll>(s => s
.MatchAll()
.Query(q => q
.Bool(b => b
.Must(m => m.Term(p => p.Name, query) || m.Term(p => p.Category,category) || m.Term(p => p.Location, Location))
)
)
);
Now another option is to use queryContainers. Something like this:
var result = _Instance.Search<Models.Search.Poll>(q => q
.Query(qq =>
{
QueryContainer termQuery = null;
QueryContainer locationQuery = null;
QueryContainer categoryQuery = null;
termQuery = qq.Term(p => p.Name, query);
categoryQuery = qq.Term(p => p.Category,category);
locationQuery = qq.Term(p => p.Location, Location);
return termQuery || categoryQuery || locationQuery;
})
);
You can also elaborate the QueryContainers and add multiple search parameters there.
Hope this helps you. Great day!

Multi Terms search NEST C#

I want to do a search matching multiple values ( an array of values ) like this :
var result1 = _client.Search<type1>(s => s
.Fields(f => f.trip_id)
.Query(q => q
.Terms(t => t.arg1, value1)).Take(_allData))
.Documents.Select(d => d.arg2).ToArray();
var result2 = _client.Search<type2>(s => s
.Query(q => q
.Terms(t => t.arg3, result1))
.Take(_allData)
).Documents.Select(s => s.ar3).ToList();
How can I do ? I was thinking about facets but I don't see how I can do it.
The only way for now that works is with a foreach iterator which is not really effective...
Thanks for your help.
You can express multiple queries like so:
.Query(q=>q.Terms(t=>t.arg3, result1) && q.Terms(t=>t.arg1, value1))
Be sure to read the documentation on writing queries to discover all the good stuff NEST has to offer.
Orelus,
I'd like to use your solution with
.And( af=>af.Term(...), af=>af.Term(...) )
I don't understand where this fits, here's an example of my non-working filter
var results = client.Search<music>(s => s
.Query(q => q
.Filtered(f => f.
Filter(b => b.Bool(m => m.Must(
t => t
.Term(p => p.artist, artist)
&& t.Term(p2 => p2.year, year)
)
)
)
)
)
);

Categories

Resources