More Like This API: Allow to provide `search_size` and `search_from`, closes #1179.
This commit is contained in:
parent
c0266a10d4
commit
297a496998
|
@ -80,6 +80,8 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
private float boostTerms = -1;
|
||||
|
||||
private SearchType searchType = SearchType.DEFAULT;
|
||||
private int searchSize = 0;
|
||||
private int searchFrom = 0;
|
||||
private String searchQueryHint;
|
||||
private String[] searchIndices;
|
||||
private String[] searchTypes;
|
||||
|
@ -97,7 +99,7 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
|
||||
/**
|
||||
* Constructs a new more like this request for a document that will be fetch from the provided index.
|
||||
* Use {@link #type(String)} and {@link #id(String)} to specificy the document to load.
|
||||
* Use {@link #type(String)} and {@link #id(String)} to specify the document to load.
|
||||
*/
|
||||
public MoreLikeThisRequest(String index) {
|
||||
this.index = index;
|
||||
|
@ -122,7 +124,7 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
}
|
||||
|
||||
/**
|
||||
* The type of document to load from which the "like" query will rutn with.
|
||||
* The type of document to load from which the "like" query will execute with.
|
||||
*/
|
||||
@Required public MoreLikeThisRequest type(String type) {
|
||||
this.type = type;
|
||||
|
@ -130,14 +132,14 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
}
|
||||
|
||||
/**
|
||||
* The id of document to load from which the "like" query will rutn with.
|
||||
* The id of document to load from which the "like" query will execute with.
|
||||
*/
|
||||
public String id() {
|
||||
return id;
|
||||
}
|
||||
|
||||
/**
|
||||
* The id of document to load from which the "like" query will rutn with.
|
||||
* The id of document to load from which the "like" query will execute with.
|
||||
*/
|
||||
@Required public MoreLikeThisRequest id(String id) {
|
||||
this.id = id;
|
||||
|
@ -494,6 +496,30 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
return this.searchScroll;
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of documents to return, defaults to 10.
|
||||
*/
|
||||
public MoreLikeThisRequest searchSize(int size) {
|
||||
this.searchSize = size;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int searchSize() {
|
||||
return this.searchSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* From which search result set to return.
|
||||
*/
|
||||
public MoreLikeThisRequest searchFrom(int from) {
|
||||
this.searchFrom = from;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int searchFrom() {
|
||||
return this.searchFrom;
|
||||
}
|
||||
|
||||
@Override public ActionRequestValidationException validate() {
|
||||
ActionRequestValidationException validationException = null;
|
||||
if (index == null) {
|
||||
|
@ -592,6 +618,9 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
searchSource = new byte[searchSourceLength];
|
||||
in.readFully(searchSource);
|
||||
}
|
||||
|
||||
searchSize = in.readVInt();
|
||||
searchFrom = in.readVInt();
|
||||
}
|
||||
|
||||
@Override public void writeTo(StreamOutput out) throws IOException {
|
||||
|
@ -659,5 +688,8 @@ public class MoreLikeThisRequest implements ActionRequest {
|
|||
out.writeVInt(searchSourceLength);
|
||||
out.writeBytes(searchSource, searchSourceOffset, searchSourceLength);
|
||||
}
|
||||
|
||||
out.writeVInt(searchSize);
|
||||
out.writeVInt(searchFrom);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -170,15 +170,19 @@ public class TransportMoreLikeThisAction extends BaseAction<MoreLikeThisRequest,
|
|||
if (searchTypes == null) {
|
||||
searchTypes = new String[]{request.type()};
|
||||
}
|
||||
|
||||
int size = request.searchSize() != 0 ? request.searchSize() : 10;
|
||||
int from = request.searchFrom() != 0 ? request.searchFrom() : 0;
|
||||
SearchRequest searchRequest = searchRequest(searchIndices)
|
||||
.types(searchTypes)
|
||||
.searchType(request.searchType())
|
||||
.scroll(request.searchScroll())
|
||||
.extraSource(searchSource()
|
||||
.query(boolBuilder)
|
||||
.from(from)
|
||||
.size(size)
|
||||
)
|
||||
.listenerThreaded(request.listenerThreaded());
|
||||
|
||||
if (request.searchSource() != null) {
|
||||
searchRequest.source(request.searchSource(), request.searchSourceOffset(), request.searchSourceLength(), request.searchSourceUnsafe());
|
||||
}
|
||||
|
|
|
@ -48,6 +48,7 @@ import org.elasticsearch.client.action.deletebyquery.DeleteByQueryRequestBuilder
|
|||
import org.elasticsearch.client.action.get.GetRequestBuilder;
|
||||
import org.elasticsearch.client.action.get.MultiGetRequestBuilder;
|
||||
import org.elasticsearch.client.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.client.action.mlt.MoreLikeThisRequestBuilder;
|
||||
import org.elasticsearch.client.action.percolate.PercolateRequestBuilder;
|
||||
import org.elasticsearch.client.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.client.action.search.SearchScrollRequestBuilder;
|
||||
|
@ -335,6 +336,15 @@ public interface Client {
|
|||
*/
|
||||
void moreLikeThis(MoreLikeThisRequest request, ActionListener<SearchResponse> listener);
|
||||
|
||||
/**
|
||||
* A more like this action to search for documents that are "like" a specific document.
|
||||
*
|
||||
* @param index The index to load the document from
|
||||
* @param type The type of the document
|
||||
* @param id The id of the document
|
||||
*/
|
||||
MoreLikeThisRequestBuilder prepareMoreLikeThis(String index, String type, String id);
|
||||
|
||||
/**
|
||||
* Percolates a request returning the matches documents.
|
||||
*/
|
||||
|
|
|
@ -0,0 +1,239 @@
|
|||
/*
|
||||
* Licensed to Elastic Search and Shay Banon under one
|
||||
* or more contributor license agreements. See the NOTICE file
|
||||
* distributed with this work for additional information
|
||||
* regarding copyright ownership. Elastic Search licenses this
|
||||
* file to you under the Apache License, Version 2.0 (the
|
||||
* "License"); you may not use this file except in compliance
|
||||
* with the License. You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing,
|
||||
* software distributed under the License is distributed on an
|
||||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
|
||||
* KIND, either express or implied. See the License for the
|
||||
* specific language governing permissions and limitations
|
||||
* under the License.
|
||||
*/
|
||||
|
||||
package org.elasticsearch.client.action.mlt;
|
||||
|
||||
import org.elasticsearch.ElasticSearchIllegalArgumentException;
|
||||
import org.elasticsearch.action.ActionListener;
|
||||
import org.elasticsearch.action.mlt.MoreLikeThisRequest;
|
||||
import org.elasticsearch.action.search.SearchResponse;
|
||||
import org.elasticsearch.action.search.SearchType;
|
||||
import org.elasticsearch.client.Client;
|
||||
import org.elasticsearch.client.action.support.BaseRequestBuilder;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
import org.elasticsearch.search.builder.SearchSourceBuilder;
|
||||
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
*/
|
||||
public class MoreLikeThisRequestBuilder extends BaseRequestBuilder<MoreLikeThisRequest, SearchResponse> {
|
||||
|
||||
public MoreLikeThisRequestBuilder(Client client, String index, String type, String id) {
|
||||
super(client, new MoreLikeThisRequest(index).type(type).id(id));
|
||||
}
|
||||
|
||||
/**
|
||||
* The fields of the document to use in order to find documents "like" this one. Defaults to run
|
||||
* against all the document fields.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setField(String... fields) {
|
||||
request.fields(fields);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The percent of the terms to match for each field. Defaults to <tt>0.3f</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setPercentTermsToMatch(float percentTermsToMatch) {
|
||||
request.percentTermsToMatch(percentTermsToMatch);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The frequency below which terms will be ignored in the source doc. Defaults to <tt>2</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setMinTermFreq(int minTermFreq) {
|
||||
request.minTermFreq(minTermFreq);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum number of query terms that will be included in any generated query. Defaults to <tt>25</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder maxQueryTerms(int maxQueryTerms) {
|
||||
request.maxQueryTerms(maxQueryTerms);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Any word in this set is considered "uninteresting" and ignored.
|
||||
*
|
||||
* <p>Even if your Analyzer allows stopwords, you might want to tell the MoreLikeThis code to ignore them, as
|
||||
* for the purposes of document similarity it seems reasonable to assume that "a stop word is never interesting".
|
||||
*
|
||||
* <p>Defaults to no stop words.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setStopWords(String... stopWords) {
|
||||
request.stopWords(stopWords);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The frequency at which words will be ignored which do not occur in at least this
|
||||
* many docs. Defaults to <tt>5</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setMinDocFreq(int minDocFreq) {
|
||||
request.minDocFreq(minDocFreq);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum frequency in which words may still appear. Words that appear
|
||||
* in more than this many docs will be ignored. Defaults to unbounded.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setMaxDocFreq(int maxDocFreq) {
|
||||
request.maxDocFreq(maxDocFreq);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The minimum word length below which words will be ignored. Defaults to <tt>0</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setMinWordLen(int minWordLen) {
|
||||
request.minWordLen(minWordLen);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The maximum word length above which words will be ignored. Defaults to unbounded.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setMaxWordLen(int maxWordLen) {
|
||||
request().maxWordLen(maxWordLen);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The boost factor to use when boosting terms. Defaults to <tt>1</tt>.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setBoostTerms(float boostTerms) {
|
||||
request.boostTerms(boostTerms);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search source request allowing to control the search request for the
|
||||
* more like this documents.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSource(SearchSourceBuilder sourceBuilder) {
|
||||
request.searchSource(sourceBuilder);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search source request allowing to control the search request for the
|
||||
* more like this documents.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSource(String searchSource) {
|
||||
request.searchSource(searchSource);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search source request allowing to control the search request for the
|
||||
* more like this documents.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSource(Map searchSource) {
|
||||
request.searchSource(searchSource);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search source request allowing to control the search request for the
|
||||
* more like this documents.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSource(XContentBuilder builder) {
|
||||
request.searchSource(builder);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search source request allowing to control the search request for the
|
||||
* more like this documents.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSource(byte[] searchSource) {
|
||||
request.searchSource(searchSource);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The search type of the mlt search query.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchType(SearchType searchType) {
|
||||
request.searchType(searchType);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The search type of the mlt search query.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchType(String searchType) throws ElasticSearchIllegalArgumentException {
|
||||
request.searchType(searchType);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The indices the resulting mlt query will run against. If not set, will run
|
||||
* against the index the document was fetched from.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchIndices(String... searchIndices) {
|
||||
request.searchIndices(searchIndices);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The types the resulting mlt query will run against. If not set, will run
|
||||
* against the type of the document fetched.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchTypes(String... searchTypes) {
|
||||
request.searchTypes(searchTypes);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* An optional search scroll request to be able to continue and scroll the search
|
||||
* operation.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchScroll(Scroll searchScroll) {
|
||||
request.searchScroll(searchScroll);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* The number of documents to return, defaults to 10.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchSize(int size) {
|
||||
request.searchSize(size);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* From which search result set to return.
|
||||
*/
|
||||
public MoreLikeThisRequestBuilder setSearchFrom(int from) {
|
||||
request.searchFrom(from);
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
@Override protected void doExecute(ActionListener<SearchResponse> listener) {
|
||||
client.moreLikeThis(request, listener);
|
||||
}
|
||||
}
|
|
@ -26,6 +26,7 @@ import org.elasticsearch.client.action.deletebyquery.DeleteByQueryRequestBuilder
|
|||
import org.elasticsearch.client.action.get.GetRequestBuilder;
|
||||
import org.elasticsearch.client.action.get.MultiGetRequestBuilder;
|
||||
import org.elasticsearch.client.action.index.IndexRequestBuilder;
|
||||
import org.elasticsearch.client.action.mlt.MoreLikeThisRequestBuilder;
|
||||
import org.elasticsearch.client.action.percolate.PercolateRequestBuilder;
|
||||
import org.elasticsearch.client.action.search.SearchRequestBuilder;
|
||||
import org.elasticsearch.client.action.search.SearchScrollRequestBuilder;
|
||||
|
@ -89,6 +90,10 @@ public abstract class AbstractClient implements InternalClient {
|
|||
return new CountRequestBuilder(this).setIndices(indices);
|
||||
}
|
||||
|
||||
@Override public MoreLikeThisRequestBuilder prepareMoreLikeThis(String index, String type, String id) {
|
||||
return new MoreLikeThisRequestBuilder(this, index, type, id);
|
||||
}
|
||||
|
||||
@Override public PercolateRequestBuilder preparePercolate(String index, String type) {
|
||||
return new PercolateRequestBuilder(this, index, type);
|
||||
}
|
||||
|
|
|
@ -27,7 +27,12 @@ import org.elasticsearch.client.Client;
|
|||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.settings.Settings;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.rest.*;
|
||||
import org.elasticsearch.rest.BaseRestHandler;
|
||||
import org.elasticsearch.rest.RestChannel;
|
||||
import org.elasticsearch.rest.RestController;
|
||||
import org.elasticsearch.rest.RestRequest;
|
||||
import org.elasticsearch.rest.XContentRestResponse;
|
||||
import org.elasticsearch.rest.XContentThrowableRestResponse;
|
||||
import org.elasticsearch.search.Scroll;
|
||||
|
||||
import java.io.IOException;
|
||||
|
@ -67,6 +72,8 @@ public class RestMoreLikeThisAction extends BaseRestHandler {
|
|||
mltRequest.searchIndices(request.paramAsStringArray("search_indices", null));
|
||||
mltRequest.searchTypes(request.paramAsStringArray("search_types", null));
|
||||
mltRequest.searchQueryHint(request.param("search_query_hint"));
|
||||
mltRequest.searchSize(request.paramAsInt("search_size", mltRequest.searchSize()));
|
||||
mltRequest.searchFrom(request.paramAsInt("search_from", mltRequest.searchFrom()));
|
||||
String searchScroll = request.param("search_scroll");
|
||||
if (searchScroll != null) {
|
||||
mltRequest.searchScroll(new Scroll(parseTimeValue(searchScroll, null)));
|
||||
|
|
Loading…
Reference in New Issue