Skip to content

Commit

Permalink
Added Contain, ContainsIgnoreCase and Exists properties to SolrFacetF…
Browse files Browse the repository at this point in the history
…ieldQuery. Also created tests for those.
  • Loading branch information
maticb authored and maticb committed May 9, 2018
1 parent eba827d commit 9f2c8a9
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 4 deletions.
48 changes: 47 additions & 1 deletion SolrNet.Tests/SolrFacetFieldQueryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -128,5 +128,51 @@ public void EnumCacheDF() {
Assert.Equal("f.pepe.facet.enum.cache.minDf", q[1].Key);
Assert.Equal("5", q[1].Value);
}

[Fact]
public void Contains()
{
var fq = new SolrFacetFieldQuery("pepe") { Contains = "cont" };
var q = Serialize(fq);
Assert.Equal(2, q.Count);
Assert.Equal("facet.field", q[0].Key);
Assert.Equal("pepe", q[0].Value);
Assert.Equal("f.pepe.facet.contains", q[1].Key);
Assert.Equal("cont", q[1].Value);
}

[Fact]
public void ContainsIgnoreCase()
{
var fq = new SolrFacetFieldQuery("pepe") { ContainsIgnoreCase = true };
var q = Serialize(fq);
Assert.Equal(1, q.Count);
Assert.Equal("facet.field", q[0].Key);
Assert.Equal("pepe", q[0].Value);

fq = new SolrFacetFieldQuery("pepe") { ContainsIgnoreCase = true, Contains = "cont" };
q = Serialize(fq);
Assert.Equal(3, q.Count);
Assert.Equal("facet.field", q[0].Key);
Assert.Equal("pepe", q[0].Value);

Assert.Equal("f.pepe.facet.contains", q[1].Key);
Assert.Equal("cont", q[1].Value);

Assert.Equal("f.pepe.facet.contains.ignoreCase", q[2].Key);
Assert.Equal("true", q[2].Value);
}

[Fact]
public void Exists()
{
var fq = new SolrFacetFieldQuery("pepe") { Exists = true };
var q = Serialize(fq);
Assert.Equal(2, q.Count);
Assert.Equal("facet.field", q[0].Key);
Assert.Equal("pepe", q[0].Value);
Assert.Equal("f.pepe.facet.exists", q[1].Key);
Assert.Equal("true", q[1].Value);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ public override IEnumerable<KeyValuePair<string, string>> Serialize(SolrFacetFie
yield return KV.Create(string.Format("f.{0}.facet.missing", fieldWithoutLocalParams), q.Missing.ToString().ToLowerInvariant());
if (q.EnumCacheMinDf.HasValue)
yield return KV.Create(string.Format("f.{0}.facet.enum.cache.minDf", fieldWithoutLocalParams), q.EnumCacheMinDf.ToString());
if (!string.IsNullOrEmpty(q.Contains))
yield return KV.Create(string.Format("f.{0}.facet.contains", fieldWithoutLocalParams), q.Contains);
if (!string.IsNullOrEmpty(q.Contains) && q.ContainsIgnoreCase.HasValue)
yield return KV.Create(string.Format("f.{0}.facet.contains.ignoreCase", fieldWithoutLocalParams), q.ContainsIgnoreCase.ToString().ToLowerInvariant());
if (q.Exists.HasValue)
yield return KV.Create(string.Format("f.{0}.facet.exists", fieldWithoutLocalParams), q.Exists.ToString().ToLowerInvariant());
}
}
}
}
19 changes: 17 additions & 2 deletions SolrNet/SolrFacetFieldQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,20 @@ public string Field {
/// </summary>
public int? EnumCacheMinDf { get; set; }

}
}
/// <summary>
/// This parameter limits the terms on which to facet to those containing the given substring.
/// This does not limit the query in any way, only the facets that would be returned in response to the query.
/// </summary>
public string Contains { get; set; }

/// <summary>
/// If facet.contains is used, the facet.contains.ignoreCase parameter causes case to be ignored when matching the given substring against candidate facet terms.
/// </summary>
public bool? ContainsIgnoreCase { get; set; }

/// <summary>
/// To cap facet counts by 1, specify facet.exists=true. It can be used with facet.method=enum or when it’s omitted. It can be used only on non-trie fields (such as strings). It may speed up facet counting on large indices and/or high-cardinality facet values..
/// </summary>
public bool? Exists { get; set; }
}
}

0 comments on commit 9f2c8a9

Please sign in to comment.