public async Task <List <PostDto> > GetAll(string query) { var search = _elasticSearchService.Client .Search <PostDto>(s => s.Index("posts") .Query(q => q.MultiMatch(c => c .Fields(p => p.Field(f => f.Name)) .Analyzer("standard") .Boost(1.1) .Query(query) .Fuzziness(Fuzziness.AutoLength(3, 6)) .Lenient() .FuzzyTranspositions() .MinimumShouldMatch(2) .Operator(Operator.Or) .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("named_query") .AutoGenerateSynonymsPhraseQuery(false) ) ) ); if (!search.IsValid) { return(null); } var ids = search.Documents.Select(d => d.Id.ToString()).ToList(); var posts = await _postRepository.GetAll(ids); return(_mapper.Map <List <PostDto> >(posts)); }
public IResponse FuzzyQuerySample(ElasticClient client, string index) { return(client.Search <Person>(s => s .Index(index) .Query(q => q .Match(match => match .Field(p => p.About) .Fuzziness(Fuzziness.AutoLength(1, 2)) .Query("labor"))))); }
protected override QueryContainer QueryFluent(QueryContainerDescriptor <Project> q) => q .MatchBoolPrefix(c => c .Field(p => p.Description) .Analyzer("standard") .Boost(1.1) .Query("lorem ips") .Fuzziness(Fuzziness.AutoLength(3, 6)) .FuzzyTranspositions() .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("named_query") );
protected override QueryContainer QueryFluent(QueryContainerDescriptor <Project> q) => q .Match(c => c .Field(p => p.Description) .Analyzer("standard") .Boost(1.1) .Query("hello world") .Fuzziness(Fuzziness.AutoLength(3, 6)) .Lenient() .FuzzyTranspositions() .MinimumShouldMatch(2) .Operator(Operator.Or) .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("named_query") .AutoGenerateSynonymsPhraseQuery(false) );
public ISearchResponse <DocumentModel> Match(string query) { return(client .Search <DocumentModel>(s => s.Query(q => q.Match(c => c .Field(p => p.Attachment.Content) .Analyzer("standard") .Boost(1.1) .CutoffFrequency(0.001) .Query(query) .Fuzziness(Fuzziness.AutoLength(3, 6)) .Lenient() .FuzzyTranspositions() .MinimumShouldMatch(2) .Operator(Operator.Or) .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("match_document_content_query") ) ) .Highlight(h => h .PreTags("<mark>") .PostTags("</mark>") .Encoder(HighlighterEncoder.Html) .Fields( fs => fs .Field(p => p.Attachment.Content) .Type("plain") .ForceSource() .FragmentSize(150) .Fragmenter(HighlighterFragmenter.Span) .NumberOfFragments(3) .NoMatchSize(150) ) ) )); }
public async Task <SearchResponse <Specimen> > SearchAsync(FindParams <Data.Shared.Models.Specimen> findParams, Data.Shared.Models.User user) { var specimenFindParams = findParams as SpecimenFindParams; var searchTerm = findParams.SearchText; var musts = GetFilters(findParams); var shoulds = new List <QueryContainer>(); var query = new QueryContainerDescriptor <Specimen>(); if (!string.IsNullOrEmpty(searchTerm)) { var fields = new FieldsDescriptor <Specimen>(); fields = fields.Field(f => f.Name) .Field(f => f.Lifeform.CommonName) .Field(f => f.Lifeform.ScientificName); if (findParams.UseNGrams) { fields = fields.Field("name.nameSearch"); } shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields) .Query(searchTerm) .Fuzziness(Fuzziness.AutoLength(1, 5)))); } musts.Add(FilterByVisibility(query, user)); var searchDescriptor = new SearchDescriptor <Specimen>() .Query(q => q .Bool(b => b .Should(shoulds.ToArray()) .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1))); var countDescriptor = new CountDescriptor <Specimen>() .Query(q => q .Bool(b => b .Should(shoulds.ToArray()) .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1))); // Sort var specimenSorts = GetSpecimenSorts(); if (!string.IsNullOrEmpty(findParams.SortBy)) { if (findParams.SortDirection == SortDirection.Ascending) { searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Ascending())); } else { searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts[findParams.SortBy]).Descending())); } } else if (string.IsNullOrEmpty(findParams.SearchText)) { searchDescriptor.Sort(s => s.Field(f => f.Field(specimenSorts["DateModified"]).Descending()).Field(f => f.Field(specimenSorts["DateCreated"]).Descending())); } var aggregations = new AggregationContainerDescriptor <Specimen>(); var searchFilters = new List <SearchFilter <Specimen> > { new SearchValueFilter <Specimen, string>("Stage", "specimenStage", specimenFindParams.Filters.StageFilter.Value) }; foreach (var filter in searchFilters) { if (filter is NestedSearchValueFilter <Specimen, string> nestedFilter) { aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is SearchRangeFilter <Specimen, double> searchRangeFilter) { aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is SearchValueFilter <Specimen, string> searchValueFilter) { aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations); } } searchDescriptor.Aggregations(a => aggregations); var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor); response.AggregationResult = ProcessAggregations(response, specimenFindParams); return(response); }
public async Task <SearchResponse <PlantInfo> > SearchAsync(FindParams <Data.Shared.Models.PlantInfo> findParams, Data.Shared.Models.User user) { var plantInfoFindParams = findParams as PlantInfoFindParams; var searchTerm = findParams.SearchText; var shoulds = new List <QueryContainer>(); var query = new QueryContainerDescriptor <PlantInfo>(); if (!string.IsNullOrEmpty(searchTerm)) { var fields = new FieldsDescriptor <PlantInfo>(); fields = fields.Field(m => m.CommonName) .Field(m => m.ScientificName) .Field(m => m.Lifeform.CommonName) .Field(m => m.Lifeform.ScientificName) .Field("commonName.nameSearch") .Field("scientificName.nameSearch") .Field("lifeform.commonName.nameSearch") .Field("lifeform.scientificName.nameSearch"); shoulds.Add(query.MultiMatch(mm => mm.Fields(mmf => fields) .Query(searchTerm) .Fuzziness(Fuzziness.AutoLength(3, 5)))); shoulds.Add(query.Nested(n => n .Path(p => p.Synonyms) .Query(q => q .Match(sq => sq .Field("synonyms.name") .Query(searchTerm) .Fuzziness(Fuzziness.AutoLength(3, 5)))))); } var filters = plantInfoFindParams.Filters; var searchFilters = new List <SearchFilter <PlantInfo> > { new NestedSearchValueFilter <PlantInfo, string>(filters.RegionFilter.Name, "location.region.keyword", "plantLocations", filters.RegionFilter.Value), new SearchValuesFilter <PlantInfo, string>(filters.WaterFilter.Name, "waterTypes", filters.WaterFilter.MinimumValue, filters.WaterFilter.MaximumValue), new SearchValuesFilter <PlantInfo, string>(filters.LightFilter.Name, "lightTypes", filters.LightFilter.MinimumValue, filters.LightFilter.MaximumValue), new SearchValuesFilter <PlantInfo, string>(filters.BloomFilter.Name, "bloomTimes", filters.BloomFilter.MinimumValue?.ToString(), filters.BloomFilter.MaximumValue?.ToString()), new NestedSearchValueFilter <PlantInfo, string>(filters.ZoneFilter.Name, "id", "zones", filters.ZoneFilter.Value?.ToString()), new SearchRangeFilter <PlantInfo, double>(filters.HeightFilter.Name, "minHeight", "maxHeight", filters.HeightFilter.Values, filters.HeightFilter.Value, filters.HeightFilter.MaximumValue), new SearchRangeFilter <PlantInfo, double>(filters.SpreadFilter.Name, "minSpread", "maxSpread", filters.SpreadFilter.Values, filters.SpreadFilter.Value, filters.SpreadFilter.MaximumValue), new NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus>(filters.NativeFilter.Name, "location.stateOrProvince.keyword", "plantLocations", "status", filters.NativeFilter.Value, filters.NativeFilter.Status) }; var musts = GetFilters(plantInfoFindParams, searchFilters); musts.Add(FilterByVisibility(query, user)); if (plantInfoFindParams.Lifeform != null) { musts.Add(query.Bool(b => b.Must(m => m.Term(t => t.Lifeform.Id, plantInfoFindParams.Lifeform.LifeformId)))); } var searchDescriptor = new SearchDescriptor <PlantInfo>() .Query(q => q .Bool(b => b .Should(shoulds.ToArray()) .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1))); var countDescriptor = new CountDescriptor <PlantInfo>() .Query(q => q .Bool(b => b .Should(shoulds.ToArray()) .Must(musts.ToArray()).MinimumShouldMatch(string.IsNullOrEmpty(searchTerm) ? 0 : 1))); var aggregations = new AggregationContainerDescriptor <PlantInfo>(); foreach (var filter in searchFilters) { if (filter is NestedSearchMultiValueFilter <PlantInfo, string, LocationStatus> nestedMultiFilter) { aggregations = nestedMultiFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is NestedSearchValueFilter <PlantInfo, string> nestedFilter) { aggregations = nestedFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is SearchRangeFilter <PlantInfo, double> searchRangeFilter) { aggregations = searchRangeFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is SearchValuesFilter <PlantInfo, string> searchValuesFilter) { aggregations = searchValuesFilter.ToAggregationContainerDescriptor(aggregations); } else if (filter is SearchValueFilter <PlantInfo, string> searchValueFilter) { aggregations = searchValueFilter.ToAggregationContainerDescriptor(aggregations); } } searchDescriptor.Aggregations(a => aggregations); // Sort var plantInfoSorts = GetPlantInfoSorts(); if (!string.IsNullOrEmpty(findParams.SortBy)) { if (findParams.SortDirection == SortDirection.Ascending) { searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Ascending())); } else { searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts[findParams.SortBy]).Descending())); } } else if (string.IsNullOrEmpty(findParams.SearchText)) { searchDescriptor.Sort(s => s.Field(f => f.Field(plantInfoSorts["DateModified"]).Descending()).Field(f => f.Field(plantInfoSorts["DateCreated"]).Descending())); } var response = await _searchClient.SearchAsync(pi => searchDescriptor.Skip(findParams.Skip).Take(findParams.Take), pi => countDescriptor); response.AggregationResult = ProcessAggregations(response, plantInfoFindParams); return(response); }
public List <Person> Match1(string name) { var result = _ESClientProvider.GetClient().Search <Person>(s => s .Query(q => q .Match(m => m .Field(f => f.Name) .Analyzer("standard") .Boost(1.1) .Query(name) .Fuzziness(Fuzziness.AutoLength(3, 6)) .Lenient() .FuzzyTranspositions() .MinimumShouldMatch(2) .Operator(Operator.Or) .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("Match") .AutoGenerateSynonymsPhraseQuery(false) ) ) ); return(result.Documents.ToList()); }
public List <Person> MatchBoolPrefix1(string name) { var result = _ESClientProvider.GetClient().Search <Person>(s => s .Size(10) .Query(q => q .MatchBoolPrefix(c => c .Field(p => p.Name) .Analyzer("standard") .Boost(1.1) .Query(name) .Fuzziness(Fuzziness.AutoLength(3, 6)) .FuzzyTranspositions() .FuzzyRewrite(MultiTermQueryRewrite.TopTermsBlendedFreqs(10)) .Name("MatchBoolPrefix") ) ) ); return(result.Documents.ToList()); }
public async Task <IEnumerable <CatalogItem> > SearchAsync(string Phrase) { var response = await elasticClient.SearchAsync <CatalogItem>(s => s .Query(q => q .MultiMatch(c => c .Fields(t => t.Field(m => m.Name) .Field(m => m.CatalogBrand.Brand) .Field(m => m.CatalogType.Type)) .Query(Phrase) .Fuzziness(Fuzziness.AutoLength(1, 5))) || q.Term(x => x.Description, Phrase))); return(response.Documents); }