Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Funder experiment #84

Open
wants to merge 4 commits into
base: elastic-2019-pagination
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 35 additions & 25 deletions src/cayenne/tasks/funder.clj
Original file line number Diff line number Diff line change
Expand Up @@ -135,10 +135,14 @@
(if (not-empty ancestors)
(build-hierarchy model (first ancestors) (hierarcy-node model funder-resource descendants child))
(hierarcy-node model funder-resource descendants child))))

(defn index-command
"Build an Elastic Search object from a resource in the context of a model.
The unique ID is taken from the Resource ID."
(defn generate-es-id
"Generate an elastic search id from a resource in the context of a model. The unique id is taken from the resource ID"
[funder-resource]
{:index {:_id (res->id funder-resource)}}
)

(defn generate-es-object
"Generates an Elastic Search object from a resource in the context of a model. The unique ID is taken from the Resource ID"
[model funder-resource]
(let [primary-name (-> model (get-labels funder-resource "prefLabel") first)
alt-names (-> model (get-labels funder-resource "altLabel"))
Expand All @@ -148,29 +152,35 @@
descendant-ids (->> descendants (map res->id) distinct sort)
level (-> ancestor-ids count (+ 1))
hierarchy (build-hierarchy model funder-resource (id-name model funder-resource))]
[{:index {:_id (res->id funder-resource)}}
{:doi (res->doi funder-resource)
:id (res->id funder-resource)
:primary-name primary-name
:name alt-names
:token (concat
(util/tokenize-name primary-name)
(flatten (map util/tokenize-name alt-names)))
:country (get-country-literal-name model funder-resource)
:parent (-> model (broader funder-resource) first res->doi)
:ancestor ancestor-ids
:level level
:child (distinct (map res->id (narrower model funder-resource)))
:descendant descendant-ids
:affiliated (distinct (map res->id (affiliated model funder-resource)))
:replaced-by (distinct (map res->id (replaced-by model funder-resource)))
:replaces (distinct (map res->id (replaces model funder-resource)))
:hierarchy-names (reduce
{:doi (res->doi funder-resource)
:id (res->id funder-resource)
:primary-name primary-name
:name alt-names
:token (concat
(util/tokenize-name primary-name)
(flatten (map util/tokenize-name alt-names)))
:country (get-country-literal-name model funder-resource)
:parent (-> model (broader funder-resource) first res->doi)
:ancestor ancestor-ids
:level level
:child (distinct (map res->id (narrower model funder-resource)))
:descendant descendant-ids
:affiliated (distinct (map res->id (affiliated model funder-resource)))
:replaced-by (distinct (map res->id (replaced-by model funder-resource)))
:replaces (distinct (map res->id (replaces model funder-resource)))
:hierarchy-names (reduce
(fn [m [k v]]
(assoc m k v))
(if (> level 1) {:more nil} {})
(partition 2 (util/get-all-in hierarchy [:id :name])))
:hierarchy hierarchy}]))
(if (> level 1) {:more nil} {})
(partition 2 (util/get-all-in hierarchy [:id :name])))
:hierarchy hierarchy}))

(defn index-command
"Build an Elastic Search object from a resource in the context of a model.
The unique ID is taken from the Resource ID."
[model funder-resource]
[(generate-es-id funder-resource) (generate-es-object model funder-resource)]
)

(defn index-funders
"Retrieve funder information RDF and index into Elastic."
Expand Down
12 changes: 12 additions & 0 deletions test/cayenne/funders_test.clj
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
(ns cayenne.funders-test
(:require [cayenne.api-fixture :refer [api-root api-get api-with]]
[clojure.java.io :refer [resource]]
[cayenne.rdf :as rdf]
[cayenne.conf :as conf]
[cayenne.tasks.funder :as funder]
[clojure.test :refer [use-fixtures deftest testing is]]))

(deftest ^:integration querying-funders
Expand All @@ -27,6 +30,15 @@
expected-response (read-string (slurp (resource (str "funders/" funder "-works.edn"))))]
(is (= expected-response response))))))

(deftest ^:unit check-index-command-output
(testing "index-command output which returns es-id and function generate-es-object yields same output except for the inclusion of elastic search index id"
(let [model (-> (java.net.URL. (conf/get-param [:location :cr-funder-registry])) rdf/document->model)
funders (first (->> model funder/find-funders (partition-all 5)))
with-es-id-output (->> funders (map (partial funder/index-command model)) flatten)
remove-id (filter #(not(contains? % :index)) with-es-id-output)
without-es-id-output (->> funders (map (partial funder/generate-es-object model)) flatten)]
(is (= remove-id without-es-id-output)))))

(use-fixtures
:once
(api-with
Expand Down