You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.2 KiB

  1. // Copyright (c) 2019 Couchbase, Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bleve
  15. import (
  16. "encoding/json"
  17. "fmt"
  18. "github.com/blevesearch/bleve/document"
  19. "github.com/blevesearch/bleve/index"
  20. "github.com/blevesearch/bleve/index/scorch"
  21. "github.com/blevesearch/bleve/mapping"
  22. )
  23. type builderImpl struct {
  24. b index.IndexBuilder
  25. m mapping.IndexMapping
  26. }
  27. func (b *builderImpl) Index(id string, data interface{}) error {
  28. if id == "" {
  29. return ErrorEmptyID
  30. }
  31. doc := document.NewDocument(id)
  32. err := b.m.MapDocument(doc, data)
  33. if err != nil {
  34. return err
  35. }
  36. err = b.b.Index(doc)
  37. return err
  38. }
  39. func (b *builderImpl) Close() error {
  40. return b.b.Close()
  41. }
  42. func newBuilder(path string, mapping mapping.IndexMapping, config map[string]interface{}) (Builder, error) {
  43. if path == "" {
  44. return nil, fmt.Errorf("builder requires path")
  45. }
  46. err := mapping.Validate()
  47. if err != nil {
  48. return nil, err
  49. }
  50. if config == nil {
  51. config = map[string]interface{}{}
  52. }
  53. // the builder does not have an API to interact with internal storage
  54. // however we can pass k/v pairs through the config
  55. mappingBytes, err := json.Marshal(mapping)
  56. if err != nil {
  57. return nil, err
  58. }
  59. config["internal"] = map[string][]byte{
  60. string(mappingInternalKey): mappingBytes,
  61. }
  62. // do not use real config, as these are options for the builder,
  63. // not the resulting index
  64. meta := newIndexMeta(scorch.Name, scorch.Name, map[string]interface{}{})
  65. err = meta.Save(path)
  66. if err != nil {
  67. return nil, err
  68. }
  69. config["path"] = indexStorePath(path)
  70. b, err := scorch.NewBuilder(config)
  71. if err != nil {
  72. return nil, err
  73. }
  74. rv := &builderImpl{
  75. b: b,
  76. m: mapping,
  77. }
  78. return rv, nil
  79. }