-
Notifications
You must be signed in to change notification settings - Fork 668
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce new scorch index builder (#1282)
The purpose of the index builder is to better support use cases where one would like to index a set of data, not search it during the build, ensure the resulting file is in an efficient optimized state, and then support opening this index read-only to serve queries. Useful implementation details: Only an Index() method is offered, meaning you do not control the batch size. The builder will group data into batches using a configurable 'batchSize' parameter (default 1000) All of these batches are persisted as segments into a temp location. You can control this using 'buildPathPrefix', or it will default to a system temp location. You must call Close() after indexing all data. This will flush the last batch, and begin merging segments. The builder will attempt to merge several segments at once. This is configurable with the 'mergeMax' setting, which defaults to 10. Merging continues until there is only 1 segment remaining. At this point, the final segment is moved to the final location (as specified by the original builder constructor). The root.bolt is created, pointing to the segment, and the remaining bleve index wrapping is completed.
- Loading branch information
Showing
9 changed files
with
780 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright (c) 2019 Couchbase, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bleve | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
|
||
"github.com/blevesearch/bleve/document" | ||
"github.com/blevesearch/bleve/index" | ||
"github.com/blevesearch/bleve/index/scorch" | ||
"github.com/blevesearch/bleve/mapping" | ||
) | ||
|
||
type builderImpl struct { | ||
b index.IndexBuilder | ||
m mapping.IndexMapping | ||
} | ||
|
||
func (b *builderImpl) Index(id string, data interface{}) error { | ||
if id == "" { | ||
return ErrorEmptyID | ||
} | ||
|
||
doc := document.NewDocument(id) | ||
err := b.m.MapDocument(doc, data) | ||
if err != nil { | ||
return err | ||
} | ||
err = b.b.Index(doc) | ||
return err | ||
} | ||
|
||
func (b *builderImpl) Close() error { | ||
return b.b.Close() | ||
} | ||
|
||
func newBuilder(path string, mapping mapping.IndexMapping, config map[string]interface{}) (Builder, error) { | ||
if path == "" { | ||
return nil, fmt.Errorf("builder requires path") | ||
} | ||
|
||
err := mapping.Validate() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
if config == nil { | ||
config = map[string]interface{}{} | ||
} | ||
|
||
// the builder does not have an API to interact with internal storage | ||
// however we can pass k/v pairs through the config | ||
mappingBytes, err := json.Marshal(mapping) | ||
if err != nil { | ||
return nil, err | ||
} | ||
config["internal"] = map[string][]byte{ | ||
string(mappingInternalKey): mappingBytes, | ||
} | ||
|
||
// do not use real config, as these are options for the builder, | ||
// not the resulting index | ||
meta := newIndexMeta(scorch.Name, scorch.Name, map[string]interface{}{}) | ||
err = meta.Save(path) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
config["path"] = indexStorePath(path) | ||
|
||
b, err := scorch.NewBuilder(config) | ||
if err != nil { | ||
return nil, err | ||
} | ||
rv := &builderImpl{ | ||
b: b, | ||
m: mapping, | ||
} | ||
|
||
return rv, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
// Copyright (c) 2019 Couchbase, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package bleve | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestBuilder(t *testing.T) { | ||
tmpDir, err := ioutil.TempDir("", "bleve-scorch-builder-test") | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
defer func() { | ||
err = os.RemoveAll(tmpDir) | ||
if err != nil { | ||
t.Fatalf("error cleaning up test index") | ||
} | ||
}() | ||
|
||
conf := map[string]interface{}{ | ||
"batchSize": 2, | ||
"mergeMax": 2, | ||
} | ||
b, err := NewBuilder(tmpDir, NewIndexMapping(), conf) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
for i := 0; i < 10; i++ { | ||
doc := map[string]interface{}{ | ||
"name": "hello", | ||
} | ||
err = b.Index(fmt.Sprintf("%d", i), doc) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
} | ||
|
||
err = b.Close() | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
idx, err := Open(tmpDir) | ||
if err != nil { | ||
t.Fatalf("error opening index: %v", err) | ||
} | ||
defer func() { | ||
err = idx.Close() | ||
if err != nil { | ||
t.Errorf("error closing index: %v", err) | ||
} | ||
}() | ||
|
||
docCount, err := idx.DocCount() | ||
if err != nil { | ||
t.Errorf("error checking doc count: %v", err) | ||
} | ||
if docCount != 10 { | ||
t.Errorf("expected doc count to be 10, got %d", docCount) | ||
} | ||
|
||
q := NewTermQuery("hello") | ||
q.SetField("name") | ||
req := NewSearchRequest(q) | ||
res, err := idx.Search(req) | ||
if err != nil { | ||
t.Errorf("error searching index: %v", err) | ||
} | ||
if res.Total != 10 { | ||
t.Errorf("expected 10 search hits, got %d", res.Total) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.