Skip to content

GokselKUCUKSAHIN/es-query-builder

Repository files navigation

es-query-builder GoDoc Build Status Go Report Card Coverage Status

A Simple query builder for Elasticsearch

go get github.com/GokselKUCUKSAHIN/es-query-builder

TODOs

Project

  • improve README
    • add examples
    • add benchmark results
    • add go get command
    • add badges
  • setup github workflow
  • add benchmark
  • add missing tests
  • add makefile
  • setup linter

Builder fields

  • range
  • sort
  • nested
  • aggs
    • terms
    • multi_terms
    • nested
    • min (later TBD)
    • max (later TBD)
    • avg (later TBD)
    • sum (later TBD)
    • range (later TBD)
    • filter (later TBD)
    • filters (later TBD)
  • match
  • match_all
  • match_none
  • minimum_should_match
  • boost

Examples

🚧 Still under construction 🚧

{
  "query": {
    "bool": {
      "must": [
        {
          "bool": {
            "should": [
              {
                "term": {
                  "doc.id": "293"
                }
              },
              {
                "term": {
                  "file.fileId": "293"
                }
              }
            ]
          }
        }
      ],
      "filter": [
        {
          "terms": {
            "type": [
              "DOC",
              "FILE"
            ]
          }
        }
      ]
    }
  }
}

With pure Go

query := map[string]interface{}{
  "query": map[string]interface{}{
    "bool": map[string]interface{}{
      "must": []interface{}{
        map[string]interface{}{
          "bool": map[string]interface{}{
            "should": []interface{}{
              map[string]interface{}{
                "term": map[string]interface{}{
                  "doc.id": id,
                },
              },
              map[string]interface{}{
                "term": map[string]interface{}{
                  "file.fileId": id,
                },
              },
            },
          },
        },
      },
      "filter": []interface{}{
        map[string]interface{}{
          "terms": map[string]interface{}{
            "type": []string{
              "DOC", "FILE",
            },
          },
        },
      },
    },
  },
}
query := es.NewQuery(
    es.Bool().
        Must(
            es.Bool().
                Should(
                    es.Term("doc.id", id),
                    es.Term("file.fileId", id),
                ), 
        ).
        Filter(
            es.Terms("type", "DOC", "FILE"),
        ),
)