blog-post

Go client for Manticore Search

In this tutorial we will study how to use Go-SDK, the official Go client for Manticore Search.

In case if you want to go through an interactive course, go here.

Installation


First, we make sure there’s a running Manticore instance on the machine. (If you need Manticore Search Installation course for a certain OS like Debian, CentOs, Ubuntu e.t.c, you can find it in our interactive courses platform )

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

Now we can instal Go-SDK with the following command:

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

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

Basic usage


Lets’ take a look at this simple Go script:

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)

The script creates a new Manticore client, executes simple INSERT requests to the testrt index and then makes a query for the added content.

Let’s run it:

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)

As we see, new docs were succesfully added to the index and two docs containing more or another words were found by the query.

A couple more examples


With Go-SDK you can easily filter your query results by index attributes

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)

This script creates a new Manticore client, executes simple INSERT requests to the testrt index and then makes two queries searching for content word. The first query retrieves all docs as all docs in the index now contain this word. The second query uses the filter we’ve created and retrieves only the docs that contain content word and have attribute gid equal to 10. In our case, there’s only one doc under these conditions, with 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)

You can also use an SQL-like expression to do your filtering.

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)

Here we use gid > 10 AND gid < 20 expression to filter our query result. Like in the previous example the first query retrieves all the docs from the index and the second one retrieves only the docs with 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)

You can find the full more info on connecting to Manticore here.

Manticore GO-SDK documentation is here.

Install Manticore Search

Install Manticore Search