public IEnumerable <string> SearchAsYouType(string content) { ElasticClient client = ElasticClientConnection.GetElasticClient(); var searchResponse = client.Search <Note>(s => s .Index("notes_sayt") .Query(q => q .MultiMatch(mm => mm .Query(content) .Type(TextQueryType.BoolPrefix) .Fields(f => f.Field("content").Field("content._2gram").Field("content._3gram")) .Fuzziness(Fuzziness.Auto) ) ) ); var resultsList = new List <string>(); foreach (var note in searchResponse.Hits) { resultsList.Add(note.Source.Content + " (Score: " + note.Score + ")"); } return(resultsList); }
public bool InsertNote(string content) { ElasticClient client = ElasticClientConnection.GetElasticClient(); var note = new Note(content); var indexResponse = client.IndexDocument(note); if (indexResponse.IsValid) { return(true); } return(false); }
public IEnumerable <string> GetAllNotes() { ElasticClient client = ElasticClientConnection.GetElasticClient(); var searchResponse = client.Search <Note>(s => s .MatchAll() ); var resultsList = new List <string>(); foreach (var note in searchResponse.Documents) { resultsList.Add(note.Content); } return(resultsList); }
public IEnumerable <string> GetNotes(string content) { ElasticClient client = ElasticClientConnection.GetElasticClient(); var searchResponse = client.Search <Note>(s => s .Query(q => q .Match(m => m .Field(f => f.Content) .Fuzziness(Fuzziness.Auto) .Operator(Operator.Or) .Query(content) ) || q .Match(m => m .Field(f => f.Content) .Fuzziness(Fuzziness.Auto) .Operator(Operator.And) .Query(content) ) || q .MatchPhrase(mp => mp .Field(f => f.Content) .Boost(2) .Query(content) ) ) ); //var searchResponse = client.Search<Note>(s => s // .Size(15) // .Query(q => q // .Match(m => m // .Field(f => f.Content) // .Fuzziness(Fuzziness.Auto) // .Query(content) // ) // ) //); var resultsList = new List <string>(); foreach (var note in searchResponse.Hits) { resultsList.Add(note.Source.Content + " (Score: " + note.Score + ")"); } return(resultsList); }
/// <summary> /// Updates document's popularity by increasing specific documents rank by 1 /// </summary> /// <param name="noteId">ID of the ES document</param> /// <returns>Returns true if update successful</returns> public bool UpdateNote(string noteId) { ElasticClient client = ElasticClientConnection.GetElasticClient(); var updateResponse = client.Update <Note>(noteId, u => u .Script(script => script .Source("ctx._source.rank += params.rank") .Params(p => p .Add("rank", 1) ) ) .RetryOnConflict(5) ); if (updateResponse.IsValid) { return(true); } return(false); }
/// <summary> /// Main search method for matching text with documents in the ES index. /// </summary> /// <param name="content">text that needs to be searched</param> /// <returns>Returns list of 10 most relevant notes</returns> public IEnumerable <Note> GetNotes(string content) { ElasticClient client = ElasticClientConnection.GetElasticClient(); var searchResponse = client.Search <Note>(s => s .Query(q => q .Match(m => m .Field(f => f.Content) .Fuzziness(Fuzziness.Auto) .Operator(Operator.Or) .Query(content) ) || q .Match(m => m .Field(f => f.Content) .Fuzziness(Fuzziness.Auto) .Operator(Operator.And) .Query(content) ) || q .MatchPhrase(mp => mp .Field(f => f.Content) .Boost(2) .Query(content) ) || q .RankFeature(rf => rf .Field(f => f.Rank) ) ) ); List <Note> resultList = new List <Note>(); foreach (var note in searchResponse.Hits) { resultList.Add(new Note { Id = note.Id, Content = note.Source.Content, Score = note.Score.ToString() }); } return(resultList); }
/// <summary> /// This method checks if the note exists in the ES index. If so, it updates the documents popularity, if not, it /// creates a new document in index. /// </summary> /// <param name="content">Content of the document that needs to be posted</param> /// <returns></returns> public bool PostNote(string content) { ElasticClient client = ElasticClientConnection.GetElasticClient(); char[] charsToTrim = { ',', '.', ' ', '@', ';', '!', '?', '"', ':' }; string trimmedContent = content.Trim(charsToTrim); var searchResponse = client.Search <Note>(s => s .Query(q => q .Term(t => t .Field(f => f.Content.Suffix("raw")) .Value(trimmedContent) ) ) ); if (searchResponse.IsValid) { if (searchResponse.Hits.Count > 0) { if (UpdateNote(searchResponse.Hits.FirstOrDefault().Id)) { return(true); } } else { if (InsertNote(trimmedContent)) { return(true); } } } return(false); }