在Riak中按属性查询

时间:2019-05-27 12:08:42

标签: java nosql riak

我正在使用Riak和Java开发应用程序。基本上,我想存储新闻,为此我有一个具有以下属性的对象:

public String title;
public String author;
public String URL;
public ArrayList<String> categories;
public String description;
public String release;

它正常工作,但现在我想允许用户通过关键字搜索新闻。

问题是我只能通过主键在Java客户端文档中找到查询,这是这样完成的:

    RiakClient client = RiakClient.newClient(10017, "127.0.0.1");
    Location location = new Location(new 
    Namespace("TestBucket"),"TestKey");

    FetchValue fv = new FetchValue.Builder(location).build();
    FetchValue.Response response = client.execute(fv);

    // Fetch object as String
    String value = response.getValue(String.class);
    System.out.println(value);

    client.shutdown();

是否可以通过属性查询?例如,您可以搜索标题中是否有单词吗?

因为现在我看到的唯一选择是从数据库中获取所有对象并手动搜索,这对我来说似乎效率很低。

1 个答案:

答案 0 :(得分:0)

是的,有两种通过属性查询事物的方法,即使用二级索引(所谓的2i)或使用Riak Search。我建议您从使用二级索引开始,这很容易。基本上,在编写数据时,需要确定要为哪个属性建立索引。然后,您可以查询这些索引。它们可以是数字或字母数字,并且您可以查询范围。

请参见https://docs.riak.com/riak/kv/latest/using/reference/secondary-indexes/index.html 以及将2i与HTTP API结合使用的示例:https://docs.riak.com/riak/kv/latest/developing/api/http/secondary-indexes/index.html

查看您所使用的Java客户端的文档。