Skip to content

Commit

Permalink
initial implementation of percolate
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbaird committed Oct 23, 2012
1 parent add9d4a commit ef54e1f
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions core/percolate.go
Original file line number Diff line number Diff line change
@@ -1 +1,64 @@
package core

import (
"encoding/json"
"fmt"
"github.com/mattbaird/elastigo/api"
)

// The percolator allows to register queries against an index, and then send percolate requests which include a doc, and
// getting back the queries that match on that doc out of the set of registered queries.
// Think of it as the reverse operation of indexing and then searching. Instead of sending docs, indexing them,
// and then running queries. One sends queries, registers them, and then sends docs and finds out which queries
// match that doc.
// see http://www.elasticsearch.org/guide/reference/api/percolate.html
func RegisterPercolate(pretty bool, index string, name string, query Query) (api.BaseResponse, error) {
var url string
var retval api.BaseResponse
url = fmt.Sprintf("/_percolator/%s/%s?%s", index, name, api.Pretty(pretty))
body, err := api.DoCommand("PUT", url, query)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Println(body)
return retval, err
}

type Query struct {
Query Term `json:"query"`
}

type Term struct {
Term map[string]string `json:"term"`
}

func Percolate(pretty bool, index string, _type string, name string, doc string) (Match, error) {
var url string
var retval Match
url = fmt.Sprintf("/%s/%s/_percolate?%s", index, _type, api.Pretty(pretty))
body, err := api.DoCommand("GET", url, doc)
if err != nil {
return retval, err
}
if err == nil {
// marshall into json
jsonErr := json.Unmarshal([]byte(body), &retval)
if jsonErr != nil {
return retval, jsonErr
}
}
fmt.Println(body)
return retval, err
}

type Match struct {
OK bool `json:"ok"`
Matches []string `json:"matches"`
}

0 comments on commit ef54e1f

Please sign in to comment.