2014-07-30 18:30:38 +02:00
|
|
|
// 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.
|
2014-09-01 17:15:38 +02:00
|
|
|
|
|
|
|
package simple
|
2014-06-26 17:43:13 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"container/heap"
|
2014-09-01 17:15:38 +02:00
|
|
|
"fmt"
|
2014-06-26 17:43:13 +02:00
|
|
|
|
2014-08-28 21:38:57 +02:00
|
|
|
"github.com/blevesearch/bleve/document"
|
2014-09-01 17:15:38 +02:00
|
|
|
"github.com/blevesearch/bleve/registry"
|
|
|
|
"github.com/blevesearch/bleve/search"
|
|
|
|
"github.com/blevesearch/bleve/search/highlight"
|
2014-06-26 17:43:13 +02:00
|
|
|
)
|
|
|
|
|
2014-09-01 17:15:38 +02:00
|
|
|
const Name = "simple"
|
|
|
|
const defaultSeparator = "…"
|
2014-06-26 17:43:13 +02:00
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
type Highlighter struct {
|
2014-09-01 17:15:38 +02:00
|
|
|
fragmenter highlight.Fragmenter
|
|
|
|
formatter highlight.FragmentFormatter
|
2014-06-26 17:43:13 +02:00
|
|
|
sep string
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func NewHighlighter(fragmenter highlight.Fragmenter, formatter highlight.FragmentFormatter, separator string) *Highlighter {
|
|
|
|
return &Highlighter{
|
2014-09-01 17:15:38 +02:00
|
|
|
fragmenter: fragmenter,
|
|
|
|
formatter: formatter,
|
|
|
|
sep: separator,
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) Fragmenter() highlight.Fragmenter {
|
2014-06-26 17:43:13 +02:00
|
|
|
return s.fragmenter
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) SetFragmenter(f highlight.Fragmenter) {
|
2014-06-26 17:43:13 +02:00
|
|
|
s.fragmenter = f
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) FragmentFormatter() highlight.FragmentFormatter {
|
2014-06-26 17:43:13 +02:00
|
|
|
return s.formatter
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) SetFragmentFormatter(f highlight.FragmentFormatter) {
|
2014-06-26 17:43:13 +02:00
|
|
|
s.formatter = f
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) Separator() string {
|
2014-06-26 17:43:13 +02:00
|
|
|
return s.sep
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) SetSeparator(sep string) {
|
2014-06-26 17:43:13 +02:00
|
|
|
s.sep = sep
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) BestFragmentInField(dm *search.DocumentMatch, doc *document.Document, field string) string {
|
2014-06-26 17:43:13 +02:00
|
|
|
fragments := s.BestFragmentsInField(dm, doc, field, 1)
|
|
|
|
if len(fragments) > 0 {
|
|
|
|
return fragments[0]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
func (s *Highlighter) BestFragmentsInField(dm *search.DocumentMatch, doc *document.Document, field string, num int) []string {
|
2014-06-26 17:43:13 +02:00
|
|
|
tlm := dm.Locations[field]
|
2014-09-01 17:15:38 +02:00
|
|
|
orderedTermLocations := highlight.OrderTermLocations(tlm)
|
2014-09-04 01:16:46 +02:00
|
|
|
scorer := NewFragmentScorer(dm.Locations[field])
|
2014-06-26 17:43:13 +02:00
|
|
|
|
|
|
|
// score the fragments and put them into a priority queue ordered by score
|
|
|
|
fq := make(FragmentQueue, 0)
|
|
|
|
heap.Init(&fq)
|
|
|
|
for _, f := range doc.Fields {
|
2014-07-14 20:47:05 +02:00
|
|
|
if f.Name() == field {
|
2014-08-06 19:52:20 +02:00
|
|
|
_, ok := f.(*document.TextField)
|
|
|
|
if ok {
|
|
|
|
fieldData := f.Value()
|
|
|
|
fragments := s.fragmenter.Fragment(fieldData, orderedTermLocations)
|
|
|
|
for _, fragment := range fragments {
|
|
|
|
scorer.Score(fragment)
|
|
|
|
heap.Push(&fq, fragment)
|
|
|
|
}
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now find the N best non-overlapping fragments
|
2014-09-01 17:15:38 +02:00
|
|
|
bestFragments := make([]*highlight.Fragment, 0)
|
2014-07-11 20:25:32 +02:00
|
|
|
if len(fq) > 0 {
|
|
|
|
candidate := heap.Pop(&fq)
|
|
|
|
OUTER:
|
|
|
|
for candidate != nil && len(bestFragments) < num {
|
|
|
|
// see if this overlaps with any of the best already identified
|
|
|
|
if len(bestFragments) > 0 {
|
|
|
|
for _, frag := range bestFragments {
|
2014-09-01 17:15:38 +02:00
|
|
|
if candidate.(*highlight.Fragment).Overlaps(frag) {
|
2014-07-11 20:25:32 +02:00
|
|
|
if len(fq) < 1 {
|
|
|
|
break OUTER
|
|
|
|
}
|
|
|
|
candidate = heap.Pop(&fq)
|
|
|
|
continue OUTER
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
2014-09-01 17:15:38 +02:00
|
|
|
bestFragments = append(bestFragments, candidate.(*highlight.Fragment))
|
2014-07-11 20:25:32 +02:00
|
|
|
} else {
|
2014-09-01 17:15:38 +02:00
|
|
|
bestFragments = append(bestFragments, candidate.(*highlight.Fragment))
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-11 20:25:32 +02:00
|
|
|
if len(fq) < 1 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
candidate = heap.Pop(&fq)
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now that we have the best fragments, we can format them
|
|
|
|
formattedFragments := make([]string, len(bestFragments))
|
|
|
|
for i, fragment := range bestFragments {
|
2014-08-28 20:57:27 +02:00
|
|
|
formattedFragments[i] = ""
|
2014-09-01 17:15:38 +02:00
|
|
|
if fragment.Start != 0 {
|
2014-08-28 20:57:27 +02:00
|
|
|
formattedFragments[i] += s.sep
|
|
|
|
}
|
|
|
|
formattedFragments[i] += s.formatter.Format(fragment, dm.Locations[field])
|
2014-09-01 17:15:38 +02:00
|
|
|
if fragment.End != len(fragment.Orig) {
|
2014-08-28 20:57:27 +02:00
|
|
|
formattedFragments[i] += s.sep
|
|
|
|
}
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
|
2014-07-03 20:53:44 +02:00
|
|
|
if dm.Fragments == nil {
|
2014-09-01 17:15:38 +02:00
|
|
|
dm.Fragments = make(search.FieldFragmentMap, 0)
|
2014-07-03 20:53:44 +02:00
|
|
|
}
|
|
|
|
dm.Fragments[field] = formattedFragments
|
|
|
|
|
2014-06-26 17:43:13 +02:00
|
|
|
return formattedFragments
|
|
|
|
}
|
|
|
|
|
2014-09-04 00:47:02 +02:00
|
|
|
// FragmentQueue implements heap.Interface and holds Items.
|
2014-09-01 17:15:38 +02:00
|
|
|
type FragmentQueue []*highlight.Fragment
|
2014-06-26 17:43:13 +02:00
|
|
|
|
|
|
|
func (fq FragmentQueue) Len() int { return len(fq) }
|
|
|
|
|
|
|
|
func (fq FragmentQueue) Less(i, j int) bool {
|
|
|
|
// We want Pop to give us the highest, not lowest, priority so we use greater than here.
|
2014-09-01 17:15:38 +02:00
|
|
|
return fq[i].Score > fq[j].Score
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fq FragmentQueue) Swap(i, j int) {
|
|
|
|
fq[i], fq[j] = fq[j], fq[i]
|
2014-09-01 17:15:38 +02:00
|
|
|
fq[i].Index = i
|
|
|
|
fq[j].Index = j
|
2014-06-26 17:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fq *FragmentQueue) Push(x interface{}) {
|
|
|
|
n := len(*fq)
|
2014-09-01 17:15:38 +02:00
|
|
|
item := x.(*highlight.Fragment)
|
|
|
|
item.Index = n
|
2014-06-26 17:43:13 +02:00
|
|
|
*fq = append(*fq, item)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fq *FragmentQueue) Pop() interface{} {
|
|
|
|
old := *fq
|
|
|
|
n := len(old)
|
|
|
|
item := old[n-1]
|
2014-09-01 17:15:38 +02:00
|
|
|
item.Index = -1 // for safety
|
2014-06-26 17:43:13 +02:00
|
|
|
*fq = old[0 : n-1]
|
|
|
|
return item
|
|
|
|
}
|
2014-09-01 17:15:38 +02:00
|
|
|
|
|
|
|
func Constructor(config map[string]interface{}, cache *registry.Cache) (highlight.Highlighter, error) {
|
|
|
|
separator := defaultSeparator
|
|
|
|
separatorVal, ok := config["separator"].(string)
|
|
|
|
if ok {
|
|
|
|
separator = separatorVal
|
|
|
|
}
|
|
|
|
|
|
|
|
fragmenterName, ok := config["fragmenter"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("must specify fragmenter")
|
|
|
|
}
|
|
|
|
fragmenter, err := cache.FragmenterNamed(fragmenterName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error building fragmenter: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
formatterName, ok := config["formatter"].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, fmt.Errorf("must specify formatter")
|
|
|
|
}
|
|
|
|
formatter, err := cache.FragmentFormatterNamed(formatterName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error building fragment formatter: %v", err)
|
|
|
|
}
|
|
|
|
|
2014-09-04 01:16:46 +02:00
|
|
|
return NewHighlighter(fragmenter, formatter, separator), nil
|
2014-09-01 17:15:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
registry.RegisterHighlighter(Name, Constructor)
|
|
|
|
}
|