Code Monkey home page Code Monkey logo

tirexs's Introduction

https://github.com/roundscope/tirexs Build Status

tirexs

An Elixir based DSL for operating the ElasticSearch cluster related stuff, such as indexes, scoped queries and so on.

releases:

  • v0.5.1 tested on elasticsearch 1.4.1 elixir 1.0.2
  • v0.5.0 tested on elastisearch 1.1.1, elixir 0.13.1
  • v0.4 tested on elastisearch 0.90.3, elixir 0.12.5

Walk-through a code

Let's create an articles index:

import Tirexs.Bulk

require Tirexs.ElasticSearch
settings = Tirexs.ElasticSearch.config()

Tirexs.Bulk.store [index: "articles", refresh: true], settings do
  create id: 1, title: "One", tags: ["elixir"], type: "article"
  create id: 2, title: "Two", tags: ["elixir", "ruby"], type: "article"
  create id: 3, title: "Three", tags: ["java"], type: "article"
  create id: 4, title: "Four", tags: ["erlang"], type: "article"
end

Ability to customize the mapping for specific document type:

import Tirexs.Mapping

index = [index: "articles", type: "article"]
mappings do
  indexes "id", type: "string", index: "not_analyzed", include_in_all: false
  indexes "title", type: "string", boost: 2.0, analyzer: "snowball"
  indexes "tags",  type: "string", analyzer: "keyword"
  indexes "content", type: "string", analyzer: "snowball"
  indexes "authors", [type: "object"] do
    indexes "name", type: "string"
    indexes "nick_name", type: "string"
  end
end

{ :ok, status, body } = Tirexs.Mapping.create_resource(index)

Now, let's go further. We will be searching for articles whose title begins with letter “T”, sorted by title in descending order, filtering them for ones tagged “elixir”, and also retrieving some facets:

import Tirexs.Search

articles = search [index: "articles"] do
  query do
    string "title:T*"
  end

  filter do
    terms "tags", ["elixir", "ruby"]
  end

  facets do
    global_tags [global: true] do
      terms field: "tags"
    end

    current_tags do
      terms field: "tags"
    end
  end

  sort do
    [
      [title: "desc"]
    ]
  end
end

result = Tirexs.Query.create_resource(articles)

Enum.each result.hits, fn(item) ->
  IO.puts inspect(item)
  #=> [{"_index","articles"},{"_type","article"},{"_id","2"},{"_score",1.0},{"_source",[{"id",2}, {"title","Two"},{"tags",["elixir","r uby"]},{"type","article"}]}]
end

Let's display the global facets:

Enum.each result.facets["global_tags"]["terms"], fn(f) ->
  IO.puts "#{f["term"]}    #{f["count"]}"
end

#=> elixir  2
#=> ruby    1
#=> java    1
#=> erlang  1

Now, let's display the facets based on current query (notice that count for articles tagged with 'java' is included, even though it's not returned by our query; count for articles tagged 'erlang' is excluded, since they don't match the current query):

Enum.each result.facets["current_tags"]["terms"], fn(f) ->
  IO.puts "#{f["term"]}    #{f["count"]}"
end

#=> ruby    1
#=> java    1
#=> elixir  1

License

Tirexs source code is released under Apache 2 License. Check LICENSE and NOTICE files for more details.

tirexs's People

Contributors

behe avatar highbeats avatar nessamurmur avatar opakalex avatar pavloo avatar zabirauf avatar zatvobor 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.