blog-post

Go client for Manticore Search

在本教程中,我们将学习如何使用 Go-SDK,这是 Manticore Search 的官方 Go 客户端。

如果您想参加互动课程,请点击 这里

安装


首先,我们确保机器上有一个正在运行的 Manticore 实例。 (如果您需要某个操作系统的 Manticore Search 安装课程,例如 Debian,CentOs,Ubuntu 等,您可以在我们的 互动课程平台 上找到)

searchd --status

root@go-sdk-75b6444c5b-n6ph9:/# searchd --status
Manticore 3.1.0 445e806e@190716 release
Copyright (c) 2001-2016, Andrew Aksyonoff
Copyright (c) 2008-2016, Sphinx Technologies Inc (http://sphinxsearch.com)
Copyright (c) 2017-2019, Manticore Software LTD (http://manticoresearch.com)

using config file '/etc/sphinxsearch/sphinx.conf'...

searchd status
--------------
uptime: 3569
connections: 1
maxed_out: 0
version: 3.1.0 445e806e@190716 release
mysql_version: 5.5.21
command_search: 0
command_excerpt: 0
command_update: 0
command_delete: 0
command_keywords: 0
command_persist: 0
command_status: 1
command_flushattrs: 0
command_set: 0
command_insert: 0
command_replace: 0
command_commit: 0
command_suggest: 0
command_json: 0
command_callpq: 0
agent_connect: 0
agent_retry: 0
queries: 0
dist_queries: 0
workers_total: 30
workers_active: 1
work_queue_length: 1
query_wall: 0.000
query_cpu: OFF
dist_wall: 0.000
dist_local: 0.000
dist_wait: 0.000
query_reads: OFF
query_readkb: OFF
query_readtime: OFF
avg_query_wall: 0.000
avg_query_cpu: OFF
avg_dist_wall: 0.000
avg_dist_local: 0.000
avg_dist_wait: 0.000
avg_query_reads: OFF
avg_query_readkb: OFF
avg_query_readtime: OFF
qcache_max_bytes: 16777216
qcache_thresh_msec: 3000
qcache_ttl_sec: 60
qcache_cached_queries: 0
qcache_used_bytes: 0
qcache_hits: 0

现在我们可以使用以下命令安装 Go-SDK:

go get github.com/manticoresoftware/go-sdk/manticore

root@go-sdk-75b6444c5b-n6ph9:/# go get github.com/manticoresoftware/go-sddk/manticore

基本用法


让我们来看一下这个简单的 Go 脚本:

cat manticore.go

root@go-sdk-75b6444c5b-n6ph9:/# cat manticore.go
package main

import "github.com/manticoresoftware/go-sdk/manticore"
import "fmt"

func main() {
cl := manticore.NewClient()
cl.SetServer("127.0.0.1", 9312)
cl.Open()
res, err := cl.Sphinxql(`replace into testrt values(1,'my subject', 'my content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(2,'another subject', 'more content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(5,'again subject', 'one more content', 10)`)
fmt.Println(res, err)
res2, err2 := cl.Query("more|another", "testrt")
fmt.Println(res2, err2)

该脚本创建一个新的 Manticore 客户端,执行简单的 INSERT 请求到 testrt 索引,然后对添加的内容进行查询。

让我们运行它:

go run manticore.go

root@go-sdk-75b6444c5b-n6ph9:/# go run manticore.go
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
Status: ok
Query time: 0s
Total: 1
Total found: 1
Schema:
Fields:
title
content
Attributes:
gid: int
Matches:
Doc: 2, Weight: 2, attrs: [15]
Word stats:
'more' (Docs:2, Hits:2)
'another' (Docs:1, Hits:1)

正如我们所见,新的文档已成功添加到索引,并且查询找到了包含 moreanother 单词的两个文档。

另外几个例子


使用 Go-SDK,您可以轻松地通过索引属性过滤查询结果

cat manticore2.go

root@go-sdk-75b6444c5b-n6ph9:/# cat manticore2.go
package main

import "github.com/manticoresoftware/go-sdk/manticore"
import "fmt"

func main() {
cl := manticore.NewClient()
cl.SetServer("127.0.0.1", 9312)
cl.Open()
res, err := cl.Sphinxql(`replace into testrt values(1,'my subject', 'my content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(2,'another subject', 'more content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(5,'again subject', 'one more content', 10)`)
fmt.Println(res, err)


q := manticore.NewSearch("content", "testrt", "")
res2, err2 := cl.RunQuery(q)
fmt.Println(res2, err2)

attrValues := []int64{10}
q.AddFilter("gid", attrValues, false)
res2, err2 = cl.RunQuery(q)
fmt.Println(res2, err2)

该脚本创建一个新的 Manticore 客户端,执行简单的 INSERT 请求到 testrt 索引,然后进行两次查询,搜索 content 单词。 第一次查询检索所有文档,因为索引中的所有文档现在都包含该单词。第二个查询使用我们创建的过滤器,仅检索包含 content 单词且属性 gid 等于 10 的文档。在我们的情况下,根据这些条件,只有一个文档符合,id=10

go run manticore2.go

root@go-sdk-75b6444c5b-n6ph9:/# go run manticore2.go
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
Status: ok
Query time: 0s
Total: 3
Total found: 3
Schema:
Fields:
title
content
Attributes:
gid: int
Matches:
Doc: 1, Weight: 1, attrs: [15]
Doc: 2, Weight: 1, attrs: [15]
Doc: 5, Weight: 1, attrs: [10]
Word stats:
'content' (Docs:3, Hits:3)

Status: ok
Query time: 0s
Total: 1
Total found: 1
Schema:
Fields:
title
content
Attributes:
gid: int
Matches:
Doc: 5, Weight: 1, attrs: [10]
Word stats:
'content' (Docs:3, Hits:3)

您也可以使用类似 SQL 的表达式来进行过滤。

cat manticore3.go

root@go-sdk-75b6444c5b-n6ph9:/# cat manticore3.go
package main

import "github.com/manticoresoftware/go-sdk/manticore"
import "fmt"

func main() {
cl := manticore.NewClient()
cl.SetServer("127.0.0.1", 9312)
cl.Open()
res, err := cl.Sphinxql(`replace into testrt values(1,'my subject', 'my content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(2,'another subject', 'more content', 15)`)
fmt.Println(res, err)
res, err = cl.Sphinxql(`replace into testrt values(5,'again subject', 'one more content', 10)`)
fmt.Println(res, err)


q := manticore.NewSearch("content", "testrt", "")
res2, err2 := cl.RunQuery(q)
fmt.Println(res2, err2)

q.AddFilterExpression("gid > 10 AND gid < 20" , false)
res2, err2 = cl.RunQuery(q)
fmt.Println(res2, err2)

在这里,我们使用 gid > 10 AND gid < 20 表达式来过滤查询结果。与之前的示例类似,第一个查询检索索引中的所有文档,第二个查询仅检索 gid=15 的文档。

go run manticore3.go

}root@go-sdk-75b6444c5b-n6ph9:/# go run manticore3.go
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
[Query OK, 1 rows affected]
Status: ok
Query time: 0s
Total: 3
Total found: 3
Schema:
Fields:
title
content
Attributes:
gid: int
Matches:
Doc: 1, Weight: 1, attrs: [15]
Doc: 2, Weight: 1, attrs: [15]
Doc: 5, Weight: 1, attrs: [10]
Word stats:
'content' (Docs:3, Hits:3)

Status: ok
Query time: 5ms
Total: 2
Total found: 2
Schema:
Fields:
title
content
Attributes:
gid: int
Matches:
Doc: 1, Weight: 1, attrs: [15]
Doc: 2, Weight: 1, attrs: [15]
Word stats:
'content' (Docs:3, Hits:3)

你可以在 这里 找到有关连接到服务器的更多信息。

Manticore GO-SDK 文档在 这里

安装Manticore Search

安装Manticore Search