mirror of
https://github.com/honeymoose/OpenSearch.git
synced 2025-02-18 19:05:06 +00:00
Merge pull request #13060 from andrestc/enhancement/functionscore-unmapped
Make FunctionScore work on unmapped field with `missing` parameter
This commit is contained in:
commit
f0b7fa2f31
@ -22,6 +22,7 @@ package org.elasticsearch.common.lucene.search.function;
|
|||||||
import org.apache.lucene.index.LeafReaderContext;
|
import org.apache.lucene.index.LeafReaderContext;
|
||||||
import org.apache.lucene.search.Explanation;
|
import org.apache.lucene.search.Explanation;
|
||||||
import org.elasticsearch.ElasticsearchException;
|
import org.elasticsearch.ElasticsearchException;
|
||||||
|
import org.elasticsearch.index.fielddata.FieldData;
|
||||||
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
|
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
|
||||||
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
|
import org.elasticsearch.index.fielddata.SortedNumericDoubleValues;
|
||||||
|
|
||||||
@ -54,7 +55,13 @@ public class FieldValueFactorFunction extends ScoreFunction {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
|
public LeafScoreFunction getLeafScoreFunction(LeafReaderContext ctx) {
|
||||||
final SortedNumericDoubleValues values = this.indexFieldData.load(ctx).getDoubleValues();
|
final SortedNumericDoubleValues values;
|
||||||
|
if(indexFieldData == null) {
|
||||||
|
values = FieldData.emptySortedNumericDoubles(ctx.reader().maxDoc());
|
||||||
|
} else {
|
||||||
|
values = this.indexFieldData.load(ctx).getDoubleValues();
|
||||||
|
}
|
||||||
|
|
||||||
return new LeafScoreFunction() {
|
return new LeafScoreFunction() {
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -19,11 +19,13 @@
|
|||||||
|
|
||||||
package org.elasticsearch.index.query.functionscore.fieldvaluefactor;
|
package org.elasticsearch.index.query.functionscore.fieldvaluefactor;
|
||||||
|
|
||||||
|
import org.apache.lucene.document.FieldType;
|
||||||
import org.elasticsearch.ElasticsearchException;
|
import org.elasticsearch.ElasticsearchException;
|
||||||
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
|
import org.elasticsearch.common.lucene.search.function.FieldValueFactorFunction;
|
||||||
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
|
import org.elasticsearch.common.lucene.search.function.ScoreFunction;
|
||||||
import org.elasticsearch.common.xcontent.XContentParser;
|
import org.elasticsearch.common.xcontent.XContentParser;
|
||||||
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
|
import org.elasticsearch.index.fielddata.IndexNumericFieldData;
|
||||||
|
import org.elasticsearch.index.fielddata.plain.DoubleArrayIndexFieldData;
|
||||||
import org.elasticsearch.index.mapper.FieldMapper;
|
import org.elasticsearch.index.mapper.FieldMapper;
|
||||||
import org.elasticsearch.index.mapper.MappedFieldType;
|
import org.elasticsearch.index.mapper.MappedFieldType;
|
||||||
import org.elasticsearch.index.query.QueryParseContext;
|
import org.elasticsearch.index.query.QueryParseContext;
|
||||||
@ -86,11 +88,15 @@ public class FieldValueFactorFunctionParser implements ScoreFunctionParser {
|
|||||||
|
|
||||||
SearchContext searchContext = SearchContext.current();
|
SearchContext searchContext = SearchContext.current();
|
||||||
MappedFieldType fieldType = searchContext.mapperService().smartNameFieldType(field);
|
MappedFieldType fieldType = searchContext.mapperService().smartNameFieldType(field);
|
||||||
|
IndexNumericFieldData fieldData = null;
|
||||||
if (fieldType == null) {
|
if (fieldType == null) {
|
||||||
throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]");
|
if(missing == null) {
|
||||||
|
throw new ElasticsearchException("Unable to find a field mapper for field [" + field + "]. No 'missing' value defined.");
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
fieldData = searchContext.fieldData().getForField(fieldType);
|
||||||
}
|
}
|
||||||
return new FieldValueFactorFunction(field, boostFactor, modifier, missing,
|
return new FieldValueFactorFunction(field, boostFactor, modifier, missing, fieldData);
|
||||||
(IndexNumericFieldData)searchContext.fieldData().getForField(fieldType));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
@ -104,6 +104,15 @@ public class FunctionScoreFieldValueIT extends ESIntegTestCase {
|
|||||||
.get();
|
.get();
|
||||||
assertOrderedSearchHits(response, "1", "2", "3");
|
assertOrderedSearchHits(response, "1", "2", "3");
|
||||||
|
|
||||||
|
// field is not mapped but we're defaulting it to 100 so all documents should have the same score
|
||||||
|
response = client().prepareSearch("test")
|
||||||
|
.setExplain(randomBoolean())
|
||||||
|
.setQuery(functionScoreQuery(matchAllQuery(),
|
||||||
|
fieldValueFactorFunction("notmapped").modifier(FieldValueFactorFunction.Modifier.RECIPROCAL).missing(100)))
|
||||||
|
.get();
|
||||||
|
assertEquals(response.getHits().getAt(0).score(), response.getHits().getAt(2).score(), 0);
|
||||||
|
|
||||||
|
|
||||||
// n divided by 0 is infinity, which should provoke an exception.
|
// n divided by 0 is infinity, which should provoke an exception.
|
||||||
try {
|
try {
|
||||||
response = client().prepareSearch("test")
|
response = client().prepareSearch("test")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user