Skip to content

Commit

Permalink
Merge remote branch 'sguo/rel-plugin' into relevance1
Browse files Browse the repository at this point in the history
  • Loading branch information
Baoqiu Cui committed Apr 12, 2012
2 parents 8a7219d + ade9ded commit 3f67045
Show file tree
Hide file tree
Showing 8 changed files with 426 additions and 334 deletions.
1 change: 1 addition & 0 deletions sensei-core/.project
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>sensei-core</name>
<comment>sensei core search. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
Expand Down
Expand Up @@ -12,7 +12,7 @@

import com.senseidb.search.query.ScoreAugmentQuery.ScoreAugmentFunction;
import com.senseidb.search.relevance.CustomRelevanceFactory;
import com.senseidb.search.relevance.impl.JSONConstants;
import com.senseidb.search.relevance.impl.RelevanceJSONConstants;
import com.senseidb.search.relevance.impl.RelevanceQuery;

public abstract class QueryConstructor
Expand Down Expand Up @@ -135,7 +135,7 @@ public static Query constructQuery(JSONObject jsonQuery, QueryParser qparser) th
// return new RelevanceQuery(baseQuery, jsonRelevance);

ScoreAugmentFunction func = CustomRelevanceFactory.build(jsonRelevance);
JSONObject valuesJson = jsonRelevance.optJSONObject(JSONConstants.KW_VALUES);
JSONObject valuesJson = jsonRelevance.optJSONObject(RelevanceJSONConstants.KW_VALUES);
if(func == null)
throw new JSONException("Can not create the score function;");

Expand Down
Expand Up @@ -42,13 +42,28 @@ public static interface ScoreAugmentFunction{
*/
public void initializeGlobal(JSONObject jsonParams) throws JSONException;


/**
* @return whether the innerscore will be used or not. If innerScore is used, newScore(float rawScore, int docID) will be called; Otherwise newScore(int docID) will be called.
*/
public boolean useInnerScore();

/**
* @param rawScore
* @param docID
* @return the modified new score for document;
* @return the modified new score for document with the original innerScore;
*/
public float newScore(float rawScore, int docID);


/**
*
* @param rawScore
* @param docID
* @return the modified new score for document without the original innerScore to save time;
*/
public float newScore(int docID);

/**
* @return the String to explain how the new score is generated;
*/
Expand Down Expand Up @@ -78,8 +93,7 @@ protected AugmentScorer(BoboIndexReader reader,Scorer innerScorer,ScoreAugmentFu
public float score()
throws IOException
{
float rawScore = _innerScorer.score();
return _func.newScore(rawScore, _innerScorer.docID());
return (_func.useInnerScore())? _func.newScore(_innerScorer.score(), _innerScorer.docID()) : _func.newScore(_innerScorer.docID());
}

@Override
Expand Down
Expand Up @@ -9,7 +9,7 @@
import com.senseidb.search.query.ScoreAugmentQuery.ScoreAugmentFunction;
import com.senseidb.search.relevance.impl.CompilationHelper;
import com.senseidb.search.relevance.impl.CustomMathModel;
import com.senseidb.search.relevance.impl.JSONConstants;
import com.senseidb.search.relevance.impl.RelevanceJSONConstants;
import com.senseidb.search.relevance.impl.CompilationHelper.DataTable;

public class CustomRelevanceFactory
Expand All @@ -22,66 +22,71 @@ public static void addCustomRelevanceFunction(String name, CustomRelevanceFuncti
map.put(name, rf);
}

//"relevance":{
// // This relevance part support both runtime anonymous model and pre-loaded relevance class;
// // (a) The runtime model can be defined in this request, and the model will be built instantly in server and cached there, if any following request using same runtime model, it will be re-used.
// // (b) The predefined model class has to extends "com.senseidb.search.relevance.CustomRelevanceFunction" abstract class, and constructed from Json Object;
// //
//
// // (1) Runtime model definition part; this json is used to define a runtime model (input variables, columns/facets, and function parameters and body);
// "model":{
//
// "variables": {
// "set_int":["c","d"], // supported hashset types: [set_int, set_float, set_string, set_double, set_long]
// "map_int_float":["j"], // currently supported hashmap: [map_int_float, map_int_double, map_int_*...] [map_string_int, map_string_float, map_string_*]
// "int":["e","f"], // supported normal variables: [int, double, float, long, bool, string]
// "long":["g","h"]
// },
// "facets":{
// "int":["year","age"], // facet type support: [double, float, int, long, short, string];
// "long":["time"] // facet variable has the same name as the facet name, and they are defined inside this json;
// },
//
// // (2) scoring function and function input parameters in Java;
// // A scoring function and its parameters are the model. A model changes when the function body or signature changes;
//
// // params for the function. Symbol order matters, and symbols must be those defined above. innerScore MUST be used, otherwise, makes no sense to use the custom relevance;
// // reserved keyword for internal parameters are: "_INNER_SCORE" and "_NOW"
//
// "function_params":["_INNER_SCORE", "timeVal", "_timeWeight", "_waterworldWeight", "_half_time"],
//
// // the value string in the following JSONObject is like this (a return statement MUST appear as the last one):
//
// // float delta = System.currentTimeMillis() - timeVal;
// // float t = delta>0 ? delta : 0;
// // float hour = t/(1000*3600);
// // float timeScore = (float) Math.exp(-(hour/_half_time));
// // float waterworldScore = _INNER_SCORE;
// // float time = timeScore * _timeWeight;
// // float water = waterworldScore * _waterworldWeight;
// // return (time + water);
//
// "function":" A LONG JAVA CODE STRING HERE, ONLY AS FUNCTION BODY, NEEDS RETURN STATEMENT."
// },
//
// //(2) Input values for the runtime model, if the model requires input values;
// "values":{
// "c":[1996,1997],
// "e":0.98,
// "j":{"key":[1,2,3], "value":[2.3, 3.4, 2.9]} // a user input hashmap;
// },
//
// // (3) Pre-defined scoreFunction class;
// "predefined-model": "model-name"
// }
// }

/************
*
"relevance":{
// This relevance part support both runtime anonymous model and pre-loaded relevance class;
// (a) The runtime model can be defined in this request, and the model will be built instantly in server and cached there, if any following request using same runtime model, it will be re-used.
// (b) The predefined model class has to extends "com.senseidb.search.relevance.CustomRelevanceFunction" abstract class, and constructed from Json Object;
//
// (1) Runtime model definition part; this json is used to define a runtime model (input variables, columns/facets, and function parameters and body);
"model":{
"variables": {
"set_int":["c","d"], // supported hashset types: [set_int, set_float, set_string, set_double, set_long]
"map_int_float":["j"], // currently supported hashmap: [map_int_float, map_int_double, map_int_*...] [map_string_int, map_string_float, map_string_*]
"int":["e","f"], // supported normal variables: [int, double, float, long, bool, string]
"long":["g","h"]
},
"facets":{
"int":["year","age"], // facet type support: [double, float, int, long, short, string];
"long":["time"] // facet variable has the same name as the facet name, and they are defined inside this json;
},
// (2) scoring function and function input parameters in Java;
// A scoring function and its parameters are the model. A model changes when the function body or signature changes;
// params for the function. Symbol order matters, and symbols must be those defined above. innerScore MUST be used, otherwise, makes no sense to use the custom relevance;
// reserved keyword for internal parameters are: "_INNER_SCORE" and "_NOW"
"function_params":["_INNER_SCORE", "timeVal", "_timeWeight", "_waterworldWeight", "_half_time"],
// the value string in the following JSONObject is like this (a return statement MUST appear as the last one):
// float delta = System.currentTimeMillis() - timeVal;
// float t = delta>0 ? delta : 0;
// float hour = t/(1000*3600);
// float timeScore = (float) Math.exp(-(hour/_half_time));
// float waterworldScore = _INNER_SCORE;
// float time = timeScore * _timeWeight;
// float water = waterworldScore * _waterworldWeight;
// return (time + water);
"function":" A LONG JAVA CODE STRING HERE, ONLY AS FUNCTION BODY, NEEDS RETURN STATEMENT."
},
//(2) Input values for the runtime model, if the model requires input values;
"values":{
"c":[1996,1997],
"e":0.98,
"j":{"key":[1,2,3], "value":[2.3, 3.4, 2.9]} // a user input hashmap;
},
// (3) Pre-defined scoreFunction class;
"predefined-model": "model-name"
}
}
**********/

public static ScoreAugmentFunction build(JSONObject jsonRelevance) throws JSONException
{
// first handle the predefined case if there is any one existing in the json;
if(jsonRelevance.has(JSONConstants.KW_PREDEFINED))
if(jsonRelevance.has(RelevanceJSONConstants.KW_PREDEFINED))
{
String pluginName = jsonRelevance.getString(JSONConstants.KW_PREDEFINED);
String pluginName = jsonRelevance.getString(RelevanceJSONConstants.KW_PREDEFINED);

if(map.containsKey(pluginName))
return map.get(pluginName);
Expand All @@ -92,9 +97,9 @@ public static ScoreAugmentFunction build(JSONObject jsonRelevance) throws JSONEx
}

// runtime anonymous model;
else if (jsonRelevance.has(JSONConstants.KW_MODEL))
else if (jsonRelevance.has(RelevanceJSONConstants.KW_MODEL))
{
JSONObject modelJson = jsonRelevance.optJSONObject(JSONConstants.KW_MODEL);
JSONObject modelJson = jsonRelevance.optJSONObject(RelevanceJSONConstants.KW_MODEL);
DataTable _dt = new DataTable();
CustomMathModel _cModel = CompilationHelper.createCustomMathScorer(modelJson, _dt);
RuntimeRelevanceFunction sm = new RuntimeRelevanceFunction(_cModel, _dt);
Expand Down

0 comments on commit 3f67045

Please sign in to comment.