Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow exporting a bleve index to []byte #46

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

peterwilliams97
Copy link

@peterwilliams97 peterwilliams97 commented Oct 8, 2019

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