Remove `custom_score` and `custom_boost_factor` queries
`custom_boost_factor` and `custom_score` were deprecated in `0.90.5` and their documentation was removed already in `1.0`. This commit removes all support for those queries since they are supercede by `function_score`.
This commit is contained in:
parent
766134f3c7
commit
006075f01e
|
@ -1,67 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* A query that simply applies the boost factor to another query (multiply it).
|
||||
*
|
||||
* @deprecated use {@link FunctionScoreQueryBuilder} instead.
|
||||
*/
|
||||
public class CustomBoostFactorQueryBuilder extends BaseQueryBuilder {
|
||||
|
||||
private final QueryBuilder queryBuilder;
|
||||
|
||||
private float boostFactor = -1;
|
||||
|
||||
/**
|
||||
* A query that simply applies the boost factor to another query (multiply
|
||||
* it).
|
||||
*
|
||||
* @param queryBuilder
|
||||
* The query to apply the boost factor to.
|
||||
*/
|
||||
public CustomBoostFactorQueryBuilder(QueryBuilder queryBuilder) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the boost factor for this query.
|
||||
*/
|
||||
public CustomBoostFactorQueryBuilder boostFactor(float boost) {
|
||||
this.boostFactor = boost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject(CustomBoostFactorQueryParser.NAME);
|
||||
builder.field("query");
|
||||
queryBuilder.toXContent(builder, params);
|
||||
if (boostFactor != -1) {
|
||||
builder.field("boost_factor", boostFactor);
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
|
@ -1,91 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.lucene.search.function.BoostScoreFunction;
|
||||
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryParser;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* @deprecated use {@link FunctionScoreQueryParser} instead.
|
||||
*/
|
||||
public class CustomBoostFactorQueryParser implements QueryParser {
|
||||
|
||||
public static final String NAME = "custom_boost_factor";
|
||||
|
||||
@Inject
|
||||
public CustomBoostFactorQueryParser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] names() {
|
||||
return new String[] { NAME, Strings.toCamelCase(NAME) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
|
||||
XContentParser parser = parseContext.parser();
|
||||
|
||||
Query query = null;
|
||||
boolean queryFound = false;
|
||||
float boost = 1.0f;
|
||||
float boostFactor = 1.0f;
|
||||
|
||||
String currentFieldName = null;
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if ("query".equals(currentFieldName)) {
|
||||
query = parseContext.parseInnerQuery();
|
||||
queryFound = true;
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_boost_factor] query does not support ["
|
||||
+ currentFieldName + "]");
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("boost_factor".equals(currentFieldName) || "boostFactor".equals(currentFieldName)) {
|
||||
boostFactor = parser.floatValue();
|
||||
} else if ("boost".equals(currentFieldName)) {
|
||||
boost = parser.floatValue();
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_boost_factor] query does not support ["
|
||||
+ currentFieldName + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!queryFound) {
|
||||
throw new QueryParsingException(parseContext.index(), "[constant_factor_query] requires 'query' element");
|
||||
}
|
||||
if (query == null) {
|
||||
return null;
|
||||
}
|
||||
FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new BoostScoreFunction(boostFactor));
|
||||
functionScoreQuery.setBoost(boost);
|
||||
return functionScoreQuery;
|
||||
}
|
||||
}
|
|
@ -1,163 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import com.carrotsearch.hppc.FloatArrayList;
|
||||
import com.google.common.collect.Maps;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A query that uses a filters with a script associated with them to compute the
|
||||
* score.
|
||||
*
|
||||
* @deprecated use {@link FunctionScoreQueryBuilder} instead.
|
||||
*/
|
||||
public class CustomFiltersScoreQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<CustomFiltersScoreQueryBuilder> {
|
||||
|
||||
private final QueryBuilder queryBuilder;
|
||||
|
||||
private String lang;
|
||||
|
||||
private float boost = -1;
|
||||
|
||||
private Float maxBoost;
|
||||
|
||||
private Map<String, Object> params = null;
|
||||
|
||||
private String scoreMode;
|
||||
|
||||
private ArrayList<FilterBuilder> filters = new ArrayList<FilterBuilder>();
|
||||
private ArrayList<String> scripts = new ArrayList<String>();
|
||||
private FloatArrayList boosts = new FloatArrayList();
|
||||
|
||||
public CustomFiltersScoreQueryBuilder(QueryBuilder queryBuilder) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
}
|
||||
|
||||
public CustomFiltersScoreQueryBuilder add(FilterBuilder filter, String script) {
|
||||
this.filters.add(filter);
|
||||
this.scripts.add(script);
|
||||
this.boosts.add(-1);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CustomFiltersScoreQueryBuilder add(FilterBuilder filter, float boost) {
|
||||
this.filters.add(filter);
|
||||
this.scripts.add(null);
|
||||
this.boosts.add(boost);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CustomFiltersScoreQueryBuilder scoreMode(String scoreMode) {
|
||||
this.scoreMode = scoreMode;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the language of the script.
|
||||
*/
|
||||
public CustomFiltersScoreQueryBuilder lang(String lang) {
|
||||
this.lang = lang;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional parameters that can be provided to the script.
|
||||
*/
|
||||
public CustomFiltersScoreQueryBuilder params(Map<String, Object> params) {
|
||||
if (this.params == null) {
|
||||
this.params = params;
|
||||
} else {
|
||||
this.params.putAll(params);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional parameters that can be provided to the script.
|
||||
*/
|
||||
public CustomFiltersScoreQueryBuilder param(String key, Object value) {
|
||||
if (params == null) {
|
||||
params = Maps.newHashMap();
|
||||
}
|
||||
params.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public CustomFiltersScoreQueryBuilder maxBoost(float maxBoost) {
|
||||
this.maxBoost = maxBoost;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the boost for this query. Documents matching this query will (in
|
||||
* addition to the normal weightings) have their score multiplied by the
|
||||
* boost provided.
|
||||
*/
|
||||
public CustomFiltersScoreQueryBuilder boost(float boost) {
|
||||
this.boost = boost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject(CustomFiltersScoreQueryParser.NAME);
|
||||
builder.field("query");
|
||||
queryBuilder.toXContent(builder, params);
|
||||
|
||||
builder.startArray("filters");
|
||||
for (int i = 0; i < filters.size(); i++) {
|
||||
builder.startObject();
|
||||
builder.field("filter");
|
||||
filters.get(i).toXContent(builder, params);
|
||||
String script = scripts.get(i);
|
||||
if (script != null) {
|
||||
builder.field("script", script);
|
||||
} else {
|
||||
builder.field("boost", boosts.get(i));
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
builder.endArray();
|
||||
|
||||
if (scoreMode != null) {
|
||||
builder.field("score_mode", scoreMode);
|
||||
}
|
||||
if (maxBoost != null) {
|
||||
builder.field("max_boost", maxBoost);
|
||||
}
|
||||
|
||||
if (lang != null) {
|
||||
builder.field("lang", lang);
|
||||
}
|
||||
if (this.params != null) {
|
||||
builder.field("params", this.params);
|
||||
}
|
||||
if (boost != -1) {
|
||||
builder.field("boost", boost);
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
|
@ -1,190 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import com.carrotsearch.hppc.FloatArrayList;
|
||||
import org.apache.lucene.search.Filter;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.lucene.search.function.BoostScoreFunction;
|
||||
import org.elasticsearch.common.lucene.search.function.FiltersFunctionScoreQuery;
|
||||
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
|
||||
import org.elasticsearch.common.lucene.search.function.ScriptScoreFunction;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryParser;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @deprecated use {@link FunctionScoreQueryParser} instead.
|
||||
*/
|
||||
public class CustomFiltersScoreQueryParser implements QueryParser {
|
||||
|
||||
public static final String NAME = "custom_filters_score";
|
||||
|
||||
@Inject
|
||||
public CustomFiltersScoreQueryParser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] names() {
|
||||
return new String[] { NAME, Strings.toCamelCase(NAME) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
|
||||
XContentParser parser = parseContext.parser();
|
||||
|
||||
Query query = null;
|
||||
boolean queryFound = false;
|
||||
float boost = 1.0f;
|
||||
String scriptLang = null;
|
||||
Map<String, Object> vars = null;
|
||||
|
||||
FiltersFunctionScoreQuery.ScoreMode scoreMode = FiltersFunctionScoreQuery.ScoreMode.First;
|
||||
ArrayList<Filter> filters = new ArrayList<Filter>();
|
||||
boolean filtersFound = false;
|
||||
ArrayList<String> scripts = new ArrayList<String>();
|
||||
FloatArrayList boosts = new FloatArrayList();
|
||||
float maxBoost = Float.MAX_VALUE;
|
||||
|
||||
String currentFieldName = null;
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if ("query".equals(currentFieldName)) {
|
||||
query = parseContext.parseInnerQuery();
|
||||
queryFound = true;
|
||||
} else if ("params".equals(currentFieldName)) {
|
||||
vars = parser.map();
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] query does not support ["
|
||||
+ currentFieldName + "]");
|
||||
}
|
||||
} else if (token == XContentParser.Token.START_ARRAY) {
|
||||
if ("filters".equals(currentFieldName)) {
|
||||
filtersFound = true;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_ARRAY) {
|
||||
String script = null;
|
||||
Filter filter = null;
|
||||
boolean filterFound = false;
|
||||
float fboost = Float.NaN;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if ("filter".equals(currentFieldName)) {
|
||||
filter = parseContext.parseInnerFilter();
|
||||
filterFound = true;
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("script".equals(currentFieldName)) {
|
||||
script = parser.text();
|
||||
} else if ("boost".equals(currentFieldName)) {
|
||||
fboost = parser.floatValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (script == null && fboost == -1) {
|
||||
throw new QueryParsingException(parseContext.index(),
|
||||
"[custom_filters_score] missing 'script' or 'boost' in filters array element");
|
||||
}
|
||||
if (!filterFound) {
|
||||
throw new QueryParsingException(parseContext.index(),
|
||||
"[custom_filters_score] missing 'filter' in filters array element");
|
||||
}
|
||||
if (filter != null) {
|
||||
filters.add(filter);
|
||||
scripts.add(script);
|
||||
boosts.add(fboost);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] query does not support ["
|
||||
+ currentFieldName + "]");
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("lang".equals(currentFieldName)) {
|
||||
scriptLang = parser.text();
|
||||
} else if ("boost".equals(currentFieldName)) {
|
||||
boost = parser.floatValue();
|
||||
} else if ("score_mode".equals(currentFieldName) || "scoreMode".equals(currentFieldName)) {
|
||||
String sScoreMode = parser.text();
|
||||
if ("avg".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Avg;
|
||||
} else if ("max".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Max;
|
||||
} else if ("min".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Min;
|
||||
} else if ("total".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Sum;
|
||||
} else if ("multiply".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.Multiply;
|
||||
} else if ("first".equals(sScoreMode)) {
|
||||
scoreMode = FiltersFunctionScoreQuery.ScoreMode.First;
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] illegal score_mode [" + sScoreMode
|
||||
+ "]");
|
||||
}
|
||||
} else if ("max_boost".equals(currentFieldName) || "maxBoost".equals(currentFieldName)) {
|
||||
maxBoost = parser.floatValue();
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] query does not support ["
|
||||
+ currentFieldName + "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!queryFound) {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] requires 'query' field");
|
||||
}
|
||||
if (query == null) {
|
||||
return null;
|
||||
}
|
||||
if (!filtersFound) {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_filters_score] requires 'filters' field");
|
||||
}
|
||||
// if all filter elements returned null, just use the query
|
||||
if (filters.isEmpty()) {
|
||||
return query;
|
||||
}
|
||||
|
||||
FiltersFunctionScoreQuery.FilterFunction[] filterFunctions = new FiltersFunctionScoreQuery.FilterFunction[filters.size()];
|
||||
for (int i = 0; i < filterFunctions.length; i++) {
|
||||
ScoreFunction scoreFunction;
|
||||
String script = scripts.get(i);
|
||||
if (script != null) {
|
||||
SearchScript searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars);
|
||||
scoreFunction = new ScriptScoreFunction(script, vars, searchScript);
|
||||
} else {
|
||||
scoreFunction = new BoostScoreFunction(boosts.get(i));
|
||||
}
|
||||
filterFunctions[i] = new FiltersFunctionScoreQuery.FilterFunction(filters.get(i), scoreFunction);
|
||||
}
|
||||
FiltersFunctionScoreQuery functionScoreQuery = new FiltersFunctionScoreQuery(query, scoreMode, filterFunctions, maxBoost);
|
||||
functionScoreQuery.setBoost(boost);
|
||||
return functionScoreQuery;
|
||||
}
|
||||
}
|
|
@ -1,146 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import org.elasticsearch.common.xcontent.XContentBuilder;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* A query that uses a script to compute or influence the score of documents
|
||||
* that match with the inner query or filter.
|
||||
*
|
||||
* @deprecated use {@link FunctionScoreQueryBuilder} instead.
|
||||
*/
|
||||
public class CustomScoreQueryBuilder extends BaseQueryBuilder implements BoostableQueryBuilder<CustomScoreQueryBuilder> {
|
||||
|
||||
private final QueryBuilder queryBuilder;
|
||||
|
||||
private final FilterBuilder filterBuilder;
|
||||
|
||||
private String script;
|
||||
|
||||
private String lang;
|
||||
|
||||
private float boost = -1;
|
||||
|
||||
private Map<String, Object> params = null;
|
||||
|
||||
/**
|
||||
* Constructs a query that defines how the scores are computed or influenced
|
||||
* for documents that match with the specified query by a custom defined
|
||||
* script.
|
||||
*
|
||||
* @param queryBuilder
|
||||
* The query that defines what documents are custom scored by
|
||||
* this query
|
||||
*/
|
||||
public CustomScoreQueryBuilder(QueryBuilder queryBuilder) {
|
||||
this.queryBuilder = queryBuilder;
|
||||
this.filterBuilder = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a query that defines how documents are scored that match with
|
||||
* the specified filter.
|
||||
*
|
||||
* @param filterBuilder
|
||||
* The filter that decides with documents are scored by this
|
||||
* query.
|
||||
*/
|
||||
public CustomScoreQueryBuilder(FilterBuilder filterBuilder) {
|
||||
this.filterBuilder = filterBuilder;
|
||||
this.queryBuilder = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the boost factor for this query.
|
||||
*/
|
||||
public CustomScoreQueryBuilder script(String script) {
|
||||
this.script = script;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the language of the script.
|
||||
*/
|
||||
public CustomScoreQueryBuilder lang(String lang) {
|
||||
this.lang = lang;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional parameters that can be provided to the script.
|
||||
*/
|
||||
public CustomScoreQueryBuilder params(Map<String, Object> params) {
|
||||
if (this.params == null) {
|
||||
this.params = params;
|
||||
} else {
|
||||
this.params.putAll(params);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Additional parameters that can be provided to the script.
|
||||
*/
|
||||
public CustomScoreQueryBuilder param(String key, Object value) {
|
||||
if (params == null) {
|
||||
params = Maps.newHashMap();
|
||||
}
|
||||
params.put(key, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the boost for this query. Documents matching this query will (in
|
||||
* addition to the normal weightings) have their score multiplied by the
|
||||
* boost provided.
|
||||
*/
|
||||
public CustomScoreQueryBuilder boost(float boost) {
|
||||
this.boost = boost;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void doXContent(XContentBuilder builder, Params params) throws IOException {
|
||||
builder.startObject(CustomScoreQueryParser.NAME);
|
||||
if (queryBuilder != null) {
|
||||
builder.field("query");
|
||||
queryBuilder.toXContent(builder, params);
|
||||
} else if (filterBuilder != null) {
|
||||
builder.field("filter");
|
||||
filterBuilder.toXContent(builder, params);
|
||||
}
|
||||
builder.field("script", script);
|
||||
if (lang != null) {
|
||||
builder.field("lang", lang);
|
||||
}
|
||||
if (this.params != null) {
|
||||
builder.field("params", this.params);
|
||||
}
|
||||
if (boost != -1) {
|
||||
builder.field("boost", boost);
|
||||
}
|
||||
builder.endObject();
|
||||
}
|
||||
}
|
|
@ -1,116 +0,0 @@
|
|||
/*
|
||||
* Licensed to Elasticsearch under one or more contributor
|
||||
* license agreements. See the NOTICE file distributed with
|
||||
* this work for additional information regarding copyright
|
||||
* ownership. Elasticsearch 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.index.query;
|
||||
|
||||
import org.apache.lucene.search.Filter;
|
||||
import org.apache.lucene.search.Query;
|
||||
import org.elasticsearch.common.Strings;
|
||||
import org.elasticsearch.common.inject.Inject;
|
||||
import org.elasticsearch.common.lucene.search.XConstantScoreQuery;
|
||||
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
|
||||
import org.elasticsearch.common.lucene.search.function.ScriptScoreFunction;
|
||||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.script.SearchScript;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @deprecated use {@link FunctionScoreQueryParser} instead.
|
||||
*/
|
||||
public class CustomScoreQueryParser implements QueryParser {
|
||||
|
||||
public static final String NAME = "custom_score";
|
||||
|
||||
@Inject
|
||||
public CustomScoreQueryParser() {
|
||||
}
|
||||
|
||||
@Override
|
||||
public String[] names() {
|
||||
return new String[] { NAME, Strings.toCamelCase(NAME) };
|
||||
}
|
||||
|
||||
@Override
|
||||
public Query parse(QueryParseContext parseContext) throws IOException, QueryParsingException {
|
||||
XContentParser parser = parseContext.parser();
|
||||
|
||||
Query query = null;
|
||||
Filter filter = null;
|
||||
boolean queryOrFilterFound = false;
|
||||
float boost = 1.0f;
|
||||
String script = null;
|
||||
String scriptLang = null;
|
||||
Map<String, Object> vars = null;
|
||||
|
||||
String currentFieldName = null;
|
||||
XContentParser.Token token;
|
||||
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
|
||||
if (token == XContentParser.Token.FIELD_NAME) {
|
||||
currentFieldName = parser.currentName();
|
||||
} else if (token == XContentParser.Token.START_OBJECT) {
|
||||
if ("query".equals(currentFieldName)) {
|
||||
query = parseContext.parseInnerQuery();
|
||||
queryOrFilterFound = true;
|
||||
} else if ("filter".equals(currentFieldName)) {
|
||||
filter = parseContext.parseInnerFilter();
|
||||
queryOrFilterFound = true;
|
||||
} else if ("params".equals(currentFieldName)) {
|
||||
vars = parser.map();
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_score] query does not support [" + currentFieldName
|
||||
+ "]");
|
||||
}
|
||||
} else if (token.isValue()) {
|
||||
if ("script".equals(currentFieldName)) {
|
||||
script = parser.text();
|
||||
} else if ("lang".equals(currentFieldName)) {
|
||||
scriptLang = parser.text();
|
||||
} else if ("boost".equals(currentFieldName)) {
|
||||
boost = parser.floatValue();
|
||||
} else {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_score] query does not support [" + currentFieldName
|
||||
+ "]");
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!queryOrFilterFound) {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_score] requires 'query' or 'filter' field");
|
||||
}
|
||||
if (script == null) {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_score] requires 'script' field");
|
||||
}
|
||||
if (query == null && filter == null) {
|
||||
return null;
|
||||
} else if (filter != null) {
|
||||
query = new XConstantScoreQuery(filter);
|
||||
}
|
||||
|
||||
SearchScript searchScript;
|
||||
try {
|
||||
searchScript = parseContext.scriptService().search(parseContext.lookup(), scriptLang, script, vars);
|
||||
} catch (Exception e) {
|
||||
throw new QueryParsingException(parseContext.index(), "[custom_score] the script could not be loaded", e);
|
||||
}
|
||||
FunctionScoreQuery functionScoreQuery = new FunctionScoreQuery(query, new ScriptScoreFunction(script, vars, searchScript));
|
||||
functionScoreQuery.setBoost(boost);
|
||||
return functionScoreQuery;
|
||||
}
|
||||
}
|
|
@ -419,44 +419,6 @@ public abstract class QueryBuilders {
|
|||
return new ConstantScoreQueryBuilder(queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* A query that simply applies the boost fact to the wrapped query (multiplies it).
|
||||
*
|
||||
* @param queryBuilder The query to apply the boost factor to.
|
||||
* @deprecated use {@link #functionScoreQuery(QueryBuilder)} instead
|
||||
*/
|
||||
public static CustomBoostFactorQueryBuilder customBoostFactorQuery(QueryBuilder queryBuilder) {
|
||||
return new CustomBoostFactorQueryBuilder(queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* A query that allows to define a custom scoring script.
|
||||
*
|
||||
* @param queryBuilder The query to custom score
|
||||
* @deprecated use {@link #functionScoreQuery(QueryBuilder)} instead
|
||||
*/
|
||||
public static CustomScoreQueryBuilder customScoreQuery(QueryBuilder queryBuilder) {
|
||||
return new CustomScoreQueryBuilder(queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* A query that allows to define a custom scoring script, that defines the score for each document that match
|
||||
* with the specified filter.
|
||||
*
|
||||
* @param filterBuilder The filter that defines which documents are scored by a script.
|
||||
* @deprecated use {@link #functionScoreQuery(QueryBuilder)} instead
|
||||
*/
|
||||
public static CustomScoreQueryBuilder customScoreQuery(FilterBuilder filterBuilder) {
|
||||
return new CustomScoreQueryBuilder(filterBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated use {@link #functionScoreQuery(QueryBuilder)} instead
|
||||
*/
|
||||
public static CustomFiltersScoreQueryBuilder customFiltersScoreQuery(QueryBuilder queryBuilder) {
|
||||
return new CustomFiltersScoreQueryBuilder(queryBuilder);
|
||||
}
|
||||
|
||||
/**
|
||||
* A query that allows to define a custom scoring function.
|
||||
*
|
||||
|
|
|
@ -87,9 +87,6 @@ public class IndicesQueriesModule extends AbstractModule {
|
|||
qpBinders.addBinding().to(WildcardQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(FilteredQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(ConstantScoreQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(CustomBoostFactorQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(CustomScoreQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(CustomFiltersScoreQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(SpanTermQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(SpanNotQueryParser.class).asEagerSingleton();
|
||||
qpBinders.addBinding().to(FieldMaskingSpanQueryParser.class).asEagerSingleton();
|
||||
|
|
|
@ -1366,16 +1366,6 @@ public class SimpleIndexQueryParserTests extends ElasticsearchTestCase {
|
|||
// assertThat(functionScoreQuery.getFunction(), instanceOf(CustomScoreQueryParser.ScriptScoreFunction.class));
|
||||
// }
|
||||
|
||||
@Test
|
||||
public void testCustomBoostFactorQueryBuilder() throws IOException {
|
||||
IndexQueryParserService queryParser = queryParser();
|
||||
Query parsedQuery = queryParser.parse(customBoostFactorQuery(termQuery("name.last", "banon")).boostFactor(1.3f)).query();
|
||||
assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
|
||||
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
|
||||
assertThat(((TermQuery) functionScoreQuery.getSubQuery()).getTerm(), equalTo(new Term("name.last", "banon")));
|
||||
assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCustomBoostFactorQueryBuilder_withFunctionScore() throws IOException {
|
||||
|
@ -1398,18 +1388,6 @@ public class SimpleIndexQueryParserTests extends ElasticsearchTestCase {
|
|||
assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001));
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void testCustomBoostFactorQuery() throws IOException {
|
||||
IndexQueryParserService queryParser = queryParser();
|
||||
String query = copyToStringFromClasspath("/org/elasticsearch/index/query/custom-boost-factor-query.json");
|
||||
Query parsedQuery = queryParser.parse(query).query();
|
||||
assertThat(parsedQuery, instanceOf(FunctionScoreQuery.class));
|
||||
FunctionScoreQuery functionScoreQuery = (FunctionScoreQuery) parsedQuery;
|
||||
assertThat(((TermQuery) functionScoreQuery.getSubQuery()).getTerm(), equalTo(new Term("name.last", "banon")));
|
||||
assertThat((double) ((BoostScoreFunction) functionScoreQuery.getFunction()).getBoost(), closeTo(1.3, 0.001));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testSpanTermQueryBuilder() throws IOException {
|
||||
IndexQueryParserService queryParser = queryParser();
|
||||
|
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"custom_boost_factor":{
|
||||
"query":{
|
||||
"term":{
|
||||
"name.last":"banon"
|
||||
}
|
||||
},
|
||||
"boost_factor":1.3
|
||||
}
|
||||
}
|
|
@ -1,10 +0,0 @@
|
|||
{
|
||||
"custom_score":{
|
||||
"query":{
|
||||
"term":{
|
||||
"name.last":"banon"
|
||||
}
|
||||
},
|
||||
"script":"score * doc['name.first']"
|
||||
}
|
||||
}
|
|
@ -109,10 +109,8 @@ public class TransportTwoNodesSearchTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
assertThat(searchResponse.getHits().totalHits(), equalTo(100l));
|
||||
assertThat(searchResponse.getHits().hits().length, equalTo(60));
|
||||
// System.out.println("max_score: " + searchResponse.hits().maxScore());
|
||||
for (int i = 0; i < 60; i++) {
|
||||
SearchHit hit = searchResponse.getHits().hits()[i];
|
||||
// System.out.println(hit.shard() + ": " + hit.score() + ":" + hit.explanation());
|
||||
assertThat(hit.explanation(), notNullValue());
|
||||
assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - i - 1)));
|
||||
}
|
||||
|
@ -141,7 +139,6 @@ public class TransportTwoNodesSearchTests extends ElasticsearchIntegrationTest {
|
|||
assertThat(searchResponse.getHits().hits().length, equalTo(60));
|
||||
for (int i = 0; i < 60; i++) {
|
||||
SearchHit hit = searchResponse.getHits().hits()[i];
|
||||
// System.out.println(hit.shard() + ": " + hit.explanation());
|
||||
assertThat(hit.explanation(), notNullValue());
|
||||
assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(i)));
|
||||
}
|
||||
|
@ -171,7 +168,6 @@ public class TransportTwoNodesSearchTests extends ElasticsearchIntegrationTest {
|
|||
assertThat(searchResponse.getHits().hits().length, equalTo(60));
|
||||
for (int i = 0; i < 60; i++) {
|
||||
SearchHit hit = searchResponse.getHits().hits()[i];
|
||||
// System.out.println(hit.shard() + ": " + hit.explanation());
|
||||
assertThat(hit.explanation(), notNullValue());
|
||||
assertThat("id[" + hit.id() + "]", hit.id(), equalTo(Integer.toString(100 - i - 1)));
|
||||
}
|
||||
|
@ -395,8 +391,8 @@ public class TransportTwoNodesSearchTests extends ElasticsearchIntegrationTest {
|
|||
logger.info("Start Testing failed multi search with a wrong query");
|
||||
|
||||
MultiSearchResponse response = client().prepareMultiSearch()
|
||||
// Add custom score query with missing script
|
||||
.add(client().prepareSearch("test").setQuery(QueryBuilders.customScoreQuery(QueryBuilders.termQuery("nid", 1))))
|
||||
// Add function score with a bogus score mode
|
||||
.add(client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(QueryBuilders.termQuery("nid", 1)).scoreMode("foobar")))
|
||||
.add(client().prepareSearch("test").setQuery(QueryBuilders.termQuery("nid", 2)))
|
||||
.add(client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()))
|
||||
.execute().actionGet();
|
||||
|
|
|
@ -36,6 +36,7 @@ import org.elasticsearch.common.settings.ImmutableSettings;
|
|||
import org.elasticsearch.common.unit.TimeValue;
|
||||
import org.elasticsearch.index.mapper.MergeMappingException;
|
||||
import org.elasticsearch.index.query.*;
|
||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
||||
import org.elasticsearch.index.search.child.ScoreType;
|
||||
import org.elasticsearch.rest.RestStatus;
|
||||
import org.elasticsearch.search.facet.terms.TermsFacet;
|
||||
|
@ -1003,97 +1004,6 @@ public class SimpleChildQuerySearchTests extends ElasticsearchIntegrationTest {
|
|||
assertThat(explainResponse.getExplanation().getDescription(), equalTo("not implemented yet..."));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testScoreForParentChildQueries() throws Exception {
|
||||
|
||||
client().admin()
|
||||
.indices()
|
||||
.prepareCreate("test")
|
||||
.addMapping(
|
||||
"child",
|
||||
jsonBuilder().startObject().startObject("child").startObject("_parent").field("type", "parent").endObject()
|
||||
.endObject().endObject())
|
||||
.addMapping(
|
||||
"child1",
|
||||
jsonBuilder().startObject().startObject("child1").startObject("_parent").field("type", "parent").endObject()
|
||||
.endObject().endObject())
|
||||
.setSettings(ImmutableSettings.settingsBuilder().put("index.number_of_shards", 2).put("index.number_of_replicas", 0))
|
||||
.execute().actionGet();
|
||||
client().admin().cluster().prepareHealth().setWaitForEvents(Priority.LANGUID).setWaitForGreenStatus().execute().actionGet();
|
||||
|
||||
indexRandom(false, createDocBuilders().toArray(new IndexRequestBuilder[0]));
|
||||
refresh();
|
||||
|
||||
SearchResponse response = client()
|
||||
.prepareSearch("test")
|
||||
.setQuery(
|
||||
QueryBuilders.hasChildQuery("child",
|
||||
QueryBuilders.customScoreQuery(matchQuery("c_field2", 0)).script("doc['c_field1'].value")).scoreType("sum"))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(response.getHits().totalHits(), equalTo(3l));
|
||||
assertThat(response.getHits().hits()[0].id(), equalTo("1"));
|
||||
assertThat(response.getHits().hits()[0].score(), equalTo(6f));
|
||||
assertThat(response.getHits().hits()[1].id(), equalTo("3"));
|
||||
assertThat(response.getHits().hits()[1].score(), equalTo(4f));
|
||||
assertThat(response.getHits().hits()[2].id(), equalTo("2"));
|
||||
assertThat(response.getHits().hits()[2].score(), equalTo(3f));
|
||||
|
||||
response = client()
|
||||
.prepareSearch("test")
|
||||
.setQuery(
|
||||
QueryBuilders.hasChildQuery("child",
|
||||
QueryBuilders.customScoreQuery(matchQuery("c_field2", 0)).script("doc['c_field1'].value")).scoreType("max"))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(response.getHits().totalHits(), equalTo(3l));
|
||||
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
||||
assertThat(response.getHits().hits()[0].score(), equalTo(4f));
|
||||
assertThat(response.getHits().hits()[1].id(), equalTo("2"));
|
||||
assertThat(response.getHits().hits()[1].score(), equalTo(3f));
|
||||
assertThat(response.getHits().hits()[2].id(), equalTo("1"));
|
||||
assertThat(response.getHits().hits()[2].score(), equalTo(2f));
|
||||
|
||||
response = client()
|
||||
.prepareSearch("test")
|
||||
.setQuery(
|
||||
QueryBuilders.hasChildQuery("child",
|
||||
QueryBuilders.customScoreQuery(matchQuery("c_field2", 0)).script("doc['c_field1'].value")).scoreType("avg"))
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(response.getHits().totalHits(), equalTo(3l));
|
||||
assertThat(response.getHits().hits()[0].id(), equalTo("3"));
|
||||
assertThat(response.getHits().hits()[0].score(), equalTo(4f));
|
||||
assertThat(response.getHits().hits()[1].id(), equalTo("2"));
|
||||
assertThat(response.getHits().hits()[1].score(), equalTo(3f));
|
||||
assertThat(response.getHits().hits()[2].id(), equalTo("1"));
|
||||
assertThat(response.getHits().hits()[2].score(), equalTo(1.5f));
|
||||
|
||||
response = client()
|
||||
.prepareSearch("test")
|
||||
.setQuery(
|
||||
QueryBuilders.hasParentQuery("parent",
|
||||
QueryBuilders.customScoreQuery(matchQuery("p_field1", "p_value3")).script("doc['p_field2'].value"))
|
||||
.scoreType("score")).addSort(SortBuilders.fieldSort("c_field3")).addSort(SortBuilders.scoreSort())
|
||||
.execute().actionGet();
|
||||
|
||||
assertThat(response.getHits().totalHits(), equalTo(7l));
|
||||
assertThat(response.getHits().hits()[0].id(), equalTo("13"));
|
||||
assertThat(response.getHits().hits()[0].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[1].id(), equalTo("14"));
|
||||
assertThat(response.getHits().hits()[1].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[2].id(), equalTo("15"));
|
||||
assertThat(response.getHits().hits()[2].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[3].id(), equalTo("16"));
|
||||
assertThat(response.getHits().hits()[3].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[4].id(), equalTo("17"));
|
||||
assertThat(response.getHits().hits()[4].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[5].id(), equalTo("18"));
|
||||
assertThat(response.getHits().hits()[5].score(), equalTo(5f));
|
||||
assertThat(response.getHits().hits()[6].id(), equalTo("1"));
|
||||
assertThat(response.getHits().hits()[6].score(), equalTo(5f));
|
||||
}
|
||||
|
||||
List<IndexRequestBuilder> createDocBuilders() {
|
||||
List<IndexRequestBuilder> indexBuilders = new ArrayList<IndexRequestBuilder>();
|
||||
// Parent 1 and its children
|
||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -35,6 +35,8 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|||
import org.elasticsearch.common.xcontent.XContentFactory;
|
||||
import org.elasticsearch.index.mapper.Uid;
|
||||
import org.elasticsearch.index.query.FilterBuilders;
|
||||
import org.elasticsearch.index.query.QueryBuilders;
|
||||
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
|
||||
import org.elasticsearch.search.SearchHit;
|
||||
import org.elasticsearch.search.SearchHitField;
|
||||
import org.elasticsearch.test.ElasticsearchIntegrationTest;
|
||||
|
@ -245,21 +247,21 @@ public class SimpleSortTests extends ElasticsearchIntegrationTest {
|
|||
|
||||
refresh();
|
||||
|
||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).execute().actionGet();
|
||||
SearchResponse searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), ScoreFunctionBuilders.scriptFunction("_source.field"))).execute().actionGet();
|
||||
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
|
||||
assertThat(searchResponse.getHits().getAt(1).score(), Matchers.lessThan(searchResponse.getHits().getAt(0).score()));
|
||||
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
|
||||
assertThat(searchResponse.getHits().getAt(2).score(), Matchers.lessThan(searchResponse.getHits().getAt(1).score()));
|
||||
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
|
||||
|
||||
searchResponse = client().prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).addSort("_score", SortOrder.DESC).execute().actionGet();
|
||||
searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), ScoreFunctionBuilders.scriptFunction("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
|
||||
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
|
||||
assertThat(searchResponse.getHits().getAt(1).score(), Matchers.lessThan(searchResponse.getHits().getAt(0).score()));
|
||||
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
|
||||
assertThat(searchResponse.getHits().getAt(2).score(), Matchers.lessThan(searchResponse.getHits().getAt(1).score()));
|
||||
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
|
||||
|
||||
searchResponse = client().prepareSearch("test").setQuery(customScoreQuery(matchAllQuery()).script("_source.field")).addSort("_score", SortOrder.DESC).execute().actionGet();
|
||||
searchResponse = client().prepareSearch("test").setQuery(QueryBuilders.functionScoreQuery(matchAllQuery(), ScoreFunctionBuilders.scriptFunction("_source.field"))).addSort("_score", SortOrder.DESC).execute().actionGet();
|
||||
assertThat(searchResponse.getHits().getAt(2).getId(), equalTo("3"));
|
||||
assertThat(searchResponse.getHits().getAt(1).getId(), equalTo("2"));
|
||||
assertThat(searchResponse.getHits().getAt(0).getId(), equalTo("1"));
|
||||
|
|
Loading…
Reference in New Issue