-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search enhancement: pinned queries (#4143)
* Search enhancement: pinned queries Addresses elastic/elasticsearch#44345 * update docs in test * remove params overloads on Ids that already convert to Ids * add note to IdsQuery
- Loading branch information
Showing
10 changed files
with
152 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Runtime.Serialization; | ||
using Elasticsearch.Net.Utf8Json; | ||
|
||
namespace Nest | ||
{ | ||
[InterfaceDataContract] | ||
[ReadAs(typeof(PinnedQuery))] | ||
public interface IPinnedQuery : IQuery | ||
{ | ||
[DataMember(Name = "ids")] | ||
IEnumerable<Id> Ids { get; set; } | ||
|
||
[DataMember(Name = "organic")] | ||
QueryContainer Organic { get; set; } | ||
} | ||
|
||
public class PinnedQuery : QueryBase, IPinnedQuery | ||
{ | ||
public IEnumerable<Id> Ids { get; set; } | ||
|
||
public QueryContainer Organic { get; set; } | ||
|
||
protected override bool Conditionless => IsConditionless(this); | ||
|
||
internal override void InternalWrapInContainer(IQueryContainer c) => c.Pinned = this; | ||
|
||
internal static bool IsConditionless(IPinnedQuery q) => !q.Ids.HasAny() && q.Organic.IsConditionless(); | ||
} | ||
|
||
public class PinnedQueryDescriptor<T> | ||
: QueryDescriptorBase<PinnedQueryDescriptor<T>, IPinnedQuery> | ||
, IPinnedQuery | ||
where T : class | ||
{ | ||
protected override bool Conditionless => PinnedQuery.IsConditionless(this); | ||
IEnumerable<Id> IPinnedQuery.Ids { get; set; } | ||
QueryContainer IPinnedQuery.Organic { get; set; } | ||
|
||
public PinnedQueryDescriptor<T> Ids(params Id[] ids) => Assign(ids, (a, v) => a.Ids = v); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<Id> ids) => Ids(ids?.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<string> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<long> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Ids(IEnumerable<Guid> ids) => Ids(ids.ToArray()); | ||
|
||
public PinnedQueryDescriptor<T> Organic(Func<QueryContainerDescriptor<T>, QueryContainer> selector) => | ||
Assign(selector, (a, v) => a.Organic = v?.Invoke(new QueryContainerDescriptor<T>())); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
src/Tests/Tests/QueryDsl/Specialized/Pinned/PinnedQueryUsageTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
using System; | ||
using Nest; | ||
using Tests.Core.ManagedElasticsearch.Clusters; | ||
using Tests.Domain; | ||
using Tests.Framework.EndpointTests.TestState; | ||
|
||
#pragma warning disable 618 //Testing an obsolete method | ||
|
||
namespace Tests.QueryDsl.Specialized.Pinned | ||
{ | ||
/** | ||
* Promotes selected documents to rank higher than those matching a given query. This feature is typically used to | ||
* guide searchers to curated documents that are promoted over and above any "organic" matches for a search. The promoted or "pinned" | ||
* documents are identified using the document IDs stored in the _id field. | ||
* See the Elasticsearch documentation on {ref_current}/query-dsl-pinned-query.html[pinned query] for more details. | ||
*/ | ||
public class PinnedQueryUsageTests : QueryDslUsageTestsBase | ||
{ | ||
public PinnedQueryUsageTests(ReadOnlyCluster i, EndpointUsage usage) : base(i, usage) { } | ||
|
||
protected override ConditionlessWhen ConditionlessWhen => new ConditionlessWhen<IPinnedQuery>(a => a.Pinned) | ||
{ | ||
q => | ||
{ | ||
q.Ids = null; | ||
q.Organic = null; | ||
}, | ||
q => | ||
{ | ||
q.Ids = Array.Empty<Id>(); | ||
q.Organic = ConditionlessQuery; | ||
}, | ||
}; | ||
|
||
protected override NotConditionlessWhen NotConditionlessWhen => new NotConditionlessWhen<IPinnedQuery>(a => a.Pinned) | ||
{ | ||
q => q.Organic = VerbatimQuery, | ||
}; | ||
|
||
protected override QueryContainer QueryInitializer => new PinnedQuery() | ||
{ | ||
Name = "named_query", | ||
Boost = 1.1, | ||
Organic = new MatchAllQuery { Name = "organic_query" }, | ||
Ids = new Id[] { 1,11,22 }, | ||
}; | ||
|
||
protected override object QueryJson => new | ||
{ | ||
pinned = new | ||
{ | ||
_name = "named_query", | ||
boost = 1.1, | ||
organic = new | ||
{ | ||
match_all = new { _name = "organic_query" } | ||
}, | ||
ids = new [] { 1, 11, 22}, | ||
} | ||
}; | ||
|
||
protected override QueryContainer QueryFluent(QueryContainerDescriptor<Project> q) => q | ||
.Pinned(c => c | ||
.Name("named_query") | ||
.Boost(1.1) | ||
.Organic(qq => qq.MatchAll(m => m.Name("organic_query"))) | ||
.Ids(1, 11, 22) | ||
); | ||
} | ||
} |