Skip to content

Commit d1e00e3

Browse files
authored
Search - added HLRC support for PinnedQueryBuilder (#45779)
* Added HLRC support for PinnedQueryBuilder Related #44074
1 parent 36d859f commit d1e00e3

File tree

3 files changed

+128
-1
lines changed

3 files changed

+128
-1
lines changed

client/rest-high-level/src/test/java/org/elasticsearch/client/SearchIT.java

+18-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.elasticsearch.common.xcontent.XContentBuilder;
4444
import org.elasticsearch.common.xcontent.XContentFactory;
4545
import org.elasticsearch.common.xcontent.XContentType;
46+
import org.elasticsearch.index.query.MatchAllQueryBuilder;
4647
import org.elasticsearch.index.query.MatchQueryBuilder;
4748
import org.elasticsearch.index.query.QueryBuilders;
4849
import org.elasticsearch.index.query.ScriptQueryBuilder;
@@ -81,6 +82,7 @@
8182
import org.elasticsearch.search.suggest.Suggest;
8283
import org.elasticsearch.search.suggest.SuggestBuilder;
8384
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder;
85+
import org.elasticsearch.xpack.core.index.query.PinnedQueryBuilder;
8486
import org.hamcrest.Matchers;
8587
import org.junit.Before;
8688

@@ -92,7 +94,10 @@
9294
import java.util.Map;
9395

9496
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
97+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
98+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit;
9599
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
100+
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
96101
import static org.hamcrest.Matchers.both;
97102
import static org.hamcrest.Matchers.containsString;
98103
import static org.hamcrest.Matchers.either;
@@ -1373,7 +1378,19 @@ public void testCountAllIndicesMatchQuery() throws IOException {
13731378
assertCountHeader(countResponse);
13741379
assertEquals(3, countResponse.getCount());
13751380
}
1376-
1381+
1382+
public void testSearchWithBasicLicensedQuery() throws IOException {
1383+
SearchRequest searchRequest = new SearchRequest("index");
1384+
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
1385+
PinnedQueryBuilder pinnedQuery = new PinnedQueryBuilder(new MatchAllQueryBuilder(), "2", "1");
1386+
searchSourceBuilder.query(pinnedQuery);
1387+
searchRequest.source(searchSourceBuilder);
1388+
SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
1389+
assertSearchHeader(searchResponse);
1390+
assertFirstHit(searchResponse, hasId("2"));
1391+
assertSecondHit(searchResponse, hasId("1"));
1392+
}
1393+
13771394
private static void assertCountHeader(CountResponse countResponse) {
13781395
assertEquals(0, countResponse.getSkippedShards());
13791396
assertEquals(0, countResponse.getFailedShards());

docs/java-rest/high-level/query-builders.asciidoc

+1
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ This page lists all the available search queries with their corresponding `Query
8383
| {ref}/query-dsl-percolate-query.html[Percolate] | {percolate-ref}/PercolateQueryBuilder.html[PercolateQueryBuilder] |
8484
| {ref}/query-dsl-wrapper-query.html[Wrapper] | {query-ref}/WrapperQueryBuilder.html[WrapperQueryBuilder] | {query-ref}/QueryBuilders.html#wrapperQuery-java.lang.String-[QueryBuilders.wrapperQuery()]
8585
| {ref}/query-dsl-rank-feature-query.html[Rank Feature] | {mapper-extras-ref}/RankFeatureQuery.html[RankFeatureQueryBuilder] |
86+
| {ref}/query-dsl-pinned-query.html[Pinned Query] | The PinnedQueryBuilder is packaged as part of the xpack-core module |
8687
|======
8788

8889
==== Span queries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
3+
* or more contributor license agreements. Licensed under the Elastic License;
4+
* you may not use this file except in compliance with the Elastic License.
5+
*/
6+
package org.elasticsearch.xpack.core.index.query;
7+
8+
import org.apache.lucene.search.Query;
9+
import org.elasticsearch.common.ParseField;
10+
import org.elasticsearch.common.io.stream.StreamOutput;
11+
import org.elasticsearch.common.xcontent.XContentBuilder;
12+
import org.elasticsearch.index.query.AbstractQueryBuilder;
13+
import org.elasticsearch.index.query.QueryBuilder;
14+
import org.elasticsearch.index.query.QueryShardContext;
15+
16+
import java.io.IOException;
17+
import java.util.ArrayList;
18+
import java.util.Collections;
19+
import java.util.List;
20+
import java.util.Objects;
21+
22+
/**
23+
* A query that will promote selected documents (identified by ID) above matches produced by an "organic" query. In practice, some upstream
24+
* system will identify the promotions associated with a user's query string and use this object to ensure these are "pinned" to the top of
25+
* the other search results.
26+
*/
27+
public class PinnedQueryBuilder extends AbstractQueryBuilder<PinnedQueryBuilder> {
28+
public static final String NAME = "pinned";
29+
protected final QueryBuilder organicQuery;
30+
protected final List<String> ids;
31+
protected static final ParseField IDS_FIELD = new ParseField("ids");
32+
protected static final ParseField ORGANIC_QUERY_FIELD = new ParseField("organic");
33+
34+
@Override
35+
public String getWriteableName() {
36+
return NAME;
37+
}
38+
39+
/**
40+
* Creates a new PinnedQueryBuilder
41+
*/
42+
public PinnedQueryBuilder(QueryBuilder organicQuery, String... ids) {
43+
if (organicQuery == null) {
44+
throw new IllegalArgumentException("[" + NAME + "] organicQuery cannot be null");
45+
}
46+
this.organicQuery = organicQuery;
47+
if (ids == null) {
48+
throw new IllegalArgumentException("[" + NAME + "] ids cannot be null");
49+
}
50+
this.ids = new ArrayList<>();
51+
Collections.addAll(this.ids, ids);
52+
53+
}
54+
55+
56+
@Override
57+
protected void doWriteTo(StreamOutput out) throws IOException {
58+
out.writeStringCollection(this.ids);
59+
out.writeNamedWriteable(organicQuery);
60+
}
61+
62+
/**
63+
* @return the organic query set in the constructor
64+
*/
65+
public QueryBuilder organicQuery() {
66+
return this.organicQuery;
67+
}
68+
69+
/**
70+
* Returns the pinned ids for the query.
71+
*/
72+
public List<String> ids() {
73+
return Collections.unmodifiableList(this.ids);
74+
}
75+
76+
77+
@Override
78+
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
79+
builder.startObject(NAME);
80+
if (organicQuery != null) {
81+
builder.field(ORGANIC_QUERY_FIELD.getPreferredName());
82+
organicQuery.toXContent(builder, params);
83+
}
84+
builder.startArray(IDS_FIELD.getPreferredName());
85+
for (String value : ids) {
86+
builder.value(value);
87+
}
88+
builder.endArray();
89+
printBoostAndQueryName(builder);
90+
builder.endObject();
91+
}
92+
93+
@Override
94+
protected Query doToQuery(QueryShardContext context) throws IOException {
95+
throw new UnsupportedOperationException("Client side-only class for use in HLRC");
96+
}
97+
98+
99+
@Override
100+
protected int doHashCode() {
101+
return Objects.hash(ids, organicQuery);
102+
}
103+
104+
@Override
105+
protected boolean doEquals(PinnedQueryBuilder other) {
106+
return Objects.equals(ids, other.ids) && Objects.equals(organicQuery, other.organicQuery) && boost == other.boost;
107+
}
108+
109+
}

0 commit comments

Comments
 (0)