Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Change "space" parameter to "space_type" for custom scoring (#232)
Browse files Browse the repository at this point in the history
  • Loading branch information
jmazanec15 authored Sep 21, 2020
1 parent c443f06 commit ee16162
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -135,9 +135,9 @@ private void validateAndInitParams(Map<String, Object> params) {
}

// validate space
final Object space = params.get("space");
final Object space = params.get("space_type");
if (space == null) {
throw new IllegalArgumentException("Missing parameter [space]");
throw new IllegalArgumentException("Missing parameter [space_type]");
}
this.similaritySpace = (String)space;
if (!KNNConstants.COSINESIMIL.equalsIgnoreCase(similaritySpace) && !KNNConstants.L2.equalsIgnoreCase(similaritySpace)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public void testKNNL2ScriptScore() throws Exception {
float[] queryVector = {1.0f, 1.0f};
params.put("field", FIELD_NAME);
params.put("vector", queryVector);
params.put("space", KNNConstants.L2);
params.put("space_type", KNNConstants.L2);
Request request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
Response response = client().performRequest(request);
assertEquals(request.getEndpoint() + ": failed", RestStatus.OK,
Expand Down Expand Up @@ -108,15 +108,15 @@ public void testKNNCosineScriptScore() throws Exception {
* params": {
* "field": "my_dense_vector",
* "vector": [2.0, 2.0],
* "space": "L2"
* "space_type": "L2"
* }
*
*
*/
float[] queryVector = {2.0f, -2.0f};
params.put("field", FIELD_NAME);
params.put("vector", queryVector);
params.put("space", KNNConstants.COSINESIMIL);
params.put("space_type", KNNConstants.COSINESIMIL);
Request request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
Response response = client().performRequest(request);
assertEquals(request.getEndpoint() + ": failed", RestStatus.OK,
Expand Down Expand Up @@ -153,13 +153,13 @@ public void testKNNInvalidSourceScript() throws Exception {
* params": {
* "field": "my_dense_vector",
* "vector": [2.0, 2.0],
* "space": "cosinesimil"
* "space_type": "cosinesimil"
* }
*/
float[] queryVector = {2.0f, -2.0f};
params.put("field", FIELD_NAME);
params.put("vector", queryVector);
params.put("space", KNNConstants.COSINESIMIL);
params.put("space_type", KNNConstants.COSINESIMIL);
Script script = new Script(Script.DEFAULT_SCRIPT_TYPE, KNNScoringScriptEngine.NAME, "Dummy_source", params);
ScriptScoreQueryBuilder sc = new ScriptScoreQueryBuilder(qb, script);

Expand Down Expand Up @@ -198,7 +198,7 @@ public void testInvalidSpace() throws Exception {
float[] queryVector = {2.0f, -2.0f};
params.put("field", FIELD_NAME);
params.put("vector", queryVector);
params.put("space", INVALID_SPACE);
params.put("space_type", INVALID_SPACE);
Request request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(EntityUtils.toString(ex.getResponse().getEntity()),
Expand All @@ -218,7 +218,7 @@ public void testMissingParamsInScript() throws Exception {
Map<String, Object> params = new HashMap<>();
float[] queryVector = {2.0f, -2.0f};
params.put("vector", queryVector);
params.put("space", KNNConstants.COSINESIMIL);
params.put("space_type", KNNConstants.COSINESIMIL);
Request request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(EntityUtils.toString(ex.getResponse().getEntity()),
Expand All @@ -234,11 +234,11 @@ public void testMissingParamsInScript() throws Exception {

// Remove space parameter
params.put("vector", queryVector);
params.remove("space");
params.remove("space_type");
Request space_request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
ex = expectThrows(ResponseException.class, () -> client().performRequest(space_request));
assertThat(EntityUtils.toString(ex.getResponse().getEntity()),
containsString("Missing parameter [space]"));
containsString("Missing parameter [space_type]"));
}

public void testUnequalDimensions() throws Exception {
Expand All @@ -257,7 +257,7 @@ public void testUnequalDimensions() throws Exception {
float[] queryVector = {2.0f, -2.0f, -2.0f}; // query dimension and field dimension mismatch
params.put("field", FIELD_NAME);
params.put("vector", queryVector);
params.put("space", KNNConstants.COSINESIMIL);
params.put("space_type", KNNConstants.COSINESIMIL);
Request request = constructKNNScriptQueryRequest(INDEX_NAME, qb, params, queryVector);
ResponseException ex = expectThrows(ResponseException.class, () -> client().performRequest(request));
assertThat(EntityUtils.toString(ex.getResponse().getEntity()),
Expand Down

0 comments on commit ee16162

Please sign in to comment.