Skip to content

Commit

Permalink
adding second example to demonstrate edge ngram mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
mschoch committed Sep 7, 2014
1 parent 2c10ce9 commit 6899268
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 3 deletions.
1 change: 1 addition & 0 deletions mapping.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
// and limitations under the License.

// +build !example1
// +build !example2

package main

Expand Down
5 changes: 2 additions & 3 deletions mapping_example1.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,8 @@ const textFieldAnalyzer = "en"
func buildIndexMapping() (*bleve.IndexMapping, error) {

// a custom field definition that uses our custom analyzer
notTooLongFieldMapping := bleve.NewFieldMapping(
"", "text", "enNotTooLong",
true, true, true, true)
notTooLongFieldMapping := bleve.NewTextFieldMapping()
notTooLongFieldMapping.Analyzer = "enNotTooLong"

// a generic reusable mapping for english text
englishTextFieldMapping := bleve.NewTextFieldMapping()
Expand Down
94 changes: 94 additions & 0 deletions mapping_example2.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.

// +build example2

package main

import (
"github.com/blevesearch/bleve"
)

const textFieldAnalyzer = "en"

func buildIndexMapping() (*bleve.IndexMapping, error) {

// a custom field definition that uses our custom analyzer
edgeNgram325FieldMapping := bleve.NewTextFieldMapping()
edgeNgram325FieldMapping.Analyzer = "enWithEdgeNgram325"

// a generic reusable mapping for english text
englishTextFieldMapping := bleve.NewTextFieldMapping()
englishTextFieldMapping.Analyzer = "en"

// a generic reusable mapping for keyword text
keywordFieldMapping := bleve.NewTextFieldMapping()
keywordFieldMapping.Analyzer = "keyword"

// a specific mapping to index the description fields
// detected language
descriptionLangFieldMapping := bleve.NewTextFieldMapping()
descriptionLangFieldMapping.Name = "descriptionLang"
descriptionLangFieldMapping.Analyzer = "detect_lang"
descriptionLangFieldMapping.Store = false
descriptionLangFieldMapping.IncludeTermVectors = false
descriptionLangFieldMapping.IncludeInAll = false

beerMapping := bleve.NewDocumentMapping()

// name
beerMapping.AddFieldMappingsAt("name", englishTextFieldMapping)

// description
beerMapping.AddFieldMappingsAt("description",
edgeNgram325FieldMapping,
descriptionLangFieldMapping)

beerMapping.AddFieldMappingsAt("type", keywordFieldMapping)
beerMapping.AddFieldMappingsAt("style", keywordFieldMapping)
beerMapping.AddFieldMappingsAt("category", keywordFieldMapping)

breweryMapping := bleve.NewDocumentMapping()
breweryMapping.AddFieldMappingsAt("name", englishTextFieldMapping)
breweryMapping.AddFieldMappingsAt("description", englishTextFieldMapping)

indexMapping := bleve.NewIndexMapping()
indexMapping.AddDocumentMapping("beer", beerMapping)
indexMapping.AddDocumentMapping("brewery", breweryMapping)

indexMapping.TypeField = "type"
indexMapping.DefaultAnalyzer = textFieldAnalyzer

err := indexMapping.AddCustomTokenFilter("edgeNgram325",
map[string]interface{}{
"type": "edge_ngram",
"min": 3.0,
"max": 25.0,
})
if err != nil {
return nil, err
}

err = indexMapping.AddCustomAnalyzer("enWithEdgeNgram325",
map[string]interface{}{
"type": "custom",
"tokenizer": "unicode",
"token_filters": []string{
"possessive_en",
"to_lower",
"stop_en",
"edgeNgram325",
},
})
if err != nil {
return nil, err
}

return indexMapping, nil
}

0 comments on commit 6899268

Please sign in to comment.