Skip to content

Commit

Permalink
support explainscore
Browse files Browse the repository at this point in the history
1. support tft.explainscore command
2. fix tairsearch failed test case because of the change of score by using regexp
  • Loading branch information
lyq2333 authored and yangbodong22011 committed Nov 9, 2023
1 parent a28d4d7 commit 366ea1d
Show file tree
Hide file tree
Showing 8 changed files with 286 additions and 104 deletions.
1 change: 1 addition & 0 deletions src/main/java/com/aliyun/tair/ModuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,7 @@ public enum ModuleCommand implements ProtocolCommand {
TFTDELDOCFIELD("tft.deldocfield"),
TFTANALYZER("tft.analyzer"),
TFTEXPLAINCOST("tft.explaincost"),
TFTEXPLAINSCORE("tft.explainscore"),
TFTADDSUG("tft.addsug"),
TFTDELSUG("tft.delsug"),
TFTSUGNUM("tft.sugnum"),
Expand Down
46 changes: 46 additions & 0 deletions src/main/java/com/aliyun/tair/tairsearch/TairSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,52 @@ public String tftexplaincost(byte[] index, byte[] request) {
}
}

/**
* explain the score of query.
*
* @param index the index name
* @param ssb the SearchSourceBuilder
* @param docId the document id(s)
* @return Success: Search result with score explanation
*/
public String tftexplainscore(String index, SearchSourceBuilder ssb, String... docId) {
return tftexplainscore(index, ssb.toString(), docId);
}

public String tftexplainscore(byte[] index, SearchSourceBuilder ssb, byte[]... docId) {
return tftexplainscore(index, SafeEncoder.encode(ssb.toString()), docId);
}

/**
* explain the score of query.
*
* @param index the index name
* @param request the query clause
* @param docId the document id(s)
* @return Success: Search result with score explanation
*/
public String tftexplainscore(String index, String request, String... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
Jedis jedis = getJedis();
try {
Object obj = jedis.sendCommand(ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return BuilderFactory.STRING.build(obj);
} finally {
releaseJedis(jedis);
}
}

public String tftexplainscore(byte[] index, byte[] request, byte[]... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
Jedis jedis = getJedis();
try {
Object obj = jedis.sendCommand(ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return BuilderFactory.STRING.build(obj);
} finally {
releaseJedis(jedis);
}
}

/**
* Add suggestions in index.
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/aliyun/tair/tairsearch/TairSearchCluster.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.aliyun.tair.tairsearch.search.builder.SearchSourceBuilder;
import com.aliyun.tair.util.JoinParameters;
import redis.clients.jedis.BuilderFactory;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.ScanResult;
import redis.clients.jedis.util.SafeEncoder;
Expand Down Expand Up @@ -416,6 +417,43 @@ public String tftexplaincost(byte[] index, byte[] request) {
return BuilderFactory.STRING.build(obj);
}

/**
* explain the score of query.
*
* @param index the index name
* @param ssb the SearchSourceBuilder
* @param docId the document id(s)
* @return Success: Search result with score explanation
*/
public String tftexplainscore(String index, SearchSourceBuilder ssb, String... docId) {
return tftexplainscore(index, ssb.toString(), docId);
}

public String tftexplainscore(byte[] index, SearchSourceBuilder ssb, byte[]... docId) {
return tftexplainscore(index, SafeEncoder.encode(ssb.toString()), docId);
}

/**
* explain the score of query.
*
* @param index the index name
* @param request the query clause
* @param docId the document id(s)
* @return Success: Search result with score explanation
*/
public String tftexplainscore(String index, String request, String... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
Object obj = jc.sendCommand(SafeEncoder.encode(index), ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return BuilderFactory.STRING.build(obj);

}

public String tftexplainscore(byte[] index, byte[] request, byte[]... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
Object obj = jc.sendCommand(index, ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return BuilderFactory.STRING.build(obj);
}

/**
* Add suggestions in index.
*
Expand Down
21 changes: 21 additions & 0 deletions src/main/java/com/aliyun/tair/tairsearch/TairSearchPipeline.java
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,27 @@ public Response<String> tftexplaincost(byte[] index, byte[] request) {
return getResponse(BuilderFactory.STRING);
}

public Response<String> tftexplainscore(String index, SearchSourceBuilder ssb, String... docId) {
return tftexplainscore(index, ssb.toString(), docId);
}

public Response<String> tftexplainscore(byte[] index, SearchSourceBuilder ssb, byte[]... docId) {
return tftexplainscore(index, SafeEncoder.encode(ssb.toString()), docId);
}

public Response<String> tftexplainscore(String index, String request, String... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
getClient("").sendCommand(ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return getResponse(BuilderFactory.STRING);

}

public Response<String> tftexplainscore(byte[] index, byte[] request, byte[]... docId) {
TFTExplainScoreParams params = new TFTExplainScoreParams();
getClient("").sendCommand(ModuleCommand.TFTEXPLAINSCORE, params.getByteParams(index, request, docId));
return getResponse(BuilderFactory.STRING);
}

/**
* Add suggestions in index.
*
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.aliyun.tair.tairsearch.params;

import redis.clients.jedis.util.SafeEncoder;

import java.util.ArrayList;

public class TFTExplainScoreParams {
public byte[][] getByteParams(String key, String request, String... docIds) {
ArrayList<byte[]> byteParams = new ArrayList<byte[]>();

byteParams.add(SafeEncoder.encode(key));
byteParams.add(SafeEncoder.encode(request));

for (String s : docIds) {
byteParams.add(SafeEncoder.encode(s));
}

return byteParams.toArray(new byte[byteParams.size()][]);
}

public byte[][] getByteParams(byte[] key, byte[] request, byte[]... docIds) {
ArrayList<byte[]> byteParams = new ArrayList<byte[]>();

byteParams.add(key);
byteParams.add(request);

for (byte[] arg : docIds) {
byteParams.add(arg);
}
return byteParams.toArray(new byte[byteParams.size()][]);
}
}
Loading

0 comments on commit 366ea1d

Please sign in to comment.