Search - added HLRC support for PinnedQueryBuilder (#45779) (#45853)

Added HLRC support for PinnedQueryBuilder

Related  #44074
This commit is contained in:
markharwood 2019-08-23 09:22:17 +01:00 committed by GitHub
parent 85d55e30d0
commit 217e41ab6c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 128 additions and 1 deletions

View File

@ -43,6 +43,7 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchAllQueryBuilder;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.ScriptQueryBuilder;
@ -81,6 +82,7 @@ import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.search.suggest.Suggest;
import org.elasticsearch.search.suggest.SuggestBuilder;
import org.elasticsearch.search.suggest.phrase.PhraseSuggestionBuilder;
import org.elasticsearch.xpack.core.index.query.PinnedQueryBuilder;
import org.hamcrest.Matchers;
import org.junit.Before;
@ -92,7 +94,10 @@ import java.util.List;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertFirstHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSecondHit;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertToXContentEquivalent;
import static org.elasticsearch.test.hamcrest.ElasticsearchAssertions.hasId;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.either;
@ -1373,7 +1378,19 @@ public class SearchIT extends ESRestHighLevelClientTestCase {
assertCountHeader(countResponse);
assertEquals(3, countResponse.getCount());
}
public void testSearchWithBasicLicensedQuery() throws IOException {
SearchRequest searchRequest = new SearchRequest("index");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
PinnedQueryBuilder pinnedQuery = new PinnedQueryBuilder(new MatchAllQueryBuilder(), "2", "1");
searchSourceBuilder.query(pinnedQuery);
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = execute(searchRequest, highLevelClient()::search, highLevelClient()::searchAsync);
assertSearchHeader(searchResponse);
assertFirstHit(searchResponse, hasId("2"));
assertSecondHit(searchResponse, hasId("1"));
}
private static void assertCountHeader(CountResponse countResponse) {
assertEquals(0, countResponse.getSkippedShards());
assertEquals(0, countResponse.getFailedShards());

View File

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

View File

@ -0,0 +1,109 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
package org.elasticsearch.xpack.core.index.query;
import org.apache.lucene.search.Query;
import org.elasticsearch.common.ParseField;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.index.query.AbstractQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryShardContext;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
/**
* A query that will promote selected documents (identified by ID) above matches produced by an "organic" query. In practice, some upstream
* 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
* the other search results.
*/
public class PinnedQueryBuilder extends AbstractQueryBuilder<PinnedQueryBuilder> {
public static final String NAME = "pinned";
protected final QueryBuilder organicQuery;
protected final List<String> ids;
protected static final ParseField IDS_FIELD = new ParseField("ids");
protected static final ParseField ORGANIC_QUERY_FIELD = new ParseField("organic");
@Override
public String getWriteableName() {
return NAME;
}
/**
* Creates a new PinnedQueryBuilder
*/
public PinnedQueryBuilder(QueryBuilder organicQuery, String... ids) {
if (organicQuery == null) {
throw new IllegalArgumentException("[" + NAME + "] organicQuery cannot be null");
}
this.organicQuery = organicQuery;
if (ids == null) {
throw new IllegalArgumentException("[" + NAME + "] ids cannot be null");
}
this.ids = new ArrayList<>();
Collections.addAll(this.ids, ids);
}
@Override
protected void doWriteTo(StreamOutput out) throws IOException {
out.writeStringCollection(this.ids);
out.writeNamedWriteable(organicQuery);
}
/**
* @return the organic query set in the constructor
*/
public QueryBuilder organicQuery() {
return this.organicQuery;
}
/**
* Returns the pinned ids for the query.
*/
public List<String> ids() {
return Collections.unmodifiableList(this.ids);
}
@Override
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
builder.startObject(NAME);
if (organicQuery != null) {
builder.field(ORGANIC_QUERY_FIELD.getPreferredName());
organicQuery.toXContent(builder, params);
}
builder.startArray(IDS_FIELD.getPreferredName());
for (String value : ids) {
builder.value(value);
}
builder.endArray();
printBoostAndQueryName(builder);
builder.endObject();
}
@Override
protected Query doToQuery(QueryShardContext context) throws IOException {
throw new UnsupportedOperationException("Client side-only class for use in HLRC");
}
@Override
protected int doHashCode() {
return Objects.hash(ids, organicQuery);
}
@Override
protected boolean doEquals(PinnedQueryBuilder other) {
return Objects.equals(ids, other.ids) && Objects.equals(organicQuery, other.organicQuery) && boost == other.boost;
}
}