目录

应用开发

开发应用时,需要先创建索引域,再根据索引域创建索引条目,最后更新索引数据。

应用开发示例

  1. 创建SearchableItemAttributeSet,并设置属性值。

    // 使用前先判断
    if(!SearchClientUtils.isSupportHwSearchService(context)){
        //  当前系统版本不支持数据捐赠特性
        return;
    }
    SearchServiceAbility searchServiceAbility = new SearchServiceAbility (context);
    // 连接服务 失败 需要尝试重连 成功继续业务逻辑
    searchServiceAbility.connect(new ServiceConnectCallback() {
        @Override
        public void onConnect() {
            // TODO
        }
     
        @Override
        public void onDisconnect() {
        }
    });
     
    //可选:如果需要设置自定义属性,需要增加如下3行
    IndexForm customIndexForm = new IndexForm("customeIndexForm1", IndexType.NO_ANALYZED, false, true, false); //
    // 初始化indexFormList
    List<IndexForm> indexFormList = searchServiceAbility.initWithItemContentType (“content”);
    indexFormList.add(customeIndexForm);
    // 查询indexForm信息,判断是否已经设置,
    // 返回值大于0表示已设置IndexForm ,是否需要重新设置取决于业务是否需要新增自定义的字段
    // 返回值小于0 需要业务方重新设置IndexForm且需要重新插入索引数据
    int indexFormVersion = searchServiceAbility.getIndexFormVersion(“pkgNameOfYourAPP”);
    if(indexFormVersion < 0){
    
    // 设置索引格式
    int result = searchServiceAbility.setIndexForm(“pkgNameOfYourAPP”, 1, indexFormList ); // 
    if (result == SearchErrorCode.SUCCESS) {
        Log.i(TAG, "searchServiceAbility.setIndexForm SUCCESS");
    } else {
        Log.i(TAG, "searchServiceAbility.setIndexForm FAIL");
        return;
    }
    }

  2. 创建IndexData。

    SearchableItemAttributeSet data = searchServiceAbility.initWithUniqueIdentifier ("测试uniqueIdentifier ",SearchParameter.DEFAULT_GROUP)  // groupId 默认为“defaultGroup”
    data. displayName="测试displayName";
    data.setCustomValues ("100", customeIndexForm);

  3. 创建索引。调用insert或者update方法时,入参的List中IndexData数量上限是100。如果变更的索引数据很多,可以多次调用insert或者update方法。

    List<IndexData> indexDataList= new ArrayList()<>;
    indexDataList.add(data);
    List<IndexData> failedList = searchServiceAbility.insert(SearchParameter.DEFAULT_GROUP, “pkgNameOfYourAPP”, indexDataList);