search

package
v0.1.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 12, 2026 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrClosed = errors.New("search engine closed")

Functions

func BuildIndexMapping

func BuildIndexMapping(defaultAnalyzer string) *mapping.IndexMappingImpl

Types

type ClauseFuzzy

type ClauseFuzzy struct {
	Field     string
	Term      string
	Fuzziness int // 0,1,2…
	Prefix    int // 前缀长
	Boost     *float64
}

type ClauseMatch

type ClauseMatch struct {
	Field    string
	Query    string
	Boost    *float64
	Operator string // "and"/"or",默认 or
}

-------- 高级搜索子句(新增) --------

type ClausePhrase

type ClausePhrase struct {
	Field  string
	Phrase string
	Slop   int // 词间隔
	Boost  *float64
}

type ClausePrefix

type ClausePrefix struct {
	Field  string
	Prefix string
	Boost  *float64
}

type ClauseQueryString

type ClauseQueryString struct {
	Query  string   // 直接使用 QueryString 语法
	Fields []string // 如果不为空,会转换成 field:(q) OR ...
	Boost  *float64
}

type ClauseRegex

type ClauseRegex struct {
	Field   string
	Pattern string
	Boost   *float64
}

type ClauseWildcard

type ClauseWildcard struct {
	Field   string
	Pattern string
	Boost   *float64
}

type Config

type Config struct {
	IndexPath           string
	DefaultAnalyzer     string
	DefaultSearchFields []string
	OpenTimeout         time.Duration
	QueryTimeout        time.Duration
	BatchSize           int
}

type Doc

type Doc struct {
	ID     string                 `json:"id"`
	Type   string                 `json:"type"`
	Fields map[string]interface{} `json:"fields"` // 使用 interface{} 来处理任何类型的数据
}

type Engine

type Engine interface {
	Index(ctx context.Context, doc Doc) error
	IndexBatch(ctx context.Context, docs []Doc) error
	Delete(ctx context.Context, id string) error
	Search(ctx context.Context, req SearchRequest) (SearchResult, error)
	Get(ctx context.Context, id string) (Hit, error)
	List(ctx context.Context, from int, size int) (SearchResult, error)
	GetAutoCompleteSuggestions(ctx context.Context, keyword string) ([]string, error)
	GetSearchSuggestions(ctx context.Context, keyword string) ([]string, error)
	Close() error
}

func New

func New(cfg Config, m mapping.IndexMapping) (Engine, error)

func NewDefault

func NewDefault(cfg Config) (Engine, error)

func NewTempDefault

func NewTempDefault(indexName string) (Engine, string, error)

type FacetRequest

type FacetRequest struct {
	Name  string // 返回名
	Field string // 字段
	Size  int    // Top N

}

Facet 聚合

type FacetResult

type FacetResult struct {
	Total int         `json:"total"`
	Terms []FacetTerm `json:"terms"`
}

type FacetTerm

type FacetTerm struct {
	Term  string `json:"term"`
	Count int    `json:"count"`
}

type Hit

type Hit struct {
	ID        string              `json:"id"`
	Score     float64             `json:"score"`
	Fields    map[string]any      `json:"fields"`
	Fragments map[string][]string `json:"fragments,omitempty"`
}

type NumericRangeFilter

type NumericRangeFilter struct {
	Field   string
	GTE, GT *float64
	LTE, LT *float64
}

type SearchRequest

type SearchRequest struct {
	// 关键字(保留老接口)
	Keyword      string
	SearchFields []string

	// 结构化 Term
	MustTerms    map[string][]string
	MustNotTerms map[string][]string
	ShouldTerms  map[string][]string

	// 数值/时间过滤
	NumericRanges []NumericRangeFilter
	TimeRanges    []TimeRangeFilter

	// 高级查询子句(新增)
	QueryString *ClauseQueryString
	Matches     []ClauseMatch
	Phrases     []ClausePhrase
	Prefixes    []ClausePrefix
	Wildcards   []ClauseWildcard
	Regexps     []ClauseRegex
	Fuzzies     []ClauseFuzzy

	// 布尔控制
	MinShould int // 至少满足多少个 should(对 ShouldTerms + 高级 should 子句生效)

	// Facet 聚合
	Facets []FacetRequest

	// 排序与分页
	SortBy []string
	From   int
	Size   int

	// 字段返回与高亮
	IncludeFields   []string
	Highlight       bool
	HighlightFields []string // 指定需要高亮的字段,默认全部 text 字段
	FragmentSize    int      // 片段长度
	MaxFragments    int      // 每字段片段数
}

type SearchResult

type SearchResult struct {
	Total  uint64                 `json:"total"`
	Took   time.Duration          `json:"took"`
	Hits   []Hit                  `json:"hits"`
	Facets map[string]FacetResult `json:"facets,omitempty"`
}

type TimeRangeFilter

type TimeRangeFilter struct {
	Field   string
	From    *time.Time
	To      *time.Time
	IncFrom bool
	IncTo   bool
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL