全部产品

词权重干预词典

使用介绍

词权重又称term weight,通过创建词权重干预词典,并在查询分析中生效该干预词典,就可以起到词权重的干预效果。目前支持对系统内置的词权重词典进行人工干预。用户实现干预操作的过程有以下四步:

  1. 创建词权重干预词典。用户通过搜索算法中心--->召回配置``--->词典管理,进入查询分析干预词典页后,点击页面右上角的“创建”。选择了词典类型后,为词典命名,干预词典创建完成,词典会出现在页面的词典列表中。

  2. 新增干预词典内的干预词条。词典创建完成后,在列表中点击词典名称或点击词典对应的“词条管理”,即可进入干预词典的详情页。用户可在详情页内进行干预词条的新增和管理。用户可选择对应的分析器(目前包括:通用分析器、电商分析器、IT-内容分析器和基于这三个分析器的自定义分析器)对query进行分词,之后对分词后的term进行词权重干预(可设置为:高、中、低三种权重)

  3. 使用干预词典。创建并填充完成停用词干预词典后,可在任意应用的查询规则内选择使用。

  4. 干预词典效果测试和上线。查询分析规则使用了干预词典后,应用到线上之前建议先进行搜索效果测试,评估效果是否符合干预预期。

生效规则

干预数据的生效规则为:

  1. 全query精确匹配优先;

  2. 位置优先:

  3. 相同位置,长度优先:长度是以分词后的term为粒度,一个term的长度为1。匹配的时候有最大的长度限制,当前的限制为5。例:mysql数据库 -> 分词为:mysql 和 数据库,有两个term,长度为2。

  4. 配置查询分析-词权重干预词典时,可以选择query是否忽略空格。

示例

  • 干预数据如下:

    1. 数据库权限管理 -> 数据库:7 | 权限:4 | 管理:1

    2. mysql数据库 -> mysql:7 | 数据库:1

    3. 数据库权限 -> 数据库:4 | 权限:1

    4. linux环境中的mysql数据库权限管理 -> linux:7 | 环境:1 | 中:1 | 的:1 | mysql:7 | 数据库:4 | 权限:1 | 管理:1(检索单元长度超过5)

  • 查询query:

    • mysql数据库权限管理=>会生效b这条干预数据(位置优先)

    • sqlserver数据库权限管理=>会生效a这条干预数据(相同位置,长度优先)

    • 数据库权限如何设置=>会生效c这条干预数据(位置优先)

    • 数据库设置权限=>不会有任何的干预数据生效(无匹配的干预数据)

    • linux环境中的mysql数据库管理权限设置=>会生效d这条规则(全query精确匹配)

    • linux环境中的mysql数据库管理权限设置攻略=> 会生效b任何规则(尽管规则d是本query的部分,但因为包含分词后的term个数超过了5个,因此不会按部分匹配到d这个规则)

    • mysql数据库的数据库权限设置=>会生效b和c两条规则

注意:

  • 在查询改写的时候,可能会改写出来两个查询query,其中第一个query会保留权重7和4的term参与召回,第二个query是用于重查(系统默认是当第一个query查询无结果才会进行重查),为了扩大召回,仅保留权重为7的term去召回。

  • 错误码:6612:term_weight makeup data fail. 错误含义:干预数据没有生效。

实战演练

业务场景:某内容行业业务在OpenSearch的应用实例中配置使用了查询分析规则,规则包含词权重功能,但是在线上发现了badcase,于是决定使用干预功能。

badcase:用户搜索Query数据权限管理,改写的query为:default:'权限' RANK default:'数据' RANK default:'管理',但用户实际想命中“数据”而非“权限”。

问题诊断:内置词权重干预badcase,需要进行词权重干预。

解决方案:新建词权重干预词典,再将该干预词典应用在线上使用的查询分析规则中。

操作步骤

1.在控制台,功能扩展,词典管理,创建词权重词典:

66666666666666666666666666666

填写“名称”,词典类型选择“词权重”,点击“保存”:

2

2.在新建的词权重干预词典里新增干预词条,输入query,选择对应的分析器、设置分词后term的权重:2.13.在查询分析界面把刚刚创建的“词权重干预词典”先应用在一个未上线的查询分析规则中,以便进行搜索效果测试:

888888888888888888

注意:是否忽略空格功能指当在使用词权重干预时是否忽略query中的空格(默认不开启),:输入Query:“sql 数据库” ,干预词条:“sql数据库”,选择“是”则按干预词典中的词权重进行干预,选择“否”则不进行干预。

4.搜索测试是否符合预期效果,将“数据”的权重提高:4

SDK 创建词权重干预词典

Java SDK:maven依赖:

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-opensearch</artifactId>
    <version>0.7.0</version>
</dependency>
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>aliyun-java-sdk-core</artifactId>
    <version>4.5.0</version>
</dependency>

Java SDK Demo

