Skip to content

Commit

Permalink
refactor mapping to inteface and move into separate package
Browse files Browse the repository at this point in the history
the index mapping contains some relatively messy logic
and the top-level bleve package only cares about a relatively
small portion of this
the motivation for this change is to codify the part that the
top-level bleve package cares about into an interface
then move all the details into its own package

NOTE: the top-level bleve package still has hard dependency on
the actual implementation (for now) because it must deserialize
mappings from JSON and simply assumes it is this one instance.
this is seen as OK for now, and this issue could be revisited
in a future change.  moving the logic into a separate package
is seen as a simplification of top-level bleve, even though
we still depend on the one particular implementation.
  • Loading branch information
mschoch committed Sep 29, 2016
1 parent 97393d0 commit 79cc39a
Show file tree
Hide file tree
Showing 37 changed files with 348 additions and 212 deletions.
7 changes: 4 additions & 3 deletions examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ import (
"testing"
"time"

"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/highlight/highlighters/ansi"
)

var mapping *IndexMapping
var indexMapping mapping.IndexMapping
var example_index Index
var err error

Expand All @@ -43,8 +44,8 @@ func TestMain(m *testing.M) {
}

func ExampleNew() {
mapping = NewIndexMapping()
example_index, err = New("path_to_index", mapping)
indexMapping = NewIndexMapping()
example_index, err = New("path_to_index", indexMapping)
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions http/index_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"fmt"
"net/http"

"github.com/blevesearch/bleve"
"github.com/blevesearch/bleve/mapping"
)

