文档

词权重干预词典

更新时间:

使用介绍

词权重又称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

  1. 在新建的词权重干预词典里新增干预词条,输入query,分析器类型选择系统内置分析器/自定分析器(根据查询分析中配置的分析器类型选择),设置分词后term的权重:

001若查询分析中配置的索引为定制模型分析器,则分析器类型选择定制模型分析器,实例选择Ha3引擎,并选择对应的应用名称,关联分析器与查询分析中配置的分析器保持一致:

002

  1. 在查询分析界面把刚刚创建的“词权重干预词典”先应用在一个未上线的查询分析规则中,以便进行搜索效果测试:

888888888888888888

重要

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

  1. 搜索测试是否符合预期效果,将“数据”的权重提高: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
        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);
        
        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());
    }
}
说明

数据结构参考:InterventionDictionaryEntry

QueryProcessor body参考:QueryProcessor

功能限制

  • 词权重干预词典一共能创建20个。

  • 每个词权重干预词典最多创建500个干预词条。

  • query中若包含英文,请使用全小写英文。

  • 每个干预词条下query最大分词term为30个。

  • 本页导读 (0)
文档反馈