Skip to content

Commit

Permalink
Fixes: #16
Browse files Browse the repository at this point in the history
  • Loading branch information
Shazwazza committed May 6, 2015
1 parent e5482ea commit 9111f71
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 9 deletions.
129 changes: 128 additions & 1 deletion Projects/Examine.Test/Search/FluentApiTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

namespace Examine.Test.Search
{

[TestFixture]
public class FluentApiTests : AbstractPartialTrustFixture<FluentApiTests>
{
Expand All @@ -35,12 +36,138 @@ public class FluentApiTests : AbstractPartialTrustFixture<FluentApiTests>
// Assert.IsTrue(results.TotalItemCount > 0);
//}

[Test]
public void FluentApi_Grouped_Or_Query_Output()
{
Console.WriteLine("GROUPED OR - SINGLE FIELD, MULTI VAL");
var criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(id:1 id:2 id:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED OR - MULTI FIELD, MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(id:1 id:2 id:3 parentID:1 parentID:2 parentID:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED OR - MULTI FIELD, EQUAL MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedOr(new[] { "id", "parentID", "blahID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(id:1 id:2 id:3 parentID:1 parentID:2 parentID:3 blahID:1 blahID:2 blahID:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED OR - MULTI FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedOr(new[] { "id", "parentID" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(id:1 parentID:1)", criteria.Query.ToString());

Console.WriteLine("GROUPED OR - SINGLE FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedOr(new[] { "id" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(id:1)", criteria.Query.ToString());
}

/// <summary>
/// Grouped AND is a special case as well since NOT and OR include all values, it doesn't make
/// logic sense that AND includes all fields and values because nothing would actually match.
/// i.e. +id:1 +id2 --> Nothing matches
/// </summary>
[Test]
public void FluentApi_Grouped_And_Query_Output()
{
Console.WriteLine("GROUPED AND - SINGLE FIELD, MULTI VAL");
var criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedAnd(new[] { "id" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(+id:1)", criteria.Query.ToString());

Console.WriteLine("GROUPED AND - MULTI FIELD, EQUAL MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedAnd(new[] { "id", "parentID", "blahID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(+id:1 +parentID:2 +blahID:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED AND - MULTI FIELD, MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedAnd(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(+id:1 +parentID:2)", criteria.Query.ToString());

Console.WriteLine("GROUPED AND - MULTI FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedAnd(new[] { "id", "parentID" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(+id:1 +parentID:1)", criteria.Query.ToString());

Console.WriteLine("GROUPED AND - SINGLE FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedAnd(new[] { "id" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias +(+id:1)", criteria.Query.ToString());
}

/// <summary>
/// CANNOT BE A MUST WITH NOT i.e. +(-id:1 -id:2 -id:3) --> That will not work with the "+"
/// </summary>
[Test]
[TestOnlyInFullTrust]
public void FluentApi_Grouped_Not_Query_Output()
{
Console.WriteLine("GROUPED NOT - SINGLE FIELD, MULTI VAL");
var criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedNot(new[] { "id" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias (-id:1 -id:2 -id:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED NOT - MULTI FIELD, MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias (-id:1 -id:2 -id:3 -parentID:1 -parentID:2 -parentID:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED NOT - MULTI FIELD, EQUAL MULTI VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedNot(new[] { "id", "parentID", "blahID" }.ToList(), new[] { "1", "2", "3" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias (-id:1 -id:2 -id:3 -parentID:1 -parentID:2 -parentID:3 -blahID:1 -blahID:2 -blahID:3)", criteria.Query.ToString());

Console.WriteLine("GROUPED NOT - MULTI FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedNot(new[] { "id", "parentID" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias (-id:1 -parentID:1)", criteria.Query.ToString());

Console.WriteLine("GROUPED NOT - SINGLE FIELD, SINGLE VAL");
criteria = (LuceneSearchCriteria)_searcher.CreateSearchCriteria("content");
criteria.NodeTypeAlias("myDocumentTypeAlias");
criteria.GroupedNot(new[] { "id" }.ToList(), new[] { "1" });
Console.WriteLine(criteria.Query);
Assert.AreEqual("+__NodeTypeAlias:mydocumenttypealias (-id:1)", criteria.Query.ToString());
}

[Test]
public void FluentApi_Grouped_Or_With_Not()
{
//paths contain punctuation, we'll escape it and ensure an exact match
var criteria = _searcher.CreateSearchCriteria("content");
var filter = criteria.GroupedOr(new string[] { "nodeName", "bodyText", "headerText" }, "ipsum").Not().Field("umbracoNaviHide", "1");
var filter = criteria.GroupedOr(new [] { "nodeName", "bodyText", "headerText" }, "ipsum").Not().Field("umbracoNaviHide", "1");
var results = _searcher.Search(filter.Compile());
Assert.AreEqual(1, results.TotalItemCount);
}
Expand Down
6 changes: 3 additions & 3 deletions Projects/Examine/Config/ExamineSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ public class ExamineSettings : ConfigurationSection

#region Singleton definition

private static readonly ExamineSettings m_Examine;
private static readonly ExamineSettings Examine;
private ExamineSettings() { }
static ExamineSettings()
{
m_Examine = ConfigurationManager.GetSection(SectionName) as ExamineSettings;
Examine = ConfigurationManager.GetSection(SectionName) as ExamineSettings;

}
/// <summary>
Expand All @@ -28,7 +28,7 @@ static ExamineSettings()
/// <value>The instance.</value>
public static ExamineSettings Instance
{
get { return m_Examine; }
get { return Examine; }
}

#endregion
Expand Down
4 changes: 2 additions & 2 deletions Projects/Examine/LuceneEngine/SearchCriteria/LuceneQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ public IBooleanOperation GroupedNot(IEnumerable<string> fields, params string[]
{
fieldVals.Add(new ExamineValue(Examineness.Explicit, f));
}
return this.search.GroupedNotInternal(fields.ToArray(), fieldVals.ToArray(), this.occurance);
return this.search.GroupedNotInternal(fields.ToArray(), fieldVals.ToArray());
}

/// <summary>
Expand All @@ -389,7 +389,7 @@ public IBooleanOperation GroupedNot(IEnumerable<string> fields, params string[]
[SecuritySafeCritical]
public IBooleanOperation GroupedNot(IEnumerable<string> fields, params IExamineValue[] query)
{
return this.search.GroupedNotInternal(fields.ToArray(), query, this.occurance);
return this.search.GroupedNotInternal(fields.ToArray(), query);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -657,17 +657,21 @@ public IBooleanOperation GroupedNot(IEnumerable<string> fields, params IExamineV
Enforcer.ArgumentNotNull(fields, "fields");
Enforcer.ArgumentNotNull(query, "query");

return this.GroupedNotInternal(fields.ToArray(), query, _occurance);
return this.GroupedNotInternal(fields.ToArray(), query);
}

[SecuritySafeCritical]
protected internal IBooleanOperation GroupedNotInternal(string[] fields, IExamineValue[] fieldVals, BooleanClause.Occur occurance)
protected internal IBooleanOperation GroupedNotInternal(string[] fields, IExamineValue[] fieldVals)
{
//if there's only 1 query text we want to build up a string like this:
//(!field1:query !field2:query !field3:query)
//but Lucene will bork if you provide an array of length 1 (which is != to the field length)

Query.Add(GetMultiFieldQuery(fields, fieldVals, BooleanClause.Occur.MUST_NOT), occurance);
Query.Add(GetMultiFieldQuery(fields, fieldVals, BooleanClause.Occur.MUST_NOT, true),
//NOTE: This is important because we cannot prefix a + to a group of NOT's, that doesn't work.
// for example, it cannot be: +(-id:1 -id:2 -id:3)
// it just needs to be (-id:1 -id:2 -id:3)
BooleanClause.Occur.SHOULD);

return new LuceneBooleanOperation(this);
}
Expand Down

0 comments on commit 9111f71

Please sign in to comment.