使用BoltDB开始使用Bleve

huangapple go评论119阅读模式
英文:

Getting started with Bleve using BoltDB

问题

我正在努力理解Bleve,我理解教程、视频和文档中的所有内容。然而,当我在BoltDB上使用它时,我感到非常困惑,不知道如何开始。

假设我有一个名为data.db的现有BoltDB数据库,其中存储了结构类型为Person的值。

type Person struct {
   ID int          `json:"id"`             
   Name string     `json:"name"` 
   Age int         `json:"age"`
   Sex string      `json:"sex"`
}

我应该如何对这些数据进行索引以便进行搜索?我应该如何处理将来存储在数据库中的数据的索引?

非常感谢您的帮助。

英文:

I am trying to wrap my head around Bleve and I understand everything that is going on in the tutorials, videos and documentation. I however get very confused when I am using it on BoltDB and don't know how to start.

Say I have an existing BoltDB database called data.db populated with values of struct type Person

type Person struct {
   ID int          `json:"id"`             
   Name string     `json:"name"` 
   Age int         `json:"age"`
   Sex string      `json:"sex"`
}

How do I index this data so that I can do a search? How do I handle the indexing of data that will be stored in the database in the future?

Any help will be highly appreciated.

答案1

得分: 8

Bleve使用BoltDB作为多个后端存储之一,与您存储应用程序数据的位置是分开的。要在Bleve中索引数据,只需添加您的Index

index.Index(person.ID, person)

index与您的应用程序数据(无论是在Bolt、Postgres等中)是分开的。

要检索数据,您需要使用bleve.NewSearchRequest()构建一个搜索请求,然后调用Index.Search()。这将返回一个SearchResult,其中包含一个Hits字段,您可以从中检索对象的ID。您可以使用这个ID在应用程序数据存储中查找对象。

免责声明:我是BoltDB的作者。

英文:

Bleve uses BoltDB as one of several backend stores and is separate from where you store your application data. To index your data in Bleve, simply add your Index:

index.Index(person.ID, person)

That index exists separately from your application data (whether it's in Bolt, Postgres, etc).

To retrieve your data, you'll need to construct a search request using bleve.NewSearchRequest(), then call Index.Search(). This will return a SearchResult which includes a Hits field where you can retrieve the ID for your object. You can use this to look up the object in your application data store.

Disclaimer: I am the author of BoltDB.

答案2

得分: 2

你如何索引数据取决于你想要如何查询它。

如果你想要通过任意字段进行查询,比如{Age:15, Name:"Bob"},那么BoltDB可能不是最适合你的问题。

BoltDB只是一个键值存储,可以快速访问顺序键和高效地进行前缀搜索。它并不真正替代通用数据库。

你可能更需要像文档存储(如MongoDB)或关系型数据库(如PostgreSQL)这样的东西。

如果你只想要使用简单的文件并且是嵌入式的,你也可以使用SQlite和Go模块。

如果你只想通过单个字段进行搜索,比如ID或Name,那么可以将其作为键。

如果查找速度完全不重要,我猜你可以使用Bolt来遍历整个数据库,解析JSON并检查字段。但那可能是你可以采取的最糟糕的方法。

英文:

How you index your data depends on how you want to query for it.

If you want to query by any arbitrary fields, like {Age:15, Name:"Bob"} then BoltDB isn't an awesome fit for your problem.

BoltDB is simply a key value store with fast access to sequential keys and efficient prefix seeking. It's not really a replacement for general use databases.

You likely want something more like a document store (ie: MongoDB) or RDBMS (ie: PostgreSQL).

If you just wanted something that uses simple files and is embedded, you could also use SQlite with the Go module

If you want to search by only a single field, like ID or Name, then use that as the key.

If lookup speed doesn't matter at all, I guess you can use Bolt to just iterate over the entire db, parse the json and check the fields. But that's probably the worst approach you could take.

huangapple
  • 本文由 发表于 2016年1月24日 14:55:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/34973055.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定