Fulltext search op (pgsql)

master
Vitaliy Filippov 2017-04-14 18:22:41 +03:00
parent 43a14306dc
commit da61367c19
2 changed files with 49 additions and 0 deletions

View File

@ -6,6 +6,9 @@ package models
import (
"time"
"fmt"
"github.com/go-xorm/builder"
)
type Commit struct {
@ -20,3 +23,13 @@ type Commit struct {
CommitterName string
CommitterTime time.Time
}
func FulltextSearchCommits(q string) ([]*Commit, error) {
sess := x.NewSession()
sess.Where(builder.Match{"message", q})
commits := make([]*Commit, 0)
if err := sess.Find(&commits); err != nil {
return nil, fmt.Errorf("Find: %v", err)
}
return commits, nil
}

36
vendor/github.com/go-xorm/builder/cond_match.go generated vendored Normal file
View File

@ -0,0 +1,36 @@
// Copyright 2016 The Xorm Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package builder
import "fmt"
// Match defines full-text match condition
type Match [2]string
var _ Cond = Match{"", ""}
// WriteTo write SQL to Writer
func (m Match) WriteTo(w Writer) error {
if _, err := fmt.Fprintf(w, "to_tsvector(%s) @@ to_tsquery(?)", m[0]); err != nil {
return err
}
w.Append(m[1])
return nil
}
// And implements And with other conditions
func (m Match) And(conds ...Cond) Cond {
return And(m, And(conds...))
}
// Or implements Or with other conditions
func (m Match) Or(conds ...Cond) Cond {
return Or(m, Or(conds...))
}
// IsValid tests if this condition is valid
func (m Match) IsValid() bool {
return len(m[0]) > 0 && len(m[1]) > 0
}