इस ट्यूटोरियल में हम अध्ययन करेंगे कि Manticore Search के लिए आधिकारिक Go क्लाइंट Go-SDK का उपयोग कैसे करें।
यदि आप एक इंटरैक्टिव पाठ्यक्रम के माध्यम से जाना चाहते हैं, तो यहाँ जाएँ।
स्थापना
पहले, हम यह सुनिश्चित करते हैं कि मशीन पर एक चल रहा Manticore इंस्टेंस है। (यदि आपको किसी विशेष OS जैसे Debian, CentOs, Ubuntu आदि के लिए Manticore Search स्थापना पाठ्यक्रम की आवश्यकता है, तो आप इसे हमारे इंटरैक्टिव पाठ्यक्रम प्लेटफार्म में पा सकते हैं।)
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 क्लाइंट बनाती है, testrt इंडेक्स में सरल INSERT अनुरोधों को निष्पादित करती है और फिर जोड़े गए सामग्री के लिए एक क्वेरी करती है।
आइए इसे चलाते हैं:
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)
जैसा कि हम देखते हैं, नए दस्तावेज़ सफलतापूर्वक इंडेक्स में जोड़े गए और क्वेरी द्वारा more या another शब्दों वाले दो दस्तावेज़ पाए गए।
कुछ और उदाहरण
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 क्लाइंट बनाती है, testrt इंडेक्स में सरल INSERT अनुरोधों को निष्पादित करती है और फिर 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]
स्थिति: ठीक
क्वेरी समय: 0स
कुल: 3
कुल मिले: 3
स्कीमा:
फील्ड्स:
शीर्षक
सामग्री
विशेषताएं:
gid: int
मैच:
डॉक: 1, वजन: 1, विशेषताएं: [15]
डॉक: 2, वजन: 1, विशेषताएं: [15]
डॉक: 5, वजन: 1, विशेषताएं: [10]
शब्द आँकड़े:
'सामग्री' (दस्तावेज़:3, हिट्स:3)
स्थिति: ठीक
क्वेरी समय: 5ms
कुल: 2
कुल मिले: 2
स्कीमा:
फील्ड्स:
शीर्षक
सामग्री
विशेषताएं:
gid: int
मैच:
डॉक: 1, वजन: 1, विशेषताएं: [15]
डॉक: 2, वजन: 1, विशेषताएं: [15]
शब्द आँकड़े:
'सामग्री' (दस्तावेज़:3, हिट्स:3)
मैनटिकोर सर्वर से कनेक्ट करने के बारे में अधिक जानकारी यहाँ मिल सकती है।
मैनटिकोर GO-SDK दस्तावेज़ीकरण यहाँ उपलब्ध है।