private static QueryContainer MakeQuery(EssaySearchRequest searchRequest, QueryContainerDescriptor <EssayDocument> q) { return(q.Bool(b => b .Filter(f => f .Terms(t => t .Name("termsToMatch") .Field(ed => ed.Terms) .Terms(searchRequest.Terms))) .Must(m => m .MultiMatch(dm => dm .Query(searchRequest.Query) .Fields(fld => fld .Field(ed => ed.Title, 2) .Field(ed => ed.Content)) .TieBreaker(0.3))))); }
/// <summary> /// Searches essays using terms aggregate. /// /// <remarks> /// Size parameter is used with an exact value to eliminate accuracy error arizing from shard recombination. /// https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html#_size /// </remarks> /// </summary> public async Task <EssaySearchResponse> SearchEssays(EssaySearchRequest searchRequest) { var client = _setup.GetClient(); var result = await client.SearchAsync <EssayDocument>( sd => sd .Query(q => MakeQuery(searchRequest, q)) .Source(MakeSource) .Aggregations(MakeAggregations)); var facets = result.Aggs.Terms <string>(MatchedTermsAggregateIdentifier); var essaySearchResponse = new EssaySearchResponse( result.Hits.Select(x => new EssayBrief(Guid.Parse(x.Id), x.Source.Title)).ToList(), facets.Buckets.Select(kb => new TermBucket(kb.Key, kb.DocCount.GetValueOrDefault())).ToList()); return(essaySearchResponse); }
public async Task <EssaySearchResponse> Get(string query, [FromQuery] string[] themes) { var searchRequest = new EssaySearchRequest(query, themes); return(await _indexer.SearchEssays(searchRequest)); }