public class TestTermWeightingInQueryProcessor {
    private static DefaultAcsClient client;
    public static void main(String[] args) throws Exception {
        String regionId = "cn-hangzhou"; // region Id
        IClientProfile profile = DefaultProfile.getProfile(regionId, "{ak}", "{secret}");
        DefaultProfile.addEndpoint(regionId, regionId, "Opensearch", "opensearch." + regionId + ".aliyuncs.com");
        DefaultAcsClient client = new DefaultAcsClient(profile);
        String dictionaryName = "词典、qp名称"; // 要创建的词典qp名称
        String appName = "应用名称"; // 要使用的应用名称
        int versionId = 1234;  // 应用版本id
//        System.out.println("List intervention dictionaries");
//        listInterventionDictionaries();
        Thread.sleep(10000);
        System.out.println("Create intervention dictionary: " + dictionaryName);
        createDictionary(dictionaryName);
//
//        Thread.sleep(10000);
//        System.out.println("Describe intervention dictionary");
//        describeInterventionDictionary(dictionaryName);
//
//        Thread.sleep(10000);
//        System.out.println("List intervention dictionary entries before");
//        listEntries(dictionaryName);
        Thread.sleep(10000);
        System.out.println("Post dictionary entries added");
        postEntries(dictionaryName, "add");
        Thread.sleep(10000);
        System.out.println("List intervention dictionary entries after add");
        listEntries(dictionaryName);
        Thread.sleep(10000);
        System.out.print("Set intervention dictionary to qp");
        setQueryProcessor(appName, versionId, dictionaryName);
        // 谨慎操作,确定在搜索测试页中测试当前qp 满足预期了以后才设置为默认qp
        Thread.sleep(10000);
        System.out.println("Set default query processor");
        setDefaultQueryProcessor(appName, versionId, dictionaryName);
//        Thread.sleep(10000);
//        System.out.println("Delete dictionary");
//        deleteDictionary(dictionaryName);
    }
    public static void listInterventionDictionaries() throws ClientException {
        ListInterventionDictionariesRequest listInterventionDictionariesRequest = new ListInterventionDictionariesRequest();
        listInterventionDictionariesRequest.setPageSize(50);
        HttpResponse response = client.doAction(listInterventionDictionariesRequest);
        System.out.println(response.getHttpContentString());
    }
    public static void createDictionary(String dictionaryName) throws UnsupportedEncodingException, ClientException {
        CreateInterventionDictionaryRequest request = new CreateInterventionDictionaryRequest();
        String body = "{\"name\": \"" + dictionaryName + "\", \"type\": \"term_weighting\"}";
        request.setHttpContent(body.getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void describeInterventionDictionary(String dictionaryName) throws ClientException {
        DescribeInterventionDictionaryRequest request = new DescribeInterventionDictionaryRequest();
        request.setName(dictionaryName);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void listEntries(String dictionaryName) throws ClientException {
        ListInterventionDictionaryEntriesRequest request = new ListInterventionDictionaryEntriesRequest();
        request.setName(dictionaryName);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void postEntries(String dictionaryName, String cmd) throws UnsupportedEncodingException, ClientException {
        PushInterventionDictionaryEntriesRequest request = new PushInterventionDictionaryEntriesRequest();
        request.setName(dictionaryName);
        // 修改为要干预的词条数据,权重高中低为 7 4 1
       //数据结构参考:https://help.aliyun.com/document_detail/173606.html?spm=a2c4g.11186623.6.727.28514cf4my0AzY
        String body = "[{\n" +
                "  \"word\": \"数据权限管理\",\n" +
                "  \"cmd\": \"" + cmd + "\",\n" +
                "  \"tokens\": [\n" +
                "    {\n" +
                "      \"token\": \"数据\",\n" +
                "      \"weight\": 7\n" +
                "    },\n" +
                "    {\n" +
                "      \"token\": \"权限\",\n" +
                "      \"weight\": 4\n" +
                "    },\n" +
                "    {\n" +
                "      \"token\": \"管理\",\n" +
                "      \"weight\": 1\n" +
                "    }\n" +
                "  ]\n" +
                "}]";
        request.setHttpContent(body.getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void deleteDictionary(String dictionaryName) throws ClientException {
        RemoveInterventionDictionaryRequest request = new RemoveInterventionDictionaryRequest();
        request.setName(dictionaryName);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void setQueryProcessor(String appName, int versionId, String dictionaryName) throws UnsupportedEncodingException, ClientException {
        CreateQueryProcessorRequest request = new CreateQueryProcessorRequest();
        request.setAppGroupIdentity(appName);
        request.setAppId(versionId);
        // query Processor 文档:https://help.aliyun.com/document_detail/170014.html?spm=a2c4g.11186623.6.719.1002401051A9Lq
        String body = "{\"name\":\""+  dictionaryName +"\",\"domain\":\"GENERAL\",\"indexes\":[\"default\"],\"processors\":[{\"name\":\"term_weighting\",\"useSystemDictionary\":true, \"interventionDictionary\":\""+dictionaryName+"\"}]}";
        request.setHttpContent(body.getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void setDefaultQueryProcessor(String appName, int versionId, String dictionaryName) throws UnsupportedEncodingException, ClientException {
        ModifyQueryProcessorRequest request = new ModifyQueryProcessorRequest();
        request.setAppGroupIdentity(appName);
        request.setAppId(versionId);
        request.setName(dictionaryName);
        String body = "{\"active\":true}";
        request.setHttpContent(body.getBytes("UTF-8"), "UTF-8", FormatType.JSON);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void deleteQueryProcessor(String appName, int versionId, String dictionaryName) throws ClientException {
        RemoveQueryProcessorRequest request = new RemoveQueryProcessorRequest();
        request.setAppGroupIdentity(appName);
        request.setAppId(versionId);
        request.setName(dictionaryName);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
    public static void listQueryProcessors(String appName, int versionId) throws ClientException {
        ListQueryProcessorsRequest request = new ListQueryProcessorsRequest();
        request.setAppGroupIdentity(appName);
        request.setAppId(versionId);
        HttpResponse response = client.doAction(request);
        System.out.println(response.getHttpContentString());
    }
}