1
// Copyright (c) 2014 Couchbase, Inc.
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
7
// http://www.apache.org/licenses/LICENSE-2.0
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.
20
"github.com/blevesearch/bleve/analysis"
21
"github.com/blevesearch/bleve/numeric"
24
const DefaultNumericIndexingOptions = StoreField | IndexField
26
const DefaultPrecisionStep uint = 4
28
type NumericField struct {
30
arrayPositions []uint64
31
options IndexingOptions
32
value numeric.PrefixCoded
33
numPlainTextBytes uint64
36
func (n *NumericField) Name() string {
40
func (n *NumericField) ArrayPositions() []uint64 {
41
return n.arrayPositions
44
func (n *NumericField) Options() IndexingOptions {
48
func (n *NumericField) Analyze() (int, analysis.TokenFrequencies) {
49
tokens := make(analysis.TokenStream, 0)
50
tokens = append(tokens, &analysis.Token{
55
Type: analysis.Numeric,
58
original, err := n.value.Int64()
61
shift := DefaultPrecisionStep
63
shiftEncoded, err := numeric.NewPrefixCodedInt64(original, shift)
67
token := analysis.Token{
69
End: len(shiftEncoded),
72
Type: analysis.Numeric,
74
tokens = append(tokens, &token)
75
shift += DefaultPrecisionStep
79
fieldLength := len(tokens)
80
tokenFreqs := analysis.TokenFrequency(tokens, n.arrayPositions, n.options.IncludeTermVectors())
81
return fieldLength, tokenFreqs
84
func (n *NumericField) Value() []byte {
88
func (n *NumericField) Number() (float64, error) {
89
i64, err := n.value.Int64()
93
return numeric.Int64ToFloat64(i64), nil
96
func (n *NumericField) GoString() string {
97
return fmt.Sprintf("&document.NumericField{Name:%s, Options: %s, Value: %s}", n.name, n.options, n.value)
100
func (n *NumericField) NumPlainTextBytes() uint64 {
101
return n.numPlainTextBytes
104
func NewNumericFieldFromBytes(name string, arrayPositions []uint64, value []byte) *NumericField {
105
return &NumericField{
107
arrayPositions: arrayPositions,
109
options: DefaultNumericIndexingOptions,
110
numPlainTextBytes: uint64(len(value)),
114
func NewNumericField(name string, arrayPositions []uint64, number float64) *NumericField {
115
return NewNumericFieldWithIndexingOptions(name, arrayPositions, number, DefaultNumericIndexingOptions)
118
func NewNumericFieldWithIndexingOptions(name string, arrayPositions []uint64, number float64, options IndexingOptions) *NumericField {
119
numberInt64 := numeric.Float64ToInt64(number)
120
prefixCoded := numeric.MustNewPrefixCodedInt64(numberInt64, 0)
121
return &NumericField{
123
arrayPositions: arrayPositions,
126
// not correct, just a place holder until we revisit how fields are
127
// represented and can fix this better
128
numPlainTextBytes: uint64(8),