Skip to content

Commit

Permalink
update custom scoring criteria docs
Browse files Browse the repository at this point in the history
  • Loading branch information
vcaesar committed Nov 19, 2017
1 parent f16224e commit 393d778
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 4 deletions.
36 changes: 36 additions & 0 deletions docs/en/custom_scoring_criteria.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Custom rating fields and rating rules
===

The riot engine supports saving some rating fields in the sequencer memory and scoring documents using custom rating rules. Example:

```go
// Custom rating field
type MyScoringFields struct {
// Add some sort of document data, can be any type, such as:
label string
counter int32
someScore float32
}

// MyScoringCriteria implements the types.ScoringCriteria interface, which is the Score function below
type MyScoringCriteria struct {
}
func (criteria MyScoringCriteria) Score(
doc types.IndexedDocument, fields interface{}) []float32 {
// First check if the scoring field is of type MyScoringFields, if not, an empty slice is returned and the document is removed from the result
if reflect.TypeOf(fields) != reflect.TypeOf(MySearchFields{}) {
return []float32{}
}

// Match is type conversion
myFields := fields.(MySearchFields)

// The following uses the data in myFields to rate the document and return the score
}
```

Document MyScoringFields data through riot.Engine IndexDocument function passed to the sequencer stored in memory. Then through the Search function parameters call MyScoringCriteria query.

Of course, the Score function of MyScoringCriteria can also read more document data from the hard disk or database through docId for scoring, but the speed is much slower than directly reading from memory. Please choose between memory and speed.

[examples/weibo/custom_scoring_criteria.go](/examples/custom_scoring_criteria.go) contains an example of using custom rules to query Weibo data.
8 changes: 4 additions & 4 deletions docs/zh/custom_scoring_criteria.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,25 @@ type MyScoringFields struct {
someScore float32
}

// MyScoringCriteria实现了types.ScoringCriteria接口,也就是下面的Score函数
// MyScoringCriteria 实现了 types.ScoringCriteria 接口,也就是下面的 Score 函数
type MyScoringCriteria struct {
}
func (criteria MyScoringCriteria) Score(
doc types.IndexedDocument, fields interface{}) []float32 {
// 首先检查评分字段是否为MyScoringFields类型的,如果不是则返回空切片,此文档将从结果中剔除
// 首先检查评分字段是否为 MyScoringFields 类型的,如果不是则返回空切片,此文档将从结果中剔除
if reflect.TypeOf(fields) != reflect.TypeOf(MySearchFields{}) {
return []float32{}
}

// 匹配则进行类型转换
myFields := fields.(MySearchFields)

// 下面利用myFields中的数据给文档评分并返回分值
// 下面利用 myFields 中的数据给文档评分并返回分值
}
```

文档的 MyScoringFields 数据通过 riot.Engine 的 IndexDocument 函数传给排序器保存在内存中。然后通过 Search 函数的参数调用 MyScoringCriteria 进行查询。

当然,MyScoringCriteria的Score 函数也可以通过 docId 从硬盘或数据库读取更多文档数据用于打分,但速度要比从内存中直接读慢许多,请在内存和速度之间合适取舍。

[examples/custom_scoring_criteria.go](/examples/custom_scoring_criteria.go) 中包含了一个利用自定义规则查询微博数据的例子。
[examples/weibo/custom_scoring_criteria.go](/examples/custom_scoring_criteria.go) 中包含了一个利用自定义规则查询微博数据的例子。

0 comments on commit 393d778

Please sign in to comment.