绑定完请刷新页面
取消
刷新

分享好友

×
取消 复制
Riak Search功能使用
2022-05-24 14:30:45

要使用Riak的search功能需要很复杂的步骤。根据官方文档的描述,要使用search功能需要经过下面的步骤:
1. 创建一个自定义的schema。如果不创建,可以使用自定义的schema。官方推荐在实际产品中使用自定义的schema。
2. 创建一个index和某个schema关联。如果不指定特定的schema,则使用系统默认的schema。
3. 关联索引。
一种是直接给一个bucket type指定一个索引,这样所有属于这个bucket type的bucket都会使用这个索引。(官方推荐使用这种)
另一种是给每一个bucket指定一个单独的索引。
另外也可以给已有的bucket指定一个索引。如果已有的bucket在默认的bucket type下面,那么可以单独给这个bucket指定一个索引。

package com.dasudian.dsdb.demo;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;
import java.util.Map;
import java.util.Scanner;
import java.util.concurrent.ExecutionException;

import com.basho.riak.client.api.RiakClient;
import com.basho.riak.client.api.commands.kv.DeleteValue;
import com.basho.riak.client.api.commands.kv.FetchValue;
import com.basho.riak.client.api.commands.kv.StoreValue;
import com.basho.riak.client.api.commands.search.StoreIndex;
import com.basho.riak.client.api.commands.search.StoreSchema;
import com.basho.riak.client.core.RiakCluster;
import com.basho.riak.client.core.RiakFuture;
import com.basho.riak.client.core.RiakNode;
import com.basho.riak.client.core.operations.SearchOperation;
import com.basho.riak.client.core.query.Location;
import com.basho.riak.client.core.query.Namespace;
import com.basho.riak.client.core.query.RiakObject;
import com.basho.riak.client.core.query.search.YokozunaIndex;
import com.basho.riak.client.core.query.search.YokozunaSchema;
import com.basho.riak.client.core.util.BinaryValue;

public class Search {
private static String schemaName = "people_schema";
private static String index = "people_index";
private static String bucketType = "people_type";
private static String bucket = "people_bucket";
private static String key = "people_key";
private static String data = "{\"name\":\"Jack_Liu\",\"age\":24,\"leader\":false}";
private static String condtion = "name:Jack*";

public static void main(String[] args) throws Exception {
RiakNode node = new RiakNode.Builder().withRemoteAddress("192.168.1.28").withRemotePort(8087).build();
RiakCluster riakCluster = new RiakCluster.Builder(node).build();
riakCluster.start();
RiakClient riakClient = new RiakClient(riakCluster);
// 1.创建一个自定义的schema
// createSchema(riakClient);
// 2.创建一个索引和这个schema关联
// createIndex(riakClient);

// 3.在命令执行下面的两条命令,创建一个bucket type和这个index关联
// riak-admin bucket-type create people_type '{"props":{"search_index":"people_index"}}'
// riak-admin bucket-type activate people_type

store(riakClient);
get(riakClient);
// 4. 搜看有没有数据
search(riakCluster);

// delete(riakClient);

riakCluster.shutdown();
}

public static void createSchema(RiakClient client) throws FileNotFoundException, ExecutionException, InterruptedException {
File xml = new File(Search.class.getClassLoader().getResource("schema.xml").getFile());
String xmlString = new Scanner(xml).useDelimiter("\\Z").next();
YokozunaSchema schema = new YokozunaSchema(schemaName, xmlString);
StoreSchema storeSchemaOp = new StoreSchema.Builder(schema).build();
client.execute(storeSchemaOp);
}

public static void createIndex(RiakClient client) throws ExecutionException, InterruptedException {
YokozunaIndex famousIndex = new YokozunaIndex(index, schemaName);
StoreIndex storeIndex = new StoreIndex.Builder(famousIndex).build();
client.execute(storeIndex);
}

public static void store(RiakClient client) throws ExecutionException, InterruptedException {
Namespace ns = new Namespace(bucketType, bucket);
String json = "application/json";
RiakObject liono = new RiakObject().setContentType(json).setValue(BinaryValue.create(data));
Location lionoLoc = new Location(ns, key);

StoreValue lionoStore = new StoreValue.Builder(liono).withLocation(lionoLoc).build();
client.execute(lionoStore);
}

public static void get(RiakClient riakClient) throws ExecutionException, InterruptedException {
Namespace ns = new Namespace(bucketType, bucket);
Location location = new Location(ns, key);
FetchValue fv = new FetchValue.Builder(location).build();
FetchValue.Response response = riakClient.execute(fv);
for (RiakObject object : response.getValues()) {
System.out.println(object.getValue());
}
}

public static void search(RiakCluster riakCluster) throws InterruptedException {
SearchOperation searchOp = new SearchOperation.Builder(BinaryValue.create(index), condtion).build();
RiakFuture<com.basho.riak.client.core.operations.SearchOperation.Response, BinaryValue> execute = riakCluster
.execute(searchOp);
execute.await();
if (execute.isDone()) {
if (execute.isSuccess()) {
List<Map<String, List<String>>> results = searchOp.getNow().getAllResults();
System.out.println(results);
} else {
System.out.println(execute.cause());
}
}
}

public static void delete(RiakClient riakClient) throws ExecutionException, InterruptedException {
Namespace ns = new Namespace(bucketType, bucket);
Location location = new Location(ns, key);
DeleteValue fv = new DeleteValue.Builder(location).build();
riakClient.execute(fv);
}
}