type GetIndexHandler struct {
Expand Down Expand Up @@ -42,9 +42,9 @@ func (h *GetIndexHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
}

rv := struct {
Status string `json:"status"`
Name string `json:"name"`
Mapping *bleve.IndexMapping `json:"mapping"`
Status string `json:"status"`
Name string `json:"name"`
Mapping mapping.IndexMapping `json:"mapping"`
}{
Status: "ok",
Name: indexName,
Expand Down
22 changes: 6 additions & 16 deletions index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/mapping"
"golang.org/x/net/context"
)

Expand All @@ -35,7 +36,7 @@ func (b *Batch) Index(id string, data interface{}) error {
return ErrorEmptyID
}
doc := document.NewDocument(id)
err := b.index.Mapping().mapDocument(doc, data)
err := b.index.Mapping().MapDocument(doc, data)
if err != nil {
return err
}
Expand Down Expand Up @@ -92,11 +93,6 @@ func (b *Batch) Reset() {
// assigns string paths to its fields or values then applies field mappings on
// them.
//
// If the value is a []byte, the indexer attempts to convert it to something
// else using the ByteArrayConverter registered as
// IndexMapping.ByteArrayConverter. By default, it interprets the value as a
// JSON payload and unmarshals it to map[string]interface{}.
//
// The DocumentMapping used to index a value is deduced by the following rules:
// 1) If value implements Classifier interface, resolve the mapping from Type().
// 2) If value has a string field or value at IndexMapping.TypeField.
Expand Down Expand Up @@ -178,7 +174,7 @@ type Index interface {

Close() error

Mapping() *IndexMapping
Mapping() mapping.IndexMapping

Stats() *IndexStat
StatsMap() map[string]interface{}
Expand All @@ -197,16 +193,10 @@ type Index interface {
Advanced() (index.Index, store.KVStore, error)
}

// A Classifier is an interface describing any object
// which knows how to identify its own type.
type Classifier interface {
Type() string
}

// New index at the specified path, must not exist.
// The provided mapping will be used for all
// Index/Search operations.
func New(path string, mapping *IndexMapping) (Index, error) {
func New(path string, mapping mapping.IndexMapping) (Index, error) {
return newIndexUsing(path, mapping, Config.DefaultIndexType, Config.DefaultKVStore, nil)
}

Expand All @@ -215,7 +205,7 @@ func New(path string, mapping *IndexMapping) (Index, error) {
// and will be lost once closed.
// The provided mapping will be used for all
// Index/Search operations.
func NewMemOnly(mapping *IndexMapping) (Index, error) {
func NewMemOnly(mapping mapping.IndexMapping) (Index, error) {
return newIndexUsing("", mapping, Config.DefaultIndexType, Config.DefaultMemKVStore, nil)
}

Expand All @@ -227,7 +217,7 @@ func NewMemOnly(mapping *IndexMapping) (Index, error) {
// The specified kvstore implementation will be used
// and the provided kvconfig will be passed to its
// constructor.
func NewUsing(path string, mapping *IndexMapping, indexType string, kvstore string, kvconfig map[string]interface{}) (Index, error) {
func NewUsing(path string, mapping mapping.IndexMapping, indexType string, kvstore string, kvconfig map[string]interface{}) (Index, error) {
return newIndexUsing(path, mapping, indexType, kvstore, kvconfig)
}

Expand Down
3 changes: 2 additions & 1 deletion index_alias_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/search"
)

Expand Down Expand Up @@ -259,7 +260,7 @@ func (i *indexAliasImpl) Close() error {
return nil
}

func (i *indexAliasImpl) Mapping() *IndexMapping {
func (i *indexAliasImpl) Mapping() mapping.IndexMapping {
i.mutex.RLock()
defer i.mutex.RUnlock()

Expand Down
3 changes: 2 additions & 1 deletion index_alias_impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/blevesearch/bleve/document"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/numeric_util"
"github.com/blevesearch/bleve/search"
)
Expand Down Expand Up @@ -1269,7 +1270,7 @@ func (i *stubIndex) Close() error {
return i.err
}

func (i *stubIndex) Mapping() *IndexMapping {
func (i *stubIndex) Mapping() mapping.IndexMapping {
return nil
}

Expand Down
15 changes: 8 additions & 7 deletions index_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store"
"github.com/blevesearch/bleve/index/upside_down"
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/registry"
"github.com/blevesearch/bleve/search"
"github.com/blevesearch/bleve/search/collectors"
Expand All @@ -35,7 +36,7 @@ type indexImpl struct {
name string
meta *indexMeta
i index.Index
m *IndexMapping
m mapping.IndexMapping
mutex sync.RWMutex
open bool
stats *IndexStat
Expand All @@ -49,7 +50,7 @@ func indexStorePath(path string) string {
return path + string(os.PathSeparator) + storePath
}

func newIndexUsing(path string, mapping *IndexMapping, indexType string, kvstore string, kvconfig map[string]interface{}) (*indexImpl, error) {
func newIndexUsing(path string, mapping mapping.IndexMapping, indexType string, kvstore string, kvconfig map[string]interface{}) (*indexImpl, error) {
// first validate the mapping
err := mapping.Validate()
if err != nil {
Expand Down Expand Up @@ -183,7 +184,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
return nil, err
}

var im IndexMapping
var im *mapping.IndexMappingImpl
err = json.Unmarshal(mappingBytes, &im)
if err != nil {
return nil, fmt.Errorf("error parsing mapping JSON: %v\nmapping contents:\n%s", err, string(mappingBytes))
Expand All @@ -202,7 +203,7 @@ func openIndexUsing(path string, runtimeConfig map[string]interface{}) (rv *inde
return rv, err
}

rv.m = &im
rv.m = im
indexStats.Register(rv)
return rv, err
}
Expand All @@ -219,7 +220,7 @@ func (i *indexImpl) Advanced() (index.Index, store.KVStore, error) {

// Mapping returns the IndexMapping in use by this
// Index.
func (i *indexImpl) Mapping() *IndexMapping {
func (i *indexImpl) Mapping() mapping.IndexMapping {
return i.m
}

Expand All @@ -239,7 +240,7 @@ func (i *indexImpl) Index(id string, data interface{}) (err error) {
}

doc := document.NewDocument(id)
err = i.m.mapDocument(doc, data)
err = i.m.MapDocument(doc, data)
if err != nil {
return
}
Expand Down Expand Up @@ -387,7 +388,7 @@ func (i *indexImpl) SearchInContext(ctx context.Context, req *SearchRequest) (sr
} else if facetRequest.DateTimeRanges != nil {
// build date range facet
facetBuilder := facets.NewDateTimeFacetBuilder(facetRequest.Field, facetRequest.Size)
dateTimeParser := i.m.dateTimeParserNamed(i.m.DefaultDateTimeParser)
dateTimeParser := i.m.DateTimeParserNamed("")
for _, dr := range facetRequest.DateTimeRanges {
dr.ParseDates(dateTimeParser)
facetBuilder.AddRange(dr.Name, dr.Start, dr.End)
Expand Down
3 changes: 2 additions & 1 deletion index_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/blevesearch/bleve/analysis/analyzers/keyword_analyzer"
"github.com/blevesearch/bleve/index"
"github.com/blevesearch/bleve/index/store/null"
"github.com/blevesearch/bleve/mapping"
"github.com/blevesearch/bleve/search"
)

Expand Down Expand Up @@ -365,7 +366,7 @@ func (s *slowQuery) SetField(f string) Query {
return s.actual.SetField(f)
}

func (s *slowQuery) Searcher(i index.IndexReader, m *IndexMapping, explain bool) (search.Searcher, error) {
func (s *slowQuery) Searcher(i index.IndexReader, m mapping.IndexMapping, explain bool) (search.Searcher, error) {
time.Sleep(s.delay)
return s.actual.Searcher(i, m, explain)
}
Expand Down
56 changes: 56 additions & 0 deletions mapping.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (c) 2014 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 "github.com/blevesearch/bleve/mapping"

// NewIndexMapping creates a new IndexMapping that will use all the default indexing rules
func NewIndexMapping() *mapping.IndexMappingImpl {
return mapping.NewIndexMapping()
}

// NewDocumentMapping returns a new document mapping
// with all the default values.
func NewDocumentMapping() *mapping.DocumentMapping {
return mapping.NewDocumentMapping()
}

// NewDocumentStaticMapping returns a new document
// mapping that will not automatically index parts
// of a document without an explicit mapping.
func NewDocumentStaticMapping() *mapping.DocumentMapping {
return mapping.NewDocumentStaticMapping()
}

// NewDocumentDisabledMapping returns a new document
// mapping that will not perform any indexing.
func NewDocumentDisabledMapping() *mapping.DocumentMapping {
return mapping.NewDocumentDisabledMapping()
}

// NewTextFieldMapping returns a default field mapping for text
func NewTextFieldMapping() *mapping.FieldMapping {
return mapping.NewTextFieldMapping()
}

// NewNumericFieldMapping returns a default field mapping for numbers
func NewNumericFieldMapping() *mapping.FieldMapping {
return mapping.NewNumericFieldMapping()
}

// NewDateTimeFieldMapping returns a default field mapping for dates
func NewDateTimeFieldMapping() *mapping.FieldMapping {
return mapping.NewDateTimeFieldMapping()
}

// NewBooleanFieldMapping returns a default field mapping for booleans
func NewBooleanFieldMapping() *mapping.FieldMapping {
return mapping.NewBooleanFieldMapping()
}
94 changes: 94 additions & 0 deletions mapping/analysis.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// Copyright (c) 2014 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 mapping

type customAnalysis struct {
CharFilters map[string]map[string]interface{} `json:"char_filters,omitempty"`
Tokenizers map[string]map[string]interface{} `json:"tokenizers,omitempty"`
TokenMaps map[string]map[string]interface{} `json:"token_maps,omitempty"`
TokenFilters map[string]map[string]interface{} `json:"token_filters,omitempty"`
Analyzers map[string]map[string]interface{} `json:"analyzers,omitempty"`
DateTimeParsers map[string]map[string]interface{} `json:"date_time_parsers,omitempty"`
}

func (c *customAnalysis) registerAll(i *IndexMappingImpl) error {
for name, config := range c.CharFilters {
_, err := i.cache.DefineCharFilter(name, config)
if err != nil {
return err
}
}

if len(c.Tokenizers) > 0 {
// put all the names in map tracking work to do
todo := map[string]struct{}{}
for name := range c.Tokenizers {
todo[name] = struct{}{}
}
registered := 1
errs := []error{}
// as long as we keep making progress, keep going
for len(todo) > 0 && registered > 0 {
registered = 0
errs = []error{}
for name := range todo {
config := c.Tokenizers[name]
_, err := i.cache.DefineTokenizer(name, config)
if err != nil {
errs = append(errs, err)
} else {
delete(todo, name)
registered++
}
}
}

if len(errs) > 0 {
return errs[0]
}
}
for name, config := range c.TokenMaps {
_, err := i.cache.DefineTokenMap(name, config)
if err != nil {
return err
}
}
for name, config := range c.TokenFilters {
_, err := i.cache.DefineTokenFilter(name, config)
if err != nil {
return err
}
}
for name, config := range c.Analyzers {
_, err := i.cache.DefineAnalyzer(name, config)
if err != nil {
return err
}
}
for name, config := range c.DateTimeParsers {
_, err := i.cache.DefineDateTimeParser(name, config)
if err != nil {
return err
}
}
return nil
}

func newCustomAnalysis() *customAnalysis {
rv := customAnalysis{
CharFilters: make(map[string]map[string]interface{}),
Tokenizers: make(map[string]map[string]interface{}),
TokenMaps: make(map[string]map[string]interface{}),
TokenFilters: make(map[string]map[string]interface{}),
Analyzers: make(map[string]map[string]interface{}),
DateTimeParsers: make(map[string]map[string]interface{}),
}
return &rv
}
Loading

0 comments on commit 79cc39a

Please sign in to comment.