Skip to content

Commit

Permalink
refactor match, query and explaination out. initial add of explain
Browse files Browse the repository at this point in the history
  • Loading branch information
mattbaird committed Oct 23, 2012
1 parent 87eabc7 commit 3b89d51
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 16 deletions.
24 changes: 24 additions & 0 deletions api/baseResponse.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,30 @@ type Status struct {
Failed int `json:"failed"`
}

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

type Term struct {
Term string `json:"term"`
}

func (q Query) setQuery(query string) {
q.Query.Term = query
}

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

type Explaination struct {
Value float32 `json:"value"`
Description string `json:"description"`
Details []Explaination `json:"details,omitifempty"`
}

func Pretty(pretty bool) string {
prettyString := ""
if pretty == true {
Expand Down
33 changes: 33 additions & 0 deletions core/explain.go
Original file line number Diff line number Diff line change
@@ -1 +1,34 @@
package core

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

// The explain api computes a score explanation for a query and a specific document.
// This can give useful feedback whether a document matches or didn’t match a specific query.
// This feature is available from version 0.19.9 and up.
// see http://www.elasticsearch.org/guide/reference/api/explain.html
func Explain(pretty bool, index string, _type string, id string, query string) (api.Match, error) {
var url string
var retval api.Match
if len(_type) > 0 {
url = fmt.Sprintf("/%s/%s/_explain?%s", index, _type, api.Pretty(pretty))
} else {
url = fmt.Sprintf("/%s/_explain?%s", index, api.Pretty(pretty))
}
body, err := api.DoCommand("GET", 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
}
19 changes: 3 additions & 16 deletions core/percolate.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import (
// 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) {
func RegisterPercolate(pretty bool, index string, name string, query api.Query) (api.BaseResponse, error) {
var url string
var retval api.BaseResponse
url = fmt.Sprintf("/_percolator/%s/%s?%s", index, name, api.Pretty(pretty))
Expand All @@ -31,17 +31,9 @@ func RegisterPercolate(pretty bool, index string, name string, query Query) (api
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) {
func Percolate(pretty bool, index string, _type string, name string, doc string) (api.Match, error) {
var url string
var retval Match
var retval api.Match
url = fmt.Sprintf("/%s/%s/_percolate?%s", index, _type, api.Pretty(pretty))
body, err := api.DoCommand("GET", url, doc)
if err != nil {
Expand All @@ -57,8 +49,3 @@ func Percolate(pretty bool, index string, _type string, name string, doc string)
fmt.Println(body)
return retval, err
}

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

0 comments on commit 3b89d51

Please sign in to comment.