Skip to content

Allow exporting a bleve index to []byte - #46

Open
peterwilliams97 wants to merge 6 commits into
blevesearch:masterfrom
peterwilliams97:no.go.mod
Open

Allow exporting a bleve index to []byte#46
peterwilliams97 wants to merge 6 commits into
blevesearch:masterfrom
peterwilliams97:no.go.mod

Conversation

@peterwilliams97

@peterwilliams97 peterwilliams97 commented Oct 8, 2019

Copy link
Copy Markdown

I need to serialize/deserialize bleve indexes to/from []byte. This PR is the best way I can see how to do so based on my reading of your code. If there is a better way, please tell me about it and ignore this PR.

The calling code that uses the proposed code change in this PR looks like this.

import (
      btreap "github.com/blevesearch/bleve/index/store/gtreap"
     "github.com/blevesearch/blevex/preload"
     "github.com/blevesearch/bleve"
)

// ExportBleveMem serializes `index` to a byte slice.
func ExportBleveMem(index bleve.Index) ([]byte, error) {
	var b bytes.Buffer
	w := &b
	i, _, err := index.Advanced()
	if err != nil {
		return nil, err
	}
	if err = preload.ExportBleve(i, w); err != nil {
		return nil, err
	}
	return b.Bytes(), nil
}

// ImportBleveMem deserializes `data` to a byte bleve.Index.
func ImportBleveMem(data []byte) (bleve.Index, error) {
	return bleve.NewUsing(
		"",
		bleve.NewIndexMapping(),
		bleve.Config.DefaultIndexType,
		preload.Name,
		map[string]interface{}{
			"kvStoreName_actual": btreap.Name,
			"preloadmem":         data,
		})
}

The code that uses this is in https://github.com/PaperCutSoftware/pdfsearch

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant