Skip to content

Commit

Permalink
Throw exception if decay is requested for a field with multiple values
Browse files Browse the repository at this point in the history
closes #3960
  • Loading branch information
brwe committed Apr 24, 2014
1 parent 0631b6a commit 95d7815
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Expand Up @@ -22,6 +22,7 @@
import org.apache.lucene.index.AtomicReaderContext;
import org.apache.lucene.search.ComplexExplanation;
import org.apache.lucene.search.Explanation;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.ElasticsearchIllegalArgumentException;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.geo.GeoDistance;
Expand Down Expand Up @@ -283,6 +284,9 @@ public GeoFieldDataScoreFunction(GeoPoint origin, double scale, double decay, do
@Override
public void setNextReader(AtomicReaderContext context) {
geoPointValues = fieldData.load(context).getGeoPointValues();
if (geoPointValues.isMultiValued()) {
throw new ElasticsearchException("Field " + fieldData.getFieldNames().fullName() + " is multy valued. Cannot compute decay for more than one value.");
}
}

private final GeoPoint getValue(int doc, GeoPoint missing) {
Expand Down Expand Up @@ -330,6 +334,9 @@ public NumericFieldDataScoreFunction(double origin, double scale, double decay,

public void setNextReader(AtomicReaderContext context) {
this.doubleValues = this.fieldData.load(context).getDoubleValues();
if (doubleValues.isMultiValued()) {
throw new ElasticsearchException("Field " + fieldData.getFieldNames().fullName() + "is multy valued. Cannot compute decay for more than one value.");
}
}

private final double getValue(int doc, double missing) {
Expand Down
Expand Up @@ -702,5 +702,40 @@ public void testNoQueryGiven() throws Exception {
"multiply"))));
response.actionGet();
}

@Test
public void testMultiValuedFieldException() throws Throwable {
assertAcked(prepareCreate("test").addMapping(
"type",
jsonBuilder().startObject().startObject("type").startObject("properties").startObject("test").field("type", "string")
.endObject().startObject("num").field("type", "double").endObject().startObject("geo").field("type", "geo_point")
.endObject().endObject().endObject().endObject()));
ensureYellow();
double[] numVals = { 1.0, 2.0, 3.0 };

client().index(
indexRequest("test").type("type").source(
jsonBuilder().startObject().field("test", "value").field("num", numVals).field("geo").startArray().startObject()
.field("lat", 1).field("lon", 1).endObject().startObject().field("lat", 1).field("lon", 2).endObject().endArray()
.endObject())).actionGet();
refresh();
SearchResponse response = client().prepareSearch("test")
.setQuery(functionScoreQuery().add(new MatchAllFilterBuilder(), linearDecayFunction("num", 1, 0.5)).scoreMode("multiply"))
.execute().actionGet();

assertThat(response.getShardFailures().length, equalTo(1));
assertThat(response.getHits().getHits().length, equalTo(0));

List<Float> lonlat = new ArrayList<Float>();
lonlat.add(new Float(1));
lonlat.add(new Float(1));

response = client().prepareSearch("test")
.setQuery(functionScoreQuery().add(new MatchAllFilterBuilder(), linearDecayFunction("geo", lonlat, "1000km")).scoreMode("multiply"))
.execute().actionGet();

assertThat(response.getShardFailures().length, equalTo(1));
assertThat(response.getHits().getHits().length, equalTo(0));
}

}

0 comments on commit 95d7815

Please sign in to comment.