forked from mattbaird/elastigo
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
63 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |