Clojure弹性搜索API Elastisch不返回结果或文档

时间:2013-03-16 16:25:49

标签: clojure elasticsearch elastisch

我正在尝试使用clojure弹性搜索API Elastisch

我正在按照文档中给出的演示代码进行操作 我正在为以下代码获取文档/索引创建输出

(defn demo-search-connect []
  (esr/connect! "http://127.0.0.1:9200")
  (let [mapping-types {:person {:properties {:username   {:type "string" :store "yes"}
                                         :first-name {:type "string" :store "yes"}
                                         :last-name  {:type "string"}
                                         :age        {:type "integer"}
                                         :title      {:type "string" :analyzer
                                          "snowball"}
                                         :planet     {:type "string"}
                                         :biography  {:type "string" :analyzer   "snowball" :term_vector "with_positions_offsets"}}}}
    doc {:username "happyjoe" :first-name "Joe" :last-name "Smith" :age 30 :title "Teh Boss" :planet "Earth" :biography "N/A"}]

 (esi/create "myapp2_development" :mappings mapping-types)
;; adds a document to the index, id is automatically generated by ElasticSearch
;= {:ok true, :_index people, :_type person, :_id "2vr8sP-LTRWhSKOxyWOi_Q", :_version 1}
(println (esd/create "myapp2_development" :person doc :settings {"number_of_shards" 1}))
))

;Supposed to return an output for the search query 
(defn demo-search-index [] 
  (esr/connect! "http://127.0.0.1:9200")
  (esd/search "myapp2_development" "person" :query {:term {:last_name "Smith"}})
  )

;Supposed to return the document with the given id
(defn get-document [id]
  (esr/connect! "http://127.0.0.1:9200")
  (esd/get "myapp2_development" "person" id)
  )

我得到第一个函数的输出:

{:ok true, :_index myapp2_development, :_type :person, :_id GzNdzrqhQECQDlkSbq-GHA, :_version 1}

从输出中我相信文档正在正确索引

问题是第二个和第三个函数返回:

{:took 153, :timed_out false, :_shards {:total 5, :successful 5, :failed 0}, :hits {:total 0, :max_score nil, :hits []}}
分别为

nil

我在这里缺少什么?

P.S:我是clojure和弹性搜索的新手

1 个答案:

答案 0 :(得分:1)

esd/get失败,因为您的映射类型为:person而不是"person"(关键字与字符串)。

您的代码esd/search存在同样的问题,但另外您应该将last_name更改为last-name,将"Smith"更改为"smith",一切都应该有效