blugelabs / bluge

indexing library for Go
Apache License 2.0
1.88k stars 122 forks source link

How to get a single field value without iterating all of them #36

Closed michaeljs1990 closed 3 years ago

michaeljs1990 commented 3 years ago

From the documentation the shown way to get a field value is to use something like the following on a returned document.

        err = match.VisitStoredFields(func(field string, value []byte) bool {
            if field == "_id" {
                fmt.Printf("match: %s\n", string(value))
            }
            return true
        })

However in some cases I only want to pull a single value from the document. I tried a combination of doc.LoadDocumentValues and doc.DocValues however it doesn't seem that I can retrieve a single value from a given document at present. This is using the in memory index currently.

mschoch commented 3 years ago

So, the reasoning behind this is that all of the stored fields are encoded as a single compressed entry. The cost to retrieve a single value is essentially the cost to retrieve any value, thus the API takes the form that it does. This is similar to Lucene, which (last time I checked) had a similar API.

We could offer an API to access a single value, but depending on how it was used, it could be even more expensive.

michaeljs1990 commented 3 years ago

That makes more sense to me my only concern was that I was doing more work than needed to retrieve a single value.