Skip to content

Commit

Permalink
added TagQuery.HasAllOr
Browse files Browse the repository at this point in the history
  • Loading branch information
Hendy committed Mar 5, 2019
1 parent 77ce6ad commit 53e2ed5
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 4 deletions.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,17 @@ lookQuery.TagQuery = new TagQuery() {
// must have all these tags
HasAll = TagQuery.MakeTags("colour:red", "colour:blue"),

// must have all tags from at least one of these collections
HasAllOr = new LookTag[][] {
TagQuery.MakeTags("colour:red", "size:large"),
TagQuery.MakeTags("colour:red", "size:small")
}

// must have at least one of these tags
HasAny = TagQuery.MakeTags("color:green", "colour:yellow"),

// must have at least one tag from each collection
HasAnyAnd= new LookTag[][] {
HasAnyAnd = new LookTag[][] {
TagQuery.MakeTags("colour:red", "size:large"),
TagQuery.MakeTags("colour:red", "size:medium")
},
Expand Down
93 changes: 91 additions & 2 deletions src/Our.Umbraco.Look.Tests/QueryTests/TagQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ public void Has_Tags()
Assert.IsTrue(new LookQuery(TestHelper.GetSearchingContext()) { TagQuery = new TagQuery() }.Search().TotalItemCount > 0);
}


[TestMethod]
public void Has()
{
Expand Down Expand Up @@ -64,14 +63,70 @@ public void Has_All()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery() { HasAll = new [] { new LookTag("shape", "circle") } };
lookQuery.TagQuery = new TagQuery() { HasAll = TagQuery.MakeTags("shape:circle") };

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(3, lookResult.TotalItemCount);
}

[TestMethod]
public void Has_All_Or()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery()
{
HasAllOr = new LookTag[][]
{
TagQuery.MakeTags("shape:circle", "size:large"),
TagQuery.MakeTags("shape:square", "size:large"),
TagQuery.MakeTags("unknown")
}
};

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(2, lookResult.TotalItemCount);
}

[TestMethod]
public void Has_All_Or_Empty_Outer_Collection()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery()
{
Has = new LookTag("shape"), // HACK: need to clear index on startup of this class
HasAllOr = new LookTag[][] { }
};

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(9, lookResult.TotalItemCount);
}

[TestMethod]
public void Has_All_Or_Empty_Inner_Collections()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery()
{
Has = new LookTag("shape"), // HACK: need to clear index on startup of this class
HasAllOr = new LookTag[][] { new LookTag[] { } }
};

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(9, lookResult.TotalItemCount);
}


[TestMethod]
public void Has_Any()
{
Expand Down Expand Up @@ -107,6 +162,40 @@ public void Has_Any_And()
Assert.AreEqual(4, lookResult.TotalItemCount);
}

[TestMethod]
public void Has_Any_And_Empty_Outer_Collection()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery()
{
Has = new LookTag("shape"), // HACK: need to clear index on startup of this class
HasAnyAnd = new LookTag[][] { }
};

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(9, lookResult.TotalItemCount);
}

[TestMethod]
public void Has_Any_And_Empty_Inner_Collections()
{
var lookQuery = new LookQuery(TestHelper.GetSearchingContext());

lookQuery.TagQuery = new TagQuery()
{
Has = new LookTag("shape"), // HACK: need to clear index on startup of this class
HasAnyAnd = new LookTag[][] { new LookTag[] { } }
};

var lookResult = lookQuery.Search();

Assert.IsTrue(lookResult.Success);
Assert.AreEqual(9, lookResult.TotalItemCount);
}

[TestMethod]
public void Not_Any()
{
Expand Down
34 changes: 33 additions & 1 deletion src/Our.Umbraco.Look/Services/LookService_Search.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ public static LookResult Search(LookQuery lookQuery)

query.Add(new TermQuery(new Term(LookConstants.HasNodeField, "1")), BooleanClause.Occur.MUST);

// Types
if (lookQuery.NodeQuery.Types != null && lookQuery.NodeQuery.Types.Any())
{
var nodeTypeQuery = new BooleanQuery();
Expand All @@ -106,6 +107,7 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(nodeTypeQuery, BooleanClause.Occur.MUST);
}

// Detached
switch (lookQuery.NodeQuery.DetachedQuery)
{
case DetachedQuery.ExcludeDetached:
Expand All @@ -125,6 +127,7 @@ public static LookResult Search(LookQuery lookQuery)
break;
}

// Cultures
if (lookQuery.NodeQuery.Cultures != null && lookQuery.NodeQuery.Cultures.Any())
{
var nodeCultureQuery = new BooleanQuery();
Expand All @@ -140,6 +143,7 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(nodeCultureQuery, BooleanClause.Occur.MUST);
}

// Aliases
if (lookQuery.NodeQuery.Aliases != null && lookQuery.NodeQuery.Aliases.Any())
{
var nodeAliasQuery = new BooleanQuery();
Expand All @@ -154,6 +158,7 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(nodeAliasQuery, BooleanClause.Occur.MUST);
}

// Ids
if (lookQuery.NodeQuery.Ids != null && lookQuery.NodeQuery.Ids.Any())
{
if (lookQuery.NodeQuery.NotIds != null)
Expand All @@ -178,6 +183,7 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(idQuery, BooleanClause.Occur.MUST);
}

// Keys
if (lookQuery.NodeQuery.Keys != null && lookQuery.NodeQuery.Keys.Any())
{
if (lookQuery.NodeQuery.NotKeys != null)
Expand All @@ -202,6 +208,7 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(keyQuery, BooleanClause.Occur.MUST);
}

// NotIds
if (lookQuery.NodeQuery.NotIds != null && lookQuery.NodeQuery.NotIds.Any())
{
foreach (var exculudeId in lookQuery.NodeQuery.NotIds)
Expand All @@ -212,6 +219,7 @@ public static LookResult Search(LookQuery lookQuery)
}
}

// NotKeys
if (lookQuery.NodeQuery.NotKeys != null && lookQuery.NodeQuery.NotKeys.Any())
{
foreach (var excludeKey in lookQuery.NodeQuery.NotKeys)
Expand Down Expand Up @@ -434,6 +442,31 @@ public static LookResult Search(LookQuery lookQuery)
}
}

// HasAllOr
if (lookQuery.TagQuery.HasAllOr != null && lookQuery.TagQuery.HasAllOr.Any() && lookQuery.TagQuery.HasAllOr.SelectMany(x => x).Any())
{
var orQuery = new BooleanQuery();

foreach (var tagCollection in lookQuery.TagQuery.HasAllOr)
{
if (tagCollection.Any())
{
var allTagQuery = new BooleanQuery();

foreach (var tag in tagCollection)
{
allTagQuery.Add(
new TermQuery(new Term(LookConstants.TagsField + tag.Group, tag.Name)),
BooleanClause.Occur.MUST);
}

orQuery.Add(allTagQuery, BooleanClause.Occur.SHOULD);
}
}

query.Add(orQuery, BooleanClause.Occur.MUST);
}

// HasAny
if (lookQuery.TagQuery.HasAny != null && lookQuery.TagQuery.HasAny.Any())
{
Expand Down Expand Up @@ -468,7 +501,6 @@ public static LookResult Search(LookQuery lookQuery)
query.Add(anyTagQuery, BooleanClause.Occur.MUST);
}
}

}

// NotAny
Expand Down
6 changes: 6 additions & 0 deletions src/Our.Umbraco.Look/TagQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ public class TagQuery
/// </summary>
public LookTag[] HasAll { get; set; }

/// <summary>
/// Must have all tags in at least one of these collections
/// </summary>
public LookTag[][] HasAllOr { get; set; }

/// <summary>
/// Must have at least one tag from the collection
/// </summary>
Expand Down Expand Up @@ -84,6 +89,7 @@ public override bool Equals(object obj)
&& tagQuery.Has == this.Has
&& tagQuery.Not == this.Not
&& tagQuery.HasAll.BothNullOrElementsEqual(this.HasAll)
&& tagQuery.HasAllOr.BothNullOrElementCollectionsEqual(this.HasAllOr)
&& tagQuery.HasAny.BothNullOrElementsEqual(this.HasAny)
&& tagQuery.HasAnyAnd.BothNullOrElementCollectionsEqual(this.HasAnyAnd)
&& tagQuery.NotAny.BothNullOrElementsEqual(this.NotAny)
Expand Down

0 comments on commit 53e2ed5

Please sign in to comment.