Skip to content

Commit

Permalink
Adds methods to builder
Browse files Browse the repository at this point in the history
  • Loading branch information
sam-drumm committed Jul 4, 2024
1 parent 1c09e23 commit 1ac6343
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ public interface IQueryBuilder<T> where T : class
{
public IQueryBuilder<T> WithWildstarQuery(string searchText, List<string> fields, TextQueryType textQueryType = TextQueryType.MostFields);

public IQueryBuilder<T> WithWildstarBoolQuery(string searchText, List<string> fields, int? minimumShouldMatch = null, TextQueryType textQueryType = TextQueryType.MostFields);

public IQueryBuilder<T> WithFilterQuery(string commaSeparatedFilters, List<string> fields, TextQueryType textQueryType = TextQueryType.MostFields);

public IQueryBuilder<T> WithExactQuery(string searchText, List<string> fields, IExactSearchQuerystringProcessor processor = null, TextQueryType textQueryType = TextQueryType.MostFields);
Expand Down
33 changes: 33 additions & 0 deletions Hackney.Core/Hackney.Core.ElasticSearch/QueryBuilder.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Hackney.Core.ElasticSearch.Interfaces;
using Nest;

Expand Down Expand Up @@ -29,6 +30,16 @@ public IQueryBuilder<T> WithWildstarQuery(string searchText, List<string> fields
return this;
}

public IQueryBuilder<T> WithWildstarBoolQuery(string searchText, List<string> fields, int? minimumShouldMatch = 1, TextQueryType textQueryType = TextQueryType.MostFields)
{
var listOfWildCardedWords = _wildCardAppenderAndPrepender.Process(searchText);

_wildstarQuery = CreateWildcardBoolQuery(listOfWildCardedWords, fields);

return this;
}


public IQueryBuilder<T> WithFilterQuery(string commaSeparatedFilters, List<string> fields, TextQueryType textQueryType = TextQueryType.MostFields)
{
if (commaSeparatedFilters != null)
Expand Down Expand Up @@ -107,5 +118,27 @@ public QueryContainer BuildSimpleQuery(QueryContainerDescriptor<T> containerDesc
}
).Query(searchTerm));
}

private static Func<QueryContainerDescriptor<T>, QueryContainer> CreateWildcardBoolQuery(
List<string> words, List<string> fields)
{
Func<QueryContainerDescriptor<T>, QueryContainer> query =
(containerDescriptor) => containerDescriptor.Bool(b => b
.Should(fields.Select(field =>
(QueryContainer)new BoolQuery
{
Should = words.Select(word =>
(QueryContainer)new WildcardQuery
{
Value = word,
Field = field
}).ToList(),
MinimumShouldMatch = words.Count
}).ToArray()
)
);

return query;
}
}
}

0 comments on commit 1ac6343

Please sign in to comment.