Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/ts 1466 newly created temp accommodation does not appear in search #63

Open
wants to merge 3 commits into
base: release
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
using Elasticsearch.Net;
using Hackney.Core.ElasticSearch;
using Hackney.Core.ElasticSearch.Interfaces;
using Moq;
using Nest;
using Elasticsearch.Net;
using Hackney.Core.ElasticSearch;
using Hackney.Core.ElasticSearch.Interfaces;
using Moq;
using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using Xunit;

namespace Hackney.Core.Tests.ElasticSearch
{
public class QueryBuilderTests
{
private readonly QueryBuilder<TestSearchObject> _sut;
private readonly Mock<IWildCardAppenderAndPrepender> _wildcardAppenderAndPrependerMock;
private readonly QueryContainerDescriptor<TestSearchObject> _queryContainerDesc;
public QueryBuilderTests()
{
_wildcardAppenderAndPrependerMock = new Mock<IWildCardAppenderAndPrepender>();
_queryContainerDesc = new QueryContainerDescriptor<TestSearchObject>();
_sut = new QueryBuilder<TestSearchObject>(_wildcardAppenderAndPrependerMock.Object);
}
{
private readonly QueryBuilder<TestSearchObject> _sut;
private readonly Mock<IWildCardAppenderAndPrepender> _wildcardAppenderAndPrependerMock;
private readonly QueryContainerDescriptor<TestSearchObject> _queryContainerDesc;
public QueryBuilderTests()
{
_wildcardAppenderAndPrependerMock = new Mock<IWildCardAppenderAndPrepender>();
_queryContainerDesc = new QueryContainerDescriptor<TestSearchObject>();

_sut = new QueryBuilder<TestSearchObject>(_wildcardAppenderAndPrependerMock.Object);
}

[Theory]
[InlineData(TextQueryType.BestFields)]
[InlineData(TextQueryType.MostFields)]
Expand All @@ -36,15 +36,15 @@ public void WhenCreatingQuery_WithSpecifiedType_ResultantQueryShouldBeThatType(T
.Returns(new List<string> { "*4*", "*Annerdale*", "*House*" });

// Act
QueryContainer query = _sut.WithWildstarQuery(searchText, fields, queryType)
.Build(_queryContainerDesc);
// Assert
QueryContainer query = _sut.WithWildstarQuery(searchText, fields, queryType)
.Build(_queryContainerDesc);

// Assert
var container = (query as IQueryContainer).Bool.Should.First() as IQueryContainer;

Assert.Equal(queryType, container.QueryString.Type);
}
}

[Fact]
public void WhenCreatingQuery_WithWildstar_ResultantQueryShouldHaveOneSubquery()
{
Expand All @@ -55,16 +55,36 @@ public void WhenCreatingQuery_WithWildstar_ResultantQueryShouldHaveOneSubquery()
.Returns(new List<string> { "*12*", "*Pitcairn*", "*Street*" });

// Act
QueryContainer query = _sut.WithWildstarQuery(searchText, fields)
.Build(_queryContainerDesc);
QueryContainer query = _sut.WithWildstarQuery(searchText, fields)
.Build(_queryContainerDesc);

// Assert
var container = (query as IQueryContainer).Bool.Should;

Assert.Equal(2, container.Count());
Assert.Equal(1, container.Count(q => q != null));
}

}

[Fact]
public void WhenCreatingQuery_WithWildstarBool_ResultantQueryShouldHaveOneSubquery()
{
// Arrange
string searchText = "8 Westminster Lane";
var fields = new List<string> { "field1", "field2" };
_wildcardAppenderAndPrependerMock.Setup(x => x.Process(It.IsAny<string>()))
.Returns(new List<string> { "*8*", "*Westminster*", "*Lane*" });

// Act
QueryContainer query = _sut.WithWildstarBoolQuery(searchText, fields)
.Build(_queryContainerDesc);

// Assert
var container = (query as IQueryContainer).Bool.Should;

Assert.Equal(2, container.Count());
Assert.Equal(1, container.Count(q => q != null));
}

[Fact]
public void WhenCreatingSimpleQuery_WithWildstar_ResultantQueryBeOfSimpleType()
{
Expand All @@ -73,14 +93,14 @@ public void WhenCreatingSimpleQuery_WithWildstar_ResultantQueryBeOfSimpleType()
var fields = new List<string> { "field11", "field12" };

// Act
QueryContainer query = _sut.BuildSimpleQuery(_queryContainerDesc, searchText, fields);
QueryContainer query = _sut.BuildSimpleQuery(_queryContainerDesc, searchText, fields);

// Assert
var container = (query as IQueryContainer).SimpleQueryString;

Assert.NotNull(container);
Assert.Equal(2, container.Fields.Count());
Assert.Equal(searchText, container.Query);
}
Assert.Equal(2, container.Fields.Count());
Assert.Equal(searchText, container.Query);
}
}
}
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;
}
}
}
Loading