add another script function test

This commit is contained in:
kimchy 2010-07-29 16:07:57 +03:00
parent 4f407e18aa
commit 0186a03684
2 changed files with 20 additions and 7 deletions

View File

@ -100,6 +100,19 @@ public class ScriptFieldsFunction implements FieldsFunction, Map {
return fieldData.docFieldData(docId);
}
public boolean containsKey(Object key) {
// assume its a string...
String fieldName = key.toString();
FieldData fieldData = localCacheFieldData.get(fieldName);
if (fieldData == null) {
FieldMapper mapper = mapperService.smartNameFieldMapper(fieldName);
if (mapper == null) {
return false;
}
}
return true;
}
public int size() {
throw new UnsupportedOperationException();
}
@ -108,10 +121,6 @@ public class ScriptFieldsFunction implements FieldsFunction, Map {
throw new UnsupportedOperationException();
}
public boolean containsKey(Object key) {
throw new UnsupportedOperationException();
}
public boolean containsValue(Object value) {
throw new UnsupportedOperationException();
}

View File

@ -62,15 +62,15 @@ public class ScriptFieldSearchTests extends AbstractNodesTests {
public void testCustomScriptBoost() throws Exception {
client.admin().indices().prepareCreate("test").execute().actionGet();
client.prepareIndex("test", "type1", "1")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).endObject())
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 1.0f).field("date", "1970-01-01T00:00:00").endObject())
.execute().actionGet();
client.admin().indices().prepareFlush().execute().actionGet();
client.prepareIndex("test", "type1", "2")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 2.0f).endObject())
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 2.0f).field("date", "1970-01-01T00:00:25").endObject())
.execute().actionGet();
client.admin().indices().prepareFlush().execute().actionGet();
client.prepareIndex("test", "type1", "3")
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 3.0f).endObject())
.setSource(jsonBuilder().startObject().field("test", "value beck").field("num1", 3.0f).field("date", "1970-01-01T00:02:00").endObject())
.execute().actionGet();
client.admin().indices().refresh(refreshRequest()).actionGet();
@ -79,16 +79,20 @@ public class ScriptFieldSearchTests extends AbstractNodesTests {
.setQuery(matchAllQuery())
.addSort("num1", Order.ASC)
.addScriptField("sNum1", "doc['num1'].value")
.addScriptField("date1", "doc['date'].date.millis")
.execute().actionGet();
assertThat(response.hits().totalHits(), equalTo(3l));
assertThat(response.hits().getAt(0).isSourceEmpty(), equalTo(true));
assertThat(response.hits().getAt(0).id(), equalTo("1"));
assertThat((Double) response.hits().getAt(0).fields().get("sNum1").values().get(0), equalTo(1.0));
assertThat((Long) response.hits().getAt(0).fields().get("date1").values().get(0), equalTo(0l));
assertThat(response.hits().getAt(1).id(), equalTo("2"));
assertThat((Double) response.hits().getAt(1).fields().get("sNum1").values().get(0), equalTo(2.0));
assertThat((Long) response.hits().getAt(1).fields().get("date1").values().get(0), equalTo(25000l));
assertThat(response.hits().getAt(2).id(), equalTo("3"));
assertThat((Double) response.hits().getAt(2).fields().get("sNum1").values().get(0), equalTo(3.0));
assertThat((Long) response.hits().getAt(2).fields().get("date1").values().get(0), equalTo(120000l));
logger.info("running doc['num1'].value * factor");
Map<String, Object> params = MapBuilder.<String, Object>newMapBuilder().put("factor", 2.0).map();