Giter VIP home page Giter VIP logo

archia's Introduction

Archia

Archia is an Elastic Search Query DSL for Kotlin.

To reduce learning curve, Archia tries to mimic Elasticsearch's own query language as close as possible. This means you can take advantage of any existing knowledge you have on Elasticsearch. Even if you didn't have much Elasticsearch knowledge, Elasticsearch queries you have googled should be easily convertible into Archia's DSL.

Note: This project's first and upcoming release is still work-in-progress. Contributions are welcome.

Why do we need this DSL?

Off the top of my head:

  • Type-safety. Most of the values are type-checked.
  • Compile-time structural validation. You can't just create any random JSON objects and pretend it's a valid query.
  • Leverage the power of the host-language. Archia code is Kotlin code. So you can reuse all of Kotlin's high-level abstractions and advanced language features to construct your queries.

Running the tests

You can run the tests using the Gradle test command:

gradle clean test -i

If you are using Intellij, you can also just run the tests from within the IDE itself.

A Quick Example:

import main.kotlin.archia.interpreter.*

val result = interpret {
    query {
        bool {
            must {
                term { "user" to "melvic" }
            }
            filter { term { "tag" to "tech"} }
            mustNot {
                range {
                    "age" { gte = 10.es(); lte = 20.es() }
                }
            }
            should {
                term { "message" to "go beyond plus ultra" }
                term {
                    "tag" {
                        value = "elasticsearch"
                        boost = 1.4
                    }
                }
            }
            minimumShouldMatch = 1.es()
            boost = 1.0
        }
    }
}

There is also a shorter form, evalQuery, that asks for a query and wraps it in a interpret { query { ... }}.

Here's another example showing evalQuery (along with other features) in action:

val output = evalQuery {
    functionScore {
        query { matchAll {} }
        boost = "5"
        functions(
            {
                filter { term { "test" to "bar" } }
                randomScore {  }
                weight = 23
            },
            {
                filter { term { "test" to "cat" } }
                weight = 42
            }
        )
        maxBoost = 42
        scoreMode = ScoreMode.MAX
        boostMode = BoostMode.MULTIPLY
        minScore = 42
    }
}

It also shows a different way of writing arrays of query clauses.

You are expected to handle errors, if any:

val output = when (result) {
    is Failed -> result.show()      
    is Success<*> -> result.value()     
}

A successful query produces a JSON object, which you can then transform into various forms of your liking.

Transformers

So far, the only built-in transformer converts the JSON object into its string representation. Using it requires the application of the mapTo method to the JsonStringOutput transformer:

...
import main.kotlin.archia.output.*

val result = ...
val output = when (result) {
    ...
}
val jsonString = output.mapTo(JsonStringOutput)
println(jsonString)

For instance, the first example would produce the following JSON string:

{
   "query": {
      "bool": {
         "must": {
            "term": { "user":"melvic" }
         },
         "should": [
            { "term": { "message": "go beyond plus ultra" } },
            {
               "term": {
                  "tag": {
                     "value": "elasticsearch",
                     "boost": 1.4
                  }
               }
            }
         ],
         "filter": {
            "term": { "tag": "tech" }
         },
         "must_not": {
            "range": {
               "age": {
                  "gte": 10,
                  "lte": 20
               }
            }
         },
         "minimum_should_match": 1,
         "boost": 1.0
      }
   }
}

(The resulting JSON string is manually prettified. At the time of this writing, prettification of the string output isn't supported yet, but soon will be.)

The following are the currently supported queries:

Compound Queries:

  • Bool Query
  • Boosting Query
  • Constant Score Query
  • Disjunction Max Query
  • Function Score Query

Leaf Queries

Full Text Queries

  • Common Terms Query
  • Intervals Query
  • Match Bool Prefix Query
  • Match Phrase Prefix Query
  • Match Phrase Query
  • Match Query
  • Multi Match Query
  • Query String Query
  • Simple Query String Query

Geo & Shape Queries

  • Geo Bounding Box Query
  • Geo Distance Query
  • Geo Point
  • Geo Polygon Query
  • Geo Shape Query
  • Shape Query

Joining Queries

  • Nested Query
  • Has Child Query
  • Has Parent Query
  • Parent Id Query

Term-level Queries

  • Exists Query
  • Fuzzy Query
  • IDs Query
  • Prefix Query
  • Range Query
  • Regexp Query
  • Term Query
  • Terms Query
  • Terms Set Query
  • Type Query
  • Wildcard Query

Others

  • Match All Query
  • Match None Query

Future features might add support for transforming the result into actual queries that connect to the elastic engine. However, this isn't currently a priority.

archia's People

Contributors

melvic-ybanez avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.