Skip to content
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

LPS-88344 Search Request with Rescore #66103

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -369,6 +369,7 @@ protected void prepare(
baseSearchRequest.setExplain(searchRequest.isExplain());
baseSearchRequest.setIncludeResponseString(
searchRequest.isIncludeResponseString());
baseSearchRequest.setRescoreQuery(searchRequest.getRescoreQuery());
}

@Reference
Expand Down
Expand Up @@ -26,6 +26,7 @@
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.rescore.QueryRescorerBuilder;

import org.osgi.service.component.annotations.Component;
import org.osgi.service.component.annotations.Reference;
Expand Down Expand Up @@ -75,6 +76,8 @@ public void assemble(
searchRequestBuilder.setTrackTotalHits(
baseSearchRequest.isTrackTotalHits());

setRescorer(searchRequestBuilder, baseSearchRequest);

facetTranslator.translate(
searchRequestBuilder, baseSearchRequest.getQuery(),
baseSearchRequest.getFacets(),
Expand Down Expand Up @@ -109,6 +112,20 @@ protected QueryBuilder getQueryBuilder(
return boolQueryBuilder;
}

protected void setRescorer(
SearchRequestBuilder searchRequestBuilder,
BaseSearchRequest baseSearchRequest) {

Query query = baseSearchRequest.getRescoreQuery();

if (query == null) {
return;
}

searchRequestBuilder.setRescorer(
new QueryRescorerBuilder(queryTranslator.translate(query, null)));
}

@Reference
protected FacetTranslator facetTranslator;

Expand Down
Expand Up @@ -16,6 +16,8 @@

import aQute.bnd.annotation.ProviderType;

import com.liferay.portal.kernel.search.Query;

/**
* Holds parameters to be used when performing a search.
*
Expand All @@ -26,6 +28,15 @@
@ProviderType
public interface SearchRequest {

/**
* Provides a secondary query to reorder the top documents returned.
*
* @return the rescore query
*
* @review
*/
public Query getRescoreQuery();

/**
* Enables explanation for each hit on how its score was computed.
*
Expand Down
Expand Up @@ -16,6 +16,8 @@

import aQute.bnd.annotation.ProviderType;

import com.liferay.portal.kernel.search.Query;

/**
* Builds a search request to be used when performing a search.
*
Expand Down Expand Up @@ -67,4 +69,14 @@ public SearchRequestBuilder addSelectedFieldNames(
public SearchRequestBuilder includeResponseString(
boolean includeResponseString);

/**
* Provides a secondary query to reorder the top documents returned.
*
* @param rescoreQuery the rescore query
* @return the same builder
*
* @review
*/
public SearchRequestBuilder rescoreQuery(Query rescoreQuery);

}
Expand Up @@ -49,6 +49,10 @@ public Query getQuery() {
return _query;
}

public Query getRescoreQuery() {
return _rescoreQuery;
}

public long getTimeoutInMilliseconds() {
return _timeoutInMilliseconds;
}
Expand Down Expand Up @@ -113,6 +117,10 @@ public void setRequestCache(boolean requestCache) {
_requestCache = requestCache;
}

public void setRescoreQuery(Query rescoreQuery) {
_rescoreQuery = rescoreQuery;
}

public void setTimeoutInMilliseconds(long timeoutInMilliseconds) {
_timeoutInMilliseconds = timeoutInMilliseconds;
}
Expand All @@ -130,6 +138,7 @@ public void setTrackTotalHits(boolean trackTotalHits) {
private Filter _postFilter;
private Query _query;
private boolean _requestCache;
private Query _rescoreQuery;
private long _timeoutInMilliseconds;
private boolean _trackTotalHits = true;

Expand Down
Expand Up @@ -14,6 +14,7 @@

package com.liferay.portal.search.internal.legacy.searcher;

import com.liferay.portal.kernel.search.Query;
import com.liferay.portal.kernel.search.QueryConfig;
import com.liferay.portal.kernel.search.SearchContext;
import com.liferay.portal.kernel.util.GetterUtil;
Expand Down Expand Up @@ -62,8 +63,20 @@ public SearchRequestBuilder includeResponseString(
return this;
}

@Override
public SearchRequestBuilder rescoreQuery(Query rescoreQuery) {
_searchContext.setAttribute(_RESCORE_QUERY, rescoreQuery);

return this;
}

public class SearchRequestImpl implements SearchRequest {

@Override
public Query getRescoreQuery() {
return (Query)_searchContext.getAttribute(_RESCORE_QUERY);
}

public SearchContext getSearchContext() {
return _searchContext;
}
Expand All @@ -86,6 +99,8 @@ public boolean isIncludeResponseString() {
private static final String _INCLUDE_RESPONSE_STRING =
"include.response.string";

private static final String _RESCORE_QUERY = "rescore.query";

private final SearchContext _searchContext;

}