Fulltext search op (pgsql)
parent
43a14306dc
commit
da61367c19
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
Loading…
Reference in New Issue