Skip to content

Added HLRC support for PinnedQueryBuilder #45779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
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;
Expand Down Expand Up @@ -81,6 +82,7 @@
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;

Expand All @@ -92,7 +94,10 @@
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;
Expand Down Expand Up @@ -1373,7 +1378,19 @@ public void testCountAllIndicesMatchQuery() throws IOException {
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());
Expand Down
1 change: 1 addition & 0 deletions docs/java-rest/high-level/query-builders.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,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
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}

}