schema文件内容

<?xml version="1.0" encoding="UTF-8" ?>
<schema name="people" version="1.5">
<fields>
<!-- 添加自己的index规则。下面3行是自己根据数据的内容来创建的 -->
<field name="name" type="string" indexed="true" stored="true" />
<field name="age" type="int" indexed="true" stored="false" />
<field name="leader" type="boolean" indexed="true" stored="false" />

<!-- All of these fields are required by Riak Search -->
<field name="_yz_id" type="_yz_str" indexed="true" stored="true" multiValued="false" required="true"/>
<field name="_yz_ed" type="_yz_str" indexed="true" stored="false" multiValued="false"/>
<field name="_yz_pn" type="_yz_str" indexed="true" stored="false" multiValued="false"/>
<field name="_yz_fpn" type="_yz_str" indexed="true" stored="false" multiValued="false"/>
<field name="_yz_vtag" type="_yz_str" indexed="true" stored="false" multiValued="false"/>
<field name="_yz_rk" type="_yz_str" indexed="true" stored="true" multiValued="false"/>
<field name="_yz_rt" type="_yz_str" indexed="true" stored="true" multiValued="false"/>
<field name="_yz_rb" type="_yz_str" indexed="true" stored="true" multiValued="false"/>
<field name="_yz_err" type="_yz_str" indexed="true" stored="false" multiValued="false"/>

<dynamicField name="*" type="ignored" />
</fields>


<uniqueKey>_yz_id</uniqueKey>

<*>
<!-- YZ String: Used for non-analyzed fields -->
<fieldType name="_yz_str" class="solr.StrField" sortMissingLast="true" />

<!-- 添加自己的类型 -->
<fieldType name="string" class="solr.StrField" sortMissingLast="true" />
<fieldType name="boolean" class="solr.BoolField" sortMissingLast="true"/>
<fieldType name="int" class="solr.TrieIntField" precisionStep="0" positionIncrementGap="0"/>
<fieldtype name="ignored" stored="false" indexed="false" multiValued="true" class="solr.StrField" />
</*>
</schema>
————————————————
版权声明:本文为CSDN博主「a987860319」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/it_liuwei/article/details/55252395

分享好友

分享这个小栈给你的朋友们,一起进步吧。

Riak KV
创建时间:2022-04-19 09:50:17
Riak KV
展开
订阅须知

• 所有用户可根据关注领域订阅专区或所有专区

• 付费订阅:虚拟交易,一经交易不退款;若特殊情况,可3日内客服咨询

• 专区发布评论属默认订阅所评论专区(除付费小栈外)

技术专家

查看更多
  • 飘絮絮絮丶
    专家
戳我,来吐槽~