Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add mapping support.

  • Loading branch information...
commit 05334a50a08cf0a77294a8b3b70d3ec1474439ce 1 parent eb72bdc
@drewr drewr authored
View
27 src/esperanto/admin/indices.clj
@@ -1,4 +1,5 @@
(ns esperanto.admin.indices
+ (:require [cheshire.core :as json])
(:use [esperanto.action :only [execute]]
[esperanto.admin.cluster :only [wait-for]])
(:import (org.elasticsearch.client Requests)
@@ -8,9 +9,15 @@
([client idx]
(make-index-creation client idx {}))
([client idx settings]
- (-> client .admin .indices
- (.prepareCreate idx)
- (.setSettings settings))))
+ (make-index-creation client idx {} {}))
+ ([client idx settings mapping]
+ (let [req (-> client .admin .indices
+ (.prepareCreate idx)
+ (.setSettings settings))]
+ (when mapping
+ (doseq [[type obj] mapping]
+ (.addMapping req (name type) (json/encode obj))))
+ req)))
(defn make-refresh-request [client & indices]
(-> client .admin .indices
@@ -28,7 +35,11 @@
([client idx]
(create client idx {}))
([client idx settings]
- @(execute (make-index-creation client idx settings))))
+ @(execute
+ (make-index-creation client idx settings)))
+ ([client idx settings mapping]
+ @(execute
+ (make-index-creation client idx settings mapping))))
(defn refresh [client & indices]
@(execute (apply make-refresh-request client indices)))
@@ -39,11 +50,13 @@
(defn delete [client & indices]
@(execute (apply make-delete-request client indices)))
-(defn index-fixture [node idx]
+(defn index-fixture [node idx & [mapping]]
(fn [f]
(when-let [istat (status (.client node) idx)]
(delete (.client node) idx))
- (create (.client node) idx {"number_of_shards" "1"
- "number_of_replicas" "0"})
+ (create (.client node) idx
+ {"number_of_shards" "1"
+ "number_of_replicas" "0"}
+ mapping)
(f)))
View
16 src/esperanto/search.clj
@@ -25,13 +25,15 @@
(.setQuery (QueryBuilders/queryString query))))
(defn hit->clj [hit]
- (with-meta (merge {:id (.getId hit)}
- (json/parse-string
- (.sourceAsString hit) :kw))
- {:index (.getIndex hit)
- :node (-> hit .getShard .getNodeId)
- :shard (-> hit .getShard .getShardId)
- :sort-vals (seq (.getSortValues hit))}))
+ (let [src (.sourceAsString hit)]
+ (with-meta (merge {:id (.getId hit)}
+ (if src
+ (json/parse-string src :kw)
+ {:ERROR "_source is not enabled"}))
+ {:index (.getIndex hit)
+ :node (-> hit .getShard .getNodeId)
+ :shard (-> hit .getShard .getShardId)
+ :sort-vals (seq (.getSortValues hit))})))
(defn search->clj [r]
(with-meta (map hit->clj (.getHits r))
View
14 test/esperanto/test/search.clj
@@ -12,6 +12,16 @@
(def index "twitter")
+(def mapping {:tweet
+ {:tweet
+ {:_source {:enabled false}
+ :properties
+ {:text
+ {:store "yes"
+ :type "string"
+ :index "not_analyzed"
+ }}}}})
+
(def doc {:type "tweet"
:text "The quick brown fox jumps over the lazy dog"})
@@ -27,12 +37,12 @@
(merge doc {:id (str i)})))
(use-fixtures :once (node-fixture node))
-(use-fixtures :each (index-fixture node index))
+(use-fixtures :each (index-fixture node index mapping))
(deftest t-index-single
(index-doc client index doc)
(refresh client index)
- (is (= 1 (-> (searchq client index "quick") meta :total))))
+ (is (= 1 (-> (searchq client index "brown") meta :total))))
(deftest t-index-double
(index-doc client index doc)
Please sign in to comment.
Something went wrong with that request. Please try again.