Skip to content

Safely create complex ES Search Queries via the Query DSL in golang

License

Notifications You must be signed in to change notification settings

mottaquikarim/esquerydsl

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

build GoDoc Go Report Card Maintainability Test Coverage

Structs and marshal-ers that help wrangle writing elastic search queries using the ES query DSL spec

Installation

go get github.com/mottaquikarim/esquerydsl

Usage

(You can copy paste the snippet below into playground and see for yourself)

package main

import (
	"encoding/json"
	"fmt"

	"github.com/mottaquikarim/esquerydsl"
)

func main() {
	body, _ := json.Marshal(esquerydsl.QueryDoc{
		Index: "some_index",
		Sort:  []map[string]string{map[string]string{"id": "asc"}},
		And: []esquerydsl.QueryItem{
			esquerydsl.QueryItem{
				Field: "some_index_id",
				Value: "some-long-key-id-value",
				Type:  esquerydsl.Match,
			},
		},
	})
	fmt.Println(string(body))
	// {"query":{"bool":{"must":[{"match":{"some_index_id":"some-long-key-id-value"}}]}},"sort":[{"id":"asc"}]}
}

(Please find additional examples in the unit tests)

MultiSearch Support

package main

import (
	"fmt"

	"github.com/mottaquikarim/esquerydsl"
)

func main() {
	doc, _ := esquerydsl.MultiSearchDoc([]esquerydsl.QueryDoc{
		esquerydsl.QueryDoc{
			Index: "index1",
			And: []esquerydsl.QueryItem{
				esquerydsl.QueryItem{
					Field: "user.id",
					Value: "kimchy!",
					Type:  esquerydsl.QueryString,
				},
			},
		},
		esquerydsl.QueryDoc{
			Index: "index2",
			And: []esquerydsl.QueryItem{
				esquerydsl.QueryItem{
					Field: "some_index_id",
					Value: "some-long-key-id-value",
					Type:  esquerydsl.Match,
				},
			},
		},
	})
	fmt.Println(doc)
	// OUTPUT:

	// {"index":"index1"}
	// {"query":{"bool":{"must":[{"query_string":{"analyze_wildcard":true,"fields":["user.id"],"query":"kimchy\\!"}}]}}}
	// {"index":"index2"}
	// {"query":{"bool":{"must":[{"match":{"some_index_id":"some-long-key-id-value"}}]}}}

	// ...
	// eventually, pass this in like so:

	// request := esapi.MsearchRequest{
	//	Body: strings.NewReader(doc),
	// }

}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

Run Tests

make test

Format

make fmt

Lint

make lint

License

MIT

About

Safely create complex ES Search Queries via the Query DSL in golang

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published