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.
21
"github.com/blevesearch/bleve/document"
22
"github.com/blevesearch/bleve/registry"
23
"github.com/blevesearch/bleve/search"
24
"github.com/blevesearch/bleve/search/highlight"
28
const DefaultSeparator = "…"
30
type Highlighter struct {
31
fragmenter highlight.Fragmenter
32
formatter highlight.FragmentFormatter
36
func NewHighlighter(fragmenter highlight.Fragmenter, formatter highlight.FragmentFormatter, separator string) *Highlighter {
38
fragmenter: fragmenter,
44
func (s *Highlighter) Fragmenter() highlight.Fragmenter {
48
func (s *Highlighter) SetFragmenter(f highlight.Fragmenter) {
52
func (s *Highlighter) FragmentFormatter() highlight.FragmentFormatter {
56
func (s *Highlighter) SetFragmentFormatter(f highlight.FragmentFormatter) {
60
func (s *Highlighter) Separator() string {
64
func (s *Highlighter) SetSeparator(sep string) {
68
func (s *Highlighter) BestFragmentInField(dm *search.DocumentMatch, doc *document.Document, field string) string {
69
fragments := s.BestFragmentsInField(dm, doc, field, 1)
70
if len(fragments) > 0 {
76
func (s *Highlighter) BestFragmentsInField(dm *search.DocumentMatch, doc *document.Document, field string, num int) []string {
77
tlm := dm.Locations[field]
78
orderedTermLocations := highlight.OrderTermLocations(tlm)
79
scorer := NewFragmentScorer(tlm)
81
// score the fragments and put them into a priority queue ordered by score
82
fq := make(FragmentQueue, 0)
84
for _, f := range doc.Fields {
85
if f.Name() == field {
86
_, ok := f.(*document.TextField)
88
termLocationsSameArrayPosition := make(highlight.TermLocations, 0)
89
for _, otl := range orderedTermLocations {
90
if otl.ArrayPositions.Equals(f.ArrayPositions()) {
91
termLocationsSameArrayPosition = append(termLocationsSameArrayPosition, otl)
95
fieldData := f.Value()
96
fragments := s.fragmenter.Fragment(fieldData, termLocationsSameArrayPosition)
97
for _, fragment := range fragments {
98
fragment.ArrayPositions = f.ArrayPositions()
99
scorer.Score(fragment)
100
heap.Push(&fq, fragment)
106
// now find the N best non-overlapping fragments
107
var bestFragments []*highlight.Fragment
109
candidate := heap.Pop(&fq)
111
for candidate != nil && len(bestFragments) < num {
112
// see if this overlaps with any of the best already identified
113
if len(bestFragments) > 0 {
114
for _, frag := range bestFragments {
115
if candidate.(*highlight.Fragment).Overlaps(frag) {
119
candidate = heap.Pop(&fq)
123
bestFragments = append(bestFragments, candidate.(*highlight.Fragment))
125
bestFragments = append(bestFragments, candidate.(*highlight.Fragment))
131
candidate = heap.Pop(&fq)
135
// now that we have the best fragments, we can format them
136
orderedTermLocations.MergeOverlapping()
137
formattedFragments := make([]string, len(bestFragments))
138
for i, fragment := range bestFragments {
139
formattedFragments[i] = ""
140
if fragment.Start != 0 {
141
formattedFragments[i] += s.sep
143
formattedFragments[i] += s.formatter.Format(fragment, orderedTermLocations)
144
if fragment.End != len(fragment.Orig) {
145
formattedFragments[i] += s.sep
149
if dm.Fragments == nil {
150
dm.Fragments = make(search.FieldFragmentMap, 0)
152
if len(formattedFragments) > 0 {
153
dm.Fragments[field] = formattedFragments
156
return formattedFragments
159
// FragmentQueue implements heap.Interface and holds Items.
160
type FragmentQueue []*highlight.Fragment
162
func (fq FragmentQueue) Len() int { return len(fq) }
164
func (fq FragmentQueue) Less(i, j int) bool {
165
// We want Pop to give us the highest, not lowest, priority so we use greater-than here.
166
return fq[i].Score > fq[j].Score
169
func (fq FragmentQueue) Swap(i, j int) {
170
fq[i], fq[j] = fq[j], fq[i]
175
func (fq *FragmentQueue) Push(x interface{}) {
177
item := x.(*highlight.Fragment)
179
*fq = append(*fq, item)
182
func (fq *FragmentQueue) Pop() interface{} {
186
item.Index = -1 // for safety
191
func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.Highlighter, error) {
192
separator := DefaultSeparator
193
separatorVal, ok := config["separator"].(string)
195
separator = separatorVal
198
fragmenterName, ok := config["fragmenter"].(string)
200
return nil, fmt.Errorf("must specify fragmenter")
202
fragmenter, err := cache.FragmenterNamed(fragmenterName)
204
return nil, fmt.Errorf("error building fragmenter: %v", err)
207
formatterName, ok := config["formatter"].(string)
209
return nil, fmt.Errorf("must specify formatter")
211
formatter, err := cache.FragmentFormatterNamed(formatterName)
213
return nil, fmt.Errorf("error building fragment formatter: %v", err)
216
return NewHighlighter(fragmenter, formatter, separator), nil
220
registry.RegisterHighlighter(Name, Constructor)