Skip to content

Commit

Permalink
Merge remote-tracking branch 'sguo/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
wonlay committed Dec 15, 2012
2 parents 409a743 + 4abf720 commit 8dd579c
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import javassist.ClassClassPath;
import javassist.ClassPool;
import javassist.CtClass;
import javassist.CtField;
import javassist.CtMethod;
import javassist.CtNewMethod;
import javassist.NotFoundException;
Expand Down Expand Up @@ -337,6 +338,8 @@ public class CompilationHelper
hs_safe.add("com.senseidb.search.relevance.impl.WeightedMFacetLong");
hs_safe.add("com.senseidb.search.relevance.impl.WeightedMFacetShort");
hs_safe.add("com.senseidb.search.relevance.impl.WeightedMFacetString");

hs_safe.add("java.util.Random");

pool.importPackage("java.util");
for (String cls: hs_safe)
Expand Down Expand Up @@ -560,7 +563,8 @@ public static CustomMathModel createCustomMathScorer(JSONObject jsonModel, DataT
ch.addInterface(ci);
String functionString = makeFuncString(dataTable);

addFacilityMethods(ch);
addStaticFacilityFields(ch);
addStaticFacilityMethods(ch);

CtMethod m;
try
Expand Down Expand Up @@ -905,7 +909,23 @@ private static boolean isKeyValueArrayMethod(JSONObject mapJSON)
return false;
}

private static void addFacilityMethods(CtClass ch) throws JSONException
private static void addStaticFacilityFields(CtClass ch) throws JSONException
{
// add a random field in the object;
CtField f;
try
{
f = CtField.make("public static java.util.Random _RANDOM = new java.util.Random();", ch);
ch.addField(f);
}
catch (CannotCompileException e)
{
logger.info(e.getMessage());
throw new JSONException(e);
}
}

private static void addStaticFacilityMethods(CtClass ch) throws JSONException
{
addMethod(EXP_INT_METHOD, ch);
addMethod(EXP_DOUBLE_METHOD, ch);
Expand Down
21 changes: 21 additions & 0 deletions sensei-core/src/test/java/com/senseidb/test/TestSensei.java
Original file line number Diff line number Diff line change
Expand Up @@ -881,6 +881,27 @@ public void testRelevanceNOW() throws Exception
assertEquals("inner score for second is not correct." , true, secondScore == 10000f);
}

public void testRelevanceStaticRandomField() throws Exception
{
logger.info("executing test case testRelevanceStaticRandomField");

String req1 = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"year\",\"goodYear\",\"_NOW\",\"now\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" return _RANDOM.nextFloat() ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"long\":[\"now\"]}},\"values\":{\"thisYear\":2001,\"now\":"+ System.currentTimeMillis() +",\"goodYear\":[1996]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res1 = search(new JSONObject(req1));
JSONArray hits1 = res1.getJSONArray("hits");
JSONObject firstHit = hits1.getJSONObject(0);

String req2 = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"year\",\"goodYear\",\"_NOW\",\"now\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" return _RANDOM.nextInt(10) + 10.0f ;\",\"variables\":{\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"long\":[\"now\"]}},\"values\":{\"thisYear\":2001,\"now\":"+ System.currentTimeMillis() +",\"goodYear\":[1996]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res2 = search(new JSONObject(req2));
JSONArray hits2 = res2.getJSONArray("hits");
JSONObject secondHit = hits2.getJSONObject(1);

double firstScore = firstHit.getDouble("_score"); // 0.0f (inclusive) to 1.0f (exclusive)
double secondScore = secondHit.getDouble("_score"); // 10.0f (inclusive) to 20.0f (exclusive)

assertEquals("inner score for first is not correct." , true, (firstScore >= 0f && firstScore <1f) );
assertEquals("inner score for second is not correct." , true, (secondScore >= 10f && secondScore < 20f));
}

public void testRelevanceHashMapInt2Float() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2Float");
Expand Down

0 comments on commit 8dd579c

Please sign in to comment.