mirror of
https://github.com/kevin-DL/m3o-go.git
synced 2026-01-11 18:44:26 +00:00
132 lines
3.0 KiB
Go
Executable File
132 lines
3.0 KiB
Go
Executable File
package search
|
|
|
|
import (
|
|
"go.m3o.com/client"
|
|
)
|
|
|
|
type Search interface {
|
|
CreateIndex(*CreateIndexRequest) (*CreateIndexResponse, error)
|
|
DeleteIndex(*DeleteIndexRequest) (*DeleteIndexResponse, error)
|
|
Delete(*DeleteRequest) (*DeleteResponse, error)
|
|
Index(*IndexRequest) (*IndexResponse, error)
|
|
Search(*SearchRequest) (*SearchResponse, error)
|
|
}
|
|
|
|
func NewSearchService(token string) *SearchService {
|
|
return &SearchService{
|
|
client: client.NewClient(&client.Options{
|
|
Token: token,
|
|
}),
|
|
}
|
|
}
|
|
|
|
type SearchService struct {
|
|
client *client.Client
|
|
}
|
|
|
|
// Create an index by name
|
|
func (t *SearchService) CreateIndex(request *CreateIndexRequest) (*CreateIndexResponse, error) {
|
|
|
|
rsp := &CreateIndexResponse{}
|
|
return rsp, t.client.Call("search", "CreateIndex", request, rsp)
|
|
|
|
}
|
|
|
|
// Delete an index by name
|
|
func (t *SearchService) DeleteIndex(request *DeleteIndexRequest) (*DeleteIndexResponse, error) {
|
|
|
|
rsp := &DeleteIndexResponse{}
|
|
return rsp, t.client.Call("search", "DeleteIndex", request, rsp)
|
|
|
|
}
|
|
|
|
// Delete a record given its ID
|
|
func (t *SearchService) Delete(request *DeleteRequest) (*DeleteResponse, error) {
|
|
|
|
rsp := &DeleteResponse{}
|
|
return rsp, t.client.Call("search", "Delete", request, rsp)
|
|
|
|
}
|
|
|
|
// Index a record i.e. insert a document to search for.
|
|
func (t *SearchService) Index(request *IndexRequest) (*IndexResponse, error) {
|
|
|
|
rsp := &IndexResponse{}
|
|
return rsp, t.client.Call("search", "Index", request, rsp)
|
|
|
|
}
|
|
|
|
// Search for records in a given in index
|
|
func (t *SearchService) Search(request *SearchRequest) (*SearchResponse, error) {
|
|
|
|
rsp := &SearchResponse{}
|
|
return rsp, t.client.Call("search", "Search", request, rsp)
|
|
|
|
}
|
|
|
|
type CreateIndexRequest struct {
|
|
// The name of the index
|
|
Index string `json:"index"`
|
|
}
|
|
|
|
type CreateIndexResponse struct {
|
|
}
|
|
|
|
type DeleteIndexRequest struct {
|
|
// The name of the index to delete
|
|
Index string `json:"index"`
|
|
}
|
|
|
|
type DeleteIndexResponse struct {
|
|
}
|
|
|
|
type DeleteRequest struct {
|
|
// The ID of the record to delete
|
|
Id string `json:"id"`
|
|
// The index the record belongs to
|
|
Index string `json:"index"`
|
|
}
|
|
|
|
type DeleteResponse struct {
|
|
}
|
|
|
|
type Field struct {
|
|
// The name of the field. Use a `.` separator to define nested fields e.g. foo.bar
|
|
Name string `json:"name"`
|
|
// The type of the field - string, number
|
|
Type string `json:"type"`
|
|
}
|
|
|
|
type IndexRequest struct {
|
|
// The data to index
|
|
Data map[string]interface{} `json:"data"`
|
|
// Optional ID for the record
|
|
Id string `json:"id"`
|
|
// The index this record belongs to
|
|
Index string `json:"index"`
|
|
}
|
|
|
|
type IndexResponse struct {
|
|
// the indexed record
|
|
Record *Record `json:"record"`
|
|
}
|
|
|
|
type Record struct {
|
|
// The JSON contents of the record
|
|
Data map[string]interface{} `json:"data"`
|
|
// The ID for this record. If blank, one will be generated
|
|
Id string `json:"id"`
|
|
}
|
|
|
|
type SearchRequest struct {
|
|
// The index the record belongs to
|
|
Index string `json:"index"`
|
|
// The query. See docs for query language examples
|
|
Query string `json:"query"`
|
|
}
|
|
|
|
type SearchResponse struct {
|
|
// The matching records
|
|
Records []Record `json:"records"`
|
|
}
|