Skip to content
Browse files

Add field test.

  • Loading branch information...
1 parent c82703b commit a2ca314c011a324ccd979e8f6955f064615ae971 @drewr drewr committed Oct 11, 2011
Showing with 86 additions and 22 deletions.
  1. +2 −1 project.clj
  2. +12 −6 src/esperanto/index.clj
  3. +9 −3 src/esperanto/search.clj
  4. +49 −9 src/esperanto/transform.clj
  5. +14 −3 test/esperanto/test/search.clj
View
3 project.clj
@@ -7,5 +7,6 @@
:dependencies [[org.clojure/clojure "1.3.0"]
[org.clojure.contrib/repl-utils "1.3.0-alpha4"]
[org.elasticsearch/elasticsearch "0.17.6"]
- [cheshire "2.0.2"]]
+ [cheshire "2.0.2"]
+ [slingshot "0.2.3"]]
:aot [esperanto.interactive])
View
18 src/esperanto/index.clj
@@ -1,15 +1,21 @@
(ns esperanto.index
(:use [esperanto.action :only [execute]]
- [esperanto.search :only [index-seq]])
+ [esperanto.search :only [index-seq]]
+ [slingshot.core])
(:require [cheshire.core :as json]))
(defn make-index-request
([client idx source]
- (make-index-request client idx (or (get source :_type)
- (get source "_type")
- (get source :type)
- (get source "type"))
- source))
+ (let [type (or (get source :_type)
+ (get source "_type")
+ (get source :type)
+ (get source "type"))]
+ (if type
+ (make-index-request client idx type source)
+ (throw+ ::ENEEDTYPE
+ (with-out-str
+ (binding [*print-length* 3]
+ (print "document needs to have a type:" source)))))))
([client idx type source]
(let [req (-> client
(.prepareIndex idx type)
View
12 src/esperanto/search.clj
@@ -35,12 +35,18 @@
([client idx]
(search client idx {:match_all {}}))
([client idx query]
- @(execute (make-search-request client idx query))))
+ @(execute (make-search-request client idx query)))
+ ([client idx query fields]
+ (let [req (make-search-request client idx query)]
+ (doseq [f fields]
+ (.addField req f))
+ @(execute req))))
(defn searchq
([client idx query]
- (search client idx {:query_string
- {:query query}})))
+ (searchq client idx query nil))
+ ([client idx query fields]
+ (search client idx {:query_string {:query query}} fields)))
(defn count
([client idx]
View
58 src/esperanto/transform.clj
@@ -476,15 +476,55 @@
[56] writeTo : void (StreamOutput,StreamContext)
"
[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))})))
+ (let [sas (.sourceAsString hit)
+ src (when sas (json/parse-string sas :kw))]
+ (with-meta (merge {:id (.getId hit)} src)
+ (merge
+ {
+ :fields (reduce (fn [acc [k v]] (merge acc (transform v)))
+ {} (.getFields hit))
+ :filters (seq (.getMatchedFilters hit))
+ :hl-fields (.getHighlightFields hit)
+ :index (.getIndex hit)
+ :node (-> hit .getShard .getNodeId)
+ :score (.getScore hit)
+ :shard (-> hit .getShard .getShardId)
+ :sort-vals (seq (.getSortValues hit))
+ :source (when src :present)
+ :type (.getType hit)
+ }))))
+
+(deftransform org.elasticsearch.search.internal.InternalSearchHitField
+ "=== public org.elasticsearch.search.internal.InternalSearchHitField ===
+ [ 0] static readSearchHitField : InternalSearchHitField (StreamInput)
+ [ 1] <init> (String,List)
+ [ 2] equals : boolean (Object)
+ [ 3] getClass : Class ()
+ [ 4] getName : String ()
+ [ 5] getValue : Object ()
+ [ 6] getValues : List ()
+ [ 7] hashCode : int ()
+ [ 8] iterator : Iterator ()
+ [ 9] name : String ()
+ [10] notify : void ()
+ [11] notifyAll : void ()
+ [12] readFrom : void (StreamInput)
+ [13] toString : String ()
+ [14] value : Object ()
+ [15] values : List ()
+ [16] wait : void ()
+ [17] wait : void (long)
+ [18] wait : void (long,int)
+ [19] writeTo : void (StreamOutput)
+ "
+ [obj]
+
+
+ {(.getName obj)
+ (let [vs (.getValues obj)]
+ (if (= 1 (count vs))
+ (first vs)
+ (seq vs)))})
(deftransform org.elasticsearch.action.index.IndexResponse
"=== public org.elasticsearch.action.index.IndexResponse ===
View
17 test/esperanto/test/search.clj
@@ -13,13 +13,12 @@
(def index "twitter")
(def mapping {:tweet
- {:_source {:enabled false}
+ {:_source {:enabled true}
:properties
{:text
{:store "yes"
:type "string"
- :index "not_analyzed"
- }}}})
+ :index "analyzed"}}}})
(def doc {:type "tweet"
:text "The quick brown fox jumps over the lazy dog"})
@@ -99,3 +98,15 @@
(is (= 1 (count client index "quick")))
(is (= 0 (count client index "foo"))))
+(deftest t-fields
+ (index-doc client index doc)
+ (refresh client index)
+ (is (= {"text" "The quick brown fox jumps over the lazy dog"}
+ (-> (searchq client index "quick" ["text"]) first meta :fields)))
+ (is (= {"text" "The quick brown fox jumps over the lazy dog"}
+ (-> (searchq client index "quick" ["*"]) first meta :fields)))
+ (is (= {}
+ (-> (searchq client index "quick" ["NOEXIST"]) first meta :fields)))
+ (is (not (-> (searchq client index "quick" ["text"]) first meta :source)))
+ (is (not (-> (searchq client index "quick" ["*"]) first meta :source)))
+ (is (= :present (-> (searchq client index "quick") first meta :source))))

0 comments on commit a2ca314

Please sign in to comment.
Something went wrong with that request. Please try again.