Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

We’re showing branches in this repository, but you can also compare across forks.

base fork: sguo/sensei
...
head fork: sguo/sensei
  • 5 commits
  • 6 files changed
  • 0 commit comments
  • 2 contributors
59 sensei-core/src/main/antlr3/com/senseidb/bql/parsers/BQL.g
View
@@ -2049,24 +2049,66 @@ null_predicate returns [JSONObject json]
}
;
-non_variable_value_list returns [Object json]
+non_variable_value_list returns [JSONArray json]
@init {
- JSONArray jsonArray = new JSONArray();
+ $json = new JSONArray();
}
: LPAR v=value
{
- jsonArray.put($v.val);
+ $json.put($v.val);
}
(COMMA v=value
{
- jsonArray.put($v.val);
+ $json.put($v.val);
}
)* RPAR
+ ;
+
+python_style_list returns [JSONArray json]
+@init {
+ $json = new JSONArray();
+}
+ : '[' v=python_style_value
+ {
+ $json.put($v.val);
+ }
+ (COMMA v=python_style_value
+ {
+ $json.put($v.val);
+ }
+ )* ']'
+ ;
+
+python_style_dict returns [JSONObject json]
+@init {
+ $json = new JSONObject();
+}
+ : '{' p=key_value_pair
{
- $json = jsonArray;
+ try {
+ $json.put($p.key, $p.val);
+ }
+ catch (JSONException err) {
+ throw new FailedPredicateException(input, "python_style_dict", "JSONException: " + err.getMessage());
+ }
}
+ (COMMA p=key_value_pair
+ {
+ try {
+ $json.put($p.key, $p.val);
+ }
+ catch (JSONException err) {
+ throw new FailedPredicateException(input, "python_style_dict", "JSONException: " + err.getMessage());
+ }
+ }
+ )* '}'
;
+python_style_value returns [Object val]
+ : value { $val = $value.val; }
+ | python_style_list { $val = $python_style_list.json; }
+ | python_style_dict { $val = $python_style_dict.json; }
+ ;
value_list returns [Object json]
: non_variable_value_list
@@ -2158,7 +2200,7 @@ prop_list returns [JSONObject json]
;
key_value_pair returns [String key, Object val]
- : STRING_LITERAL COLON (v=value | vs=non_variable_value_list)
+ : STRING_LITERAL COLON (v=value | vs=python_style_list | vd=python_style_dict)
{
String orig = $STRING_LITERAL.text;
orig = orig.substring(1, orig.length() - 1);
@@ -2166,9 +2208,12 @@ key_value_pair returns [String key, Object val]
if (v != null) {
$val = $v.val;
}
- else {
+ else if (vs != null) {
$val = $vs.json;
}
+ else {
+ $val = $vd.json;
+ }
}
;
132 sensei-core/src/main/java/com/senseidb/search/relevance/impl/CompilationHelper.java
View
@@ -89,7 +89,8 @@
"values": {
"c":[1996,1997],
"e":0.98,
- "j":{"key":[1,2,3], "value":[2.3, 3.4, 2.9]} // a user input hashmap;
+ "j":{"1":2.3, "2":3.4, "3":2.9} // A user input hashmap;
+ "jj":{"key":[1,2,3], "value":[2.3, 3.4, 2.9]} // It also supports this method to pass in a map.
}
}
@@ -151,10 +152,10 @@
"values":{
"goodYear":[1996,1997],
"thisYear":2001,
- "mileageWeight":{"key":[11400,11000],"value":[777.9, 10.2]},
- "yearcolor":{"key":[1998],"value":["red"]},
- "colorweight":{"key":["red"],"value":[335.5]},
- "categorycolor":{"key":["compact"],"value":["red"]},
+ "mileageWeight":{"11400":777.9, "11000":10.2},
+ "yearcolor":{"1998":"red"},
+ "colorweight":{"red":335.5},
+ "categorycolor":{"compact":"red"},
"coolTag":"cool"
}
}
@@ -667,20 +668,31 @@ else if (typeNum >= RelevanceJSONConstants.TYPENUMBER_MAP_INT_INT &&
if (values == null)
throw new JSONException("Variable "+ symbol + " does not have value.");
- JSONArray keysList = values.optJSONArray("key");
- JSONArray valuesList = values.optJSONArray("value");
-
- if (keysList == null)
- throw new JSONException("Variable " + symbol + " is a map, but does not have a key list.");
-
- if (valuesList == null)
- throw new JSONException("Variable " + symbol + "is a map, but does not have a value list.");
-
+ JSONArray keysList = values.names();
int keySize = keysList.length();
- int valueSize = valuesList.length();
-
- if (keySize != valueSize)
- throw new JSONException("Variable " + symbol + ": key size is different from value size, can not convert to a map." );
+
+ // denote if the map is represented in a way of combination of key jsonarray and value jsonarray;
+ boolean isKeyValue = isKeyValueArrayMethod(values);
+ JSONArray keysArrayList = null, valuesArrayList = null;
+ int keyArraySize, valueArraySize;
+ if(isKeyValue)
+ {
+ keysArrayList = values.optJSONArray(RelevanceJSONConstants.KW_KEY);
+ valuesArrayList = values.optJSONArray(RelevanceJSONConstants.KW_VALUE);
+
+ if (keysArrayList == null)
+ throw new JSONException("Variable " + symbol + " is a map, but does not have a key list.");
+
+ if (valuesArrayList == null)
+ throw new JSONException("Variable " + symbol + "is a map, but does not have a value list.");
+
+ keyArraySize = keysArrayList.length();
+ valueArraySize = valuesArrayList.length();
+ if (keyArraySize != valueArraySize)
+ throw new JSONException("Variable " + symbol + ": key size is different from value size, can not convert to a map." );
+
+ keySize = keysArrayList.length();
+ }
Map hm = null;
switch (typeNum)
@@ -689,70 +701,115 @@ else if (typeNum >= RelevanceJSONConstants.TYPENUMBER_MAP_INT_INT &&
hm = new Int2IntOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Int2IntOpenHashMap) hm).put(keysList.getInt(j), valuesList.getInt(j));
+ if(isKeyValue)
+ ((Int2IntOpenHashMap) hm).put(keysArrayList.getInt(j), valuesArrayList.getInt(j));
+ else
+ ((Int2IntOpenHashMap) hm).put(keysList.getInt(j), values.getInt(keysList.getString(j)));
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_INT_DOUBLE:
hm = new Int2DoubleOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Int2DoubleOpenHashMap) hm).put(keysList.getInt(j), valuesList.getDouble(j));
+ if(isKeyValue)
+ ((Int2DoubleOpenHashMap) hm).put(keysArrayList.getInt(j), valuesArrayList.getDouble(j));
+ else
+ ((Int2DoubleOpenHashMap) hm).put(keysList.getInt(j), values.getDouble(keysList.getString(j)));
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_INT_FLOAT:
hm = new Int2FloatOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Int2FloatOpenHashMap) hm).put(keysList.getInt(j), (float) valuesList.getDouble(j));
+ if(isKeyValue)
+ ((Int2FloatOpenHashMap) hm).put(keysArrayList.getInt(j), (float) valuesArrayList.getDouble(j));
+ else
+ ((Int2FloatOpenHashMap) hm).put(keysList.getInt(j), (float) values.getDouble(keysList.getString(j)));
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_INT_LONG:
hm = new Int2LongOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Int2LongOpenHashMap) hm).put(keysList.getInt(j), Long.parseLong(valuesList.getString(j)));
+ if(isKeyValue)
+ ((Int2LongOpenHashMap) hm).put(keysArrayList.getInt(j), Long.parseLong(valuesArrayList.getString(j)));
+ else
+ ((Int2LongOpenHashMap) hm).put(keysList.getInt(j), Long.parseLong(values.getString(keysList.getString(j))));
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_INT_STRING:
hm = new Int2ObjectOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Int2ObjectOpenHashMap) hm).put(keysList.getInt(j), valuesList.getString(j));
+ if(isKeyValue)
+ ((Int2ObjectOpenHashMap) hm).put(keysArrayList.getInt(j), valuesArrayList.getString(j));
+ else
+ ((Int2ObjectOpenHashMap) hm).put(keysList.getInt(j), values.getString(keysList.getString(j)));
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_STRING_INT:
hm = new Object2IntOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Object2IntOpenHashMap) hm).put(keysList.getString(j), valuesList.getInt(j));
+ if(isKeyValue)
+ ((Object2IntOpenHashMap) hm).put(keysArrayList.getString(j), valuesArrayList.getInt(j));
+ else
+ {
+ String key = keysList.getString(j);
+ ((Object2IntOpenHashMap) hm).put(key, values.getInt(key));
+ }
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_STRING_DOUBLE:
hm = new Object2DoubleOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Object2DoubleOpenHashMap) hm).put(keysList.getString(j), valuesList.getDouble(j));
+ if(isKeyValue)
+ ((Object2DoubleOpenHashMap) hm).put(keysArrayList.getString(j), valuesArrayList.getDouble(j));
+ else
+ {
+ String key = keysList.getString(j);
+ ((Object2DoubleOpenHashMap) hm).put(key, values.getDouble(key));
+ }
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_STRING_FLOAT:
hm = new Object2FloatOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Object2FloatOpenHashMap) hm).put(keysList.getString(j), (float) valuesList.getDouble(j));
+ if(isKeyValue)
+ ((Object2FloatOpenHashMap) hm).put(keysArrayList.getString(j), (float) valuesArrayList.getDouble(j));
+ else
+ {
+ String key = keysList.getString(j);
+ ((Object2FloatOpenHashMap) hm).put(key, (float) values.getDouble(key));
+ }
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_STRING_LONG:
hm = new Object2LongOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Object2LongOpenHashMap) hm).put(keysList.getString(j), Long.parseLong(valuesList.getString(j)));
+ if(isKeyValue)
+ ((Object2LongOpenHashMap) hm).put(keysArrayList.getString(j), Long.parseLong(valuesArrayList.getString(j)));
+ else
+ {
+ String key = keysList.getString(j);
+ ((Object2LongOpenHashMap) hm).put(key, Long.parseLong(values.getString(keysList.getString(j))));
+ }
}
break;
case RelevanceJSONConstants.TYPENUMBER_MAP_STRING_STRING:
hm = new Object2ObjectOpenHashMap();
for (int j = 0; j < keySize; j++)
{
- ((Object2ObjectOpenHashMap) hm).put(keysList.getString(j), valuesList.getString(j));
+ if(isKeyValue)
+ ((Object2ObjectOpenHashMap) hm).put(keysArrayList.getString(j), valuesArrayList.getString(j));
+ else
+ {
+ String key = keysList.getString(j);
+ ((Object2ObjectOpenHashMap) hm).put(key, values.getString(key));
+ }
}
break;
}
@@ -802,6 +859,25 @@ else if (typeNum >= RelevanceJSONConstants.TYPENUMBER_INT &&
}
} // End of initializeValues()
+ /**
+ * check if in the JSON values part the map variable is represented in a json map way or two key and value json array;
+ * "JsonMapway":{"1":2.3, "2":3.4, "3":2.9} // A user input hashmap;
+ * "KeyValueJsonArrayPairWay":{"key":[1,2,3], "value":[2.3, 3.4, 2.9]} // It also supports this method to pass in a map.
+ * @param values
+ * @return boolean
+ */
+ private static boolean isKeyValueArrayMethod(JSONObject mapJSON)
+ {
+ if(mapJSON.has(RelevanceJSONConstants.KW_KEY) && mapJSON.has(RelevanceJSONConstants.KW_VALUE))
+ {
+ JSONArray keysList = mapJSON.optJSONArray(RelevanceJSONConstants.KW_KEY);
+ JSONArray valuesList = mapJSON.optJSONArray(RelevanceJSONConstants.KW_VALUE);
+ if(keysList != null && valuesList != null)
+ return true;
+ }
+ return false;
+ }
+
private static void addFacilityMethods(CtClass ch) throws JSONException
{
addMethod(EXP_INT_METHOD, ch);
2  sensei-core/src/main/java/com/senseidb/search/relevance/impl/RelevanceJSONConstants.java
View
@@ -10,6 +10,8 @@
// (1) json keys;
public static final String KW_MODEL = "model";
public static final String KW_PREDEFINED = "predefined_model";
+ public static final String KW_KEY = "key";
+ public static final String KW_VALUE = "value";
public static final String KW_VALUES = "values";
public static final String KW_VARIABLES = "variables";
public static final String KW_FACETS = "facets";
46 sensei-core/src/test/java/com/senseidb/search/facet/attribute/USCPExtractor.java
View
@@ -1,46 +0,0 @@
-package com.senseidb.search.facet.attribute;
-
-import java.io.File;
-import java.io.FileWriter;
-import java.util.HashSet;
-import java.util.Set;
-
-import org.apache.commons.io.FileUtils;
-import org.json.JSONArray;
-import org.json.JSONObject;
-
-public class USCPExtractor {
- public static void main(String[] args) throws Exception {
- String uscpStr = FileUtils.readFileToString(new File("uscp.txt"));
- JSONObject uscpJson = new JSONObject(uscpStr);
- JSONArray hits = uscpJson.getJSONArray("hits");
- File result = new File("uscp_data.json");
- if (result.exists()) result.delete();
- FileWriter fileWriter = new FileWriter(result, true);
- Set<String> obj_props = new HashSet<String>();
- for (int i = 0; i < hits.length(); i++) {
- JSONArray stored = hits.getJSONObject(i).getJSONArray("stored");
- String srcData = hits.getJSONObject(i).getString("srcdata");
- JSONObject ret = new JSONObject(srcData);
- for (int j = 0; j < stored.length(); j++) {
- JSONObject valJson = stored.optJSONObject(j);
- if (valJson == null) {
- continue;
- }
- String name = valJson.optString("name");
- String val = valJson.optString("val");
-
- if (name == null || val == null || name.startsWith("_")) {
- continue;
- }
- ret.put(name, val);
- }
-
- //fileWriter.append(ret + "\n");
- if (ret.optString("object_property") != null && ret.optString("object_property").length() > 0) {
- obj_props.add(ret.optString("object_property"));
- }
- }
- System.out.println(obj_props);
- }
-}
125 sensei-core/src/test/java/com/senseidb/test/TestSensei.java
View
@@ -253,7 +253,7 @@ public void testBqlExtraFilter() throws Exception
public void testBqlRelevance1() throws Exception
{
logger.info("Executing test case testBqlRelevance1");
- String req = "{\"bql\":\"SELECT * FROM cars USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':(1996)) DEFINED AS (int thisYear, IntOpenHashSet goodYear) BEGIN if (goodYear.contains(year)) return (float)Math.exp(10d); if (year==thisYear) return 87f; return _INNER_SCORE; END\"}";
+ String req = "{\"bql\":\"SELECT * FROM cars USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) DEFINED AS (int thisYear, IntOpenHashSet goodYear) BEGIN if (goodYear.contains(year)) return (float)Math.exp(10d); if (year==thisYear) return 87f; return _INNER_SCORE; END\"}";
JSONObject res = search(new JSONObject(req));
@@ -774,8 +774,8 @@ public void testRelevanceInnerScore() throws Exception
public void testRelevanceHashMapInt2Float() throws Exception
{
- logger.info("executing test case testRelevanceHashMapInt2Int");
- String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(mileageWeight.containsKey(mileage)) return 10000+mileageWeight.get(mileage); if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ logger.info("executing test case testRelevanceHashMapInt2Float");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(mileageWeight.containsKey(mileage)) return 10000+mileageWeight.get(mileage); if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
@@ -801,7 +801,7 @@ public void testRelevanceHashMapInt2Float() throws Exception
public void testRelevanceHashMapInt2String() throws Exception
{
logger.info("executing test case testRelevanceHashMapInt2String");
- String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\"if(yearcolor.containsKey(year) && yearcolor.get(year).equals(color)) return 100000f; if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\"if(yearcolor.containsKey(year) && yearcolor.get(year).equals(color)) return 100000f; if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
@@ -832,7 +832,7 @@ public void testRelevanceHashMapInt2String() throws Exception
public void testRelevanceHashMapString2Float() throws Exception
{
logger.info("executing test case testRelevanceHashMapString2Float");
- String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(colorweight.containsKey(color) ) return 200f + colorweight.getFloat(color); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(colorweight.containsKey(color) ) return 200f + colorweight.getFloat(color); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
@@ -856,8 +856,8 @@ public void testRelevanceHashMapString2Float() throws Exception
public void testRelevanceHashMapString2String() throws Exception
{
- logger.info("executing test case testRelevanceHashMapInt2Int");
- String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(categorycolor.containsKey(category) && categorycolor.get(category).equals(color)) return 10000f; if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ logger.info("executing test case testRelevanceHashMapString2String");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(categorycolor.containsKey(category) && categorycolor.get(category).equals(color)) return 10000f; if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"1998\":\"red\"},\"mileageWeight\":{\"11400\":777.9, \"11000\":10.2},\"colorweight\":{\"red\":335.5},\"goodYear\":[1996,1997],\"categorycolor\":{\"compact\":\"red\"}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
JSONObject res = search(new JSONObject(req));
assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
//the first one should has socre 10777.900390625, and mileage: 11400;
@@ -884,6 +884,117 @@ public void testRelevanceHashMapString2String() throws Exception
assertEquals("color for second is not correct." , true, secondColor.equals("red") );
}
+ public void testRelevanceHashMapInt2FloatArrayWay() throws Exception
+ {
+ logger.info("executing test case testRelevanceHashMapInt2FloatArrayWay");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"long\":[\"groupid\"]},\"function\":\" if(mileageWeight.containsKey(mileage)) return 10000+mileageWeight.get(mileage); if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"]}},\"values\":{\"thisYear\":2001,\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"goodYear\":[1996,1997]}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ JSONObject res = search(new JSONObject(req));
+ assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
+ //the first one should has socre 10777.900390625, and mileage: 11400;
+ JSONArray hits = res.getJSONArray("hits");
+ JSONObject firstHit = hits.getJSONObject(0);
+ JSONObject secondHit = hits.getJSONObject(1);
+
+ double firstScore = firstHit.getDouble("_score");
+ double secondScore = secondHit.getDouble("_score");
+
+ String firstMileage = firstHit.getJSONArray("mileage").getString(0);
+ String secondMileage = secondHit.getJSONArray("mileage").getString(0);
+
+ assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10777.900390625) < 1 );
+ assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10777.900390625) < 1 );
+
+ assertEquals("mileage for first is not correct." , true, Integer.parseInt(firstMileage)==11400 );
+ assertEquals("mileage for second is not correct." , true, Integer.parseInt(secondMileage)==11400 );
+
+ }
+
+
+ public void testRelevanceHashMapInt2StringArrayWay() throws Exception
+ {
+ logger.info("executing test case testRelevanceHashMapInt2StringArrayWay");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\"if(yearcolor.containsKey(year) && yearcolor.get(year).equals(color)) return 100000f; if(goodYear.contains(year)) return (float)Math.exp(2d); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ JSONObject res = search(new JSONObject(req));
+ assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
+ //the first one should has socre 10777.900390625, and mileage: 11400;
+ JSONArray hits = res.getJSONArray("hits");
+ JSONObject firstHit = hits.getJSONObject(0);
+ JSONObject secondHit = hits.getJSONObject(1);
+
+ double firstScore = firstHit.getDouble("_score");
+ double secondScore = secondHit.getDouble("_score");
+
+ String firstYear = firstHit.getJSONArray("year").getString(0);
+ String secondYear = secondHit.getJSONArray("year").getString(0);
+
+ String firstColor = firstHit.getJSONArray("color").getString(0);
+ String secondColor = secondHit.getJSONArray("color").getString(0);
+
+ assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 100000) < 1 );
+ assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 100000) < 1 );
+
+ assertEquals("year for first is not correct." , true, Integer.parseInt(firstYear)==1998 );
+ assertEquals("year for second is not correct." , true, Integer.parseInt(secondYear)==1998 );
+
+ assertEquals("color for first is not correct." , true, firstColor.equals("red") );
+ assertEquals("color for second is not correct." , true, secondColor.equals("red") );
+
+ }
+
+ public void testRelevanceHashMapString2FloatArrayWay() throws Exception
+ {
+ logger.info("executing test case testRelevanceHashMapString2FloatArrayWay");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(colorweight.containsKey(color) ) return 200f + colorweight.getFloat(color); if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ JSONObject res = search(new JSONObject(req));
+ assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
+ //the first one should has socre 10777.900390625, and mileage: 11400;
+ JSONArray hits = res.getJSONArray("hits");
+ JSONObject firstHit = hits.getJSONObject(0);
+ JSONObject secondHit = hits.getJSONObject(1);
+
+ double firstScore = firstHit.getDouble("_score");
+ double secondScore = secondHit.getDouble("_score");
+
+ String firstColor = firstHit.getJSONArray("color").getString(0);
+ String secondColor = secondHit.getJSONArray("color").getString(0);
+
+ assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 535.5) < 1 );
+ assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 535.5) < 1 );
+
+ assertEquals("color for first is not correct." , true, firstColor.equals("red") );
+ assertEquals("color for second is not correct." , true, secondColor.equals("red") );
+
+ }
+
+ public void testRelevanceHashMapString2StringArrayWay() throws Exception
+ {
+ logger.info("executing test case testRelevanceHashMapString2StringArrayWay");
+ String req = "{\"sort\":[\"_score\"],\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"model\":{\"function_params\":[\"_INNER_SCORE\",\"thisYear\",\"year\",\"goodYear\",\"mileageWeight\",\"mileage\",\"color\",\"yearcolor\",\"colorweight\",\"category\",\"categorycolor\"],\"facets\":{\"int\":[\"year\",\"mileage\"],\"string\":[\"color\",\"category\"],\"long\":[\"groupid\"]},\"function\":\" if(categorycolor.containsKey(category) && categorycolor.get(category).equals(color)) return 10000f; if(year==thisYear) return 87f ; return _INNER_SCORE;\",\"variables\":{\"map_int_float\":[\"mileageWeight\"],\"map_int_string\":[\"yearcolor\"],\"set_int\":[\"goodYear\"],\"int\":[\"thisYear\"],\"map_string_float\":[\"colorweight\"],\"map_string_string\":[\"categorycolor\"]}},\"values\":{\"thisYear\":2001,\"yearcolor\":{\"value\":[\"red\"],\"key\":[1998]},\"mileageWeight\":{\"value\":[777.9,10.2],\"key\":[11400,11000]},\"colorweight\":{\"value\":[335.5],\"key\":[\"red\"]},\"goodYear\":[1996,1997],\"categorycolor\":{\"value\":[\"red\"],\"key\":[\"compact\"]}}}}},\"fetchStored\":false,\"from\":0,\"explain\":false,\"size\":6}";
+ JSONObject res = search(new JSONObject(req));
+ assertEquals("numhits is wrong", 15000, res.getInt("numhits"));
+ //the first one should has socre 10777.900390625, and mileage: 11400;
+ JSONArray hits = res.getJSONArray("hits");
+ JSONObject firstHit = hits.getJSONObject(0);
+ JSONObject secondHit = hits.getJSONObject(1);
+
+ double firstScore = firstHit.getDouble("_score");
+ double secondScore = secondHit.getDouble("_score");
+
+ String firstCategory = firstHit.getJSONArray("category").getString(0);
+ String secondCategory = secondHit.getJSONArray("category").getString(0);
+
+ String firstColor = firstHit.getJSONArray("color").getString(0);
+ String secondColor = secondHit.getJSONArray("color").getString(0);
+
+ assertEquals("inner score for first is not correct." , true, Math.abs(firstScore - 10000) < 1 );
+ assertEquals("inner score for second is not correct." , true, Math.abs(secondScore - 10000) < 1 );
+
+ assertEquals("category for first is not correct." , true, firstCategory.equals("compact") );
+ assertEquals("category for second is not correct." , true, secondCategory.equals("compact") );
+
+ assertEquals("color for first is not correct." , true, firstColor.equals("red") );
+ assertEquals("color for second is not correct." , true, secondColor.equals("red") );
+ }
public void testRelevanceMultiContains() throws Exception
22 sensei-core/src/test/java/com/senseidb/test/bql/parsers/TestBQL.java
View
@@ -1557,7 +1557,7 @@ public void testRelevanceModelExample1() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
- "USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':(1996)) " +
+ "USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) " +
" DEFINED AS (int thisYear, IntOpenHashSet goodYear) " +
" BEGIN " +
" if (goodYear.contains(year)) " +
@@ -1583,7 +1583,7 @@ public void testRelevanceModelExample2() throws Exception
"SELECT * " +
"FROM cars " +
"WHERE color = 'red' " +
- "USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':(1996)) " +
+ "USING RELEVANCE MODEL my_model ('thisYear':2001, 'goodYear':[1996]) " +
" DEFINED AS (int thisYear, IntOpenHashSet goodYear) " +
" BEGIN " +
" if (goodYear.contains(year)) " +
@@ -1628,4 +1628,22 @@ public void testRelevanceModelVariableScopes() throws Exception
assertTrue(_comp.isEquals(json, expected));
}
+ @Test
+ public void testRelevanceModelMapValue() throws Exception
+ {
+ System.out.println("testRelevanceModelMapValue");
+ System.out.println("==================================================");
+
+ JSONObject json = _compiler.compile(
+ "SELECT * " +
+ "FROM cars " +
+ "WHERE color = 'red' " +
+ "USING RELEVANCE MODEL my_model ('thisYear':2001, 'myMap':{'aaa':1, 'bbb':2}) " +
+ "ORDER BY relevance"
+ );
+
+ JSONObject expected = new JSONObject("{\"sort\":\"relevance\",\"query\":{\"query_string\":{\"query\":\"\",\"relevance\":{\"predefined_model\":\"my_model\",\"values\":{\"thisYear\":2001,\"myMap\":{\"aaa\":1,\"bbb\":2}}}}},\"selections\":[{\"term\":{\"color\":{\"value\":\"red\"}}}],\"meta\":{\"select_list\":[\"*\"]}}");
+ assertTrue(_comp.isEquals(json, expected));
+ }
+
}

No commit comments for this range

Something went wrong with that request. Please try again.