├── .gitignore ├── .travis.yml ├── CONTRIBUTING.md ├── ChangeLog.md ├── README.md ├── aplv-20.html ├── docs └── guides │ ├── README.md │ ├── aggregation.md │ ├── community.md │ ├── facets.md │ ├── getting_started.md │ ├── indexing.md │ ├── percolation.md │ ├── querying.md │ └── suggestions.md ├── eplv-10.html ├── project.clj ├── resources └── config │ └── elasticsearch.yml ├── src └── clojurewerkz │ └── elastisch │ ├── aggregation.clj │ ├── common │ └── bulk.clj │ ├── escape.clj │ ├── native.clj │ ├── native │ ├── admin.clj │ ├── bulk.clj │ ├── conversion.clj │ ├── conversion_stats.clj │ ├── document.clj │ ├── index.clj │ ├── multi.clj │ ├── percolation.clj │ └── response.clj │ ├── query.clj │ ├── rest.clj │ └── rest │ ├── admin.clj │ ├── bulk.clj │ ├── document.clj │ ├── index.clj │ ├── multi.clj │ ├── percolation.clj │ ├── response.clj │ └── utils.clj └── test └── clojurewerkz └── elastisch ├── escape_test.clj ├── fixtures.clj ├── internal ├── rest_test.clj └── utils_test.clj ├── native_api ├── aggregations │ ├── avg_aggregation_test.clj │ ├── cardinality_aggregation_test.clj │ ├── date_histogram_aggregation_test.clj │ ├── date_range_aggregation_test.clj │ ├── extended_stats_aggregation_test.clj │ ├── histogram_aggregation_test.clj │ ├── max_aggregation_test.clj │ ├── min_aggregation_test.clj │ ├── missing_aggregation_test.clj │ ├── percentiles_aggregation_test.clj │ ├── range_aggregation_test.clj │ ├── stats_aggregation_test.clj │ ├── sum_aggregation_test.clj │ ├── terms_aggregation_test.clj │ ├── top_hits_aggregations_test.clj │ └── value_count_aggregation_test.clj ├── bulk_test.clj ├── conversion_stats_test.clj ├── count_test.clj ├── delete_test.clj ├── filtering_test.clj ├── get_test.clj ├── highlighting_test.clj ├── indexing_test.clj ├── indices_settings_test.clj ├── indices_test.clj ├── mappings_test.clj ├── multi_test.clj ├── percolation_test.clj ├── queries │ ├── bool_query_test.clj │ ├── filtered_query_test.clj │ ├── fuzzy_query_test.clj │ ├── ids_query_test.clj │ ├── match_all_query_test.clj │ ├── mlt_query_test.clj │ ├── prefix_query_test.clj │ ├── query_string_query_test.clj │ ├── range_query_test.clj │ ├── term_query_test.clj │ ├── type_query_test.clj │ └── wildcard_query_test.clj ├── search_scroll_test.clj ├── search_test.clj ├── suggest_test.clj ├── update_test.clj └── upsert_test.clj ├── query_test.clj ├── rest_api ├── aggregations │ ├── avg_aggregation_test.clj │ ├── cardinality_aggregation_test.clj │ ├── date_histogram_aggregation_test.clj │ ├── date_range_aggregation_test.clj │ ├── extended_stats_aggregation_test.clj │ ├── histogram_aggregation_test.clj │ ├── max_aggregation_test.clj │ ├── min_aggregation_test.clj │ ├── missing_aggregation_test.clj │ ├── percentiles_aggregation_test.clj │ ├── range_aggregation_test.clj │ ├── stats_aggregation_test.clj │ ├── sum_aggregation_test.clj │ ├── terms_aggregation_test.clj │ └── value_count_aggregation_test.clj ├── analyze_test.clj ├── bulk_test.clj ├── cluster_health_test.clj ├── cluster_state_test.clj ├── count_test.clj ├── delete_test.clj ├── document_test.clj ├── filtering_test.clj ├── get_test.clj ├── highlighting_test.clj ├── indexing_test.clj ├── indices_test.clj ├── mappings_test.clj ├── more_like_this_test.clj ├── multi_test.clj ├── nodes_info_test.clj ├── nodes_stats_test.clj ├── percolation_test.clj ├── queries │ ├── bool_query_test.clj │ ├── filtered_query_test.clj │ ├── fuzzy_query_test.clj │ ├── ids_query_test.clj │ ├── match_all_query_test.clj │ ├── mlt_query_test.clj │ ├── prefix_query_test.clj │ ├── query_string_query_test.clj │ ├── range_query_test.clj │ ├── span_query_test.clj │ ├── term_query_test.clj │ ├── type_query_test.clj │ └── wildcard_query_test.clj ├── response_test.clj ├── search_scroll_test.clj ├── search_test.clj └── update_test.clj └── test └── helpers.clj /.gitignore: -------------------------------------------------------------------------------- 1 | pom.xml* 2 | *jar 3 | /lib/ 4 | /classes/ 5 | *~ 6 | debug/* 7 | doc/* 8 | deploy.docs.sh 9 | .lein-* 10 | .nrepl-* 11 | data/* 12 | target 13 | /.idea/ 14 | elastisch.iml -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | jdk: 2 | - oraclejdk8 3 | language: clojure 4 | lein: lein 5 | sudo: required 6 | services: 7 | - docker 8 | 9 | env: 10 | - ES_VERSION="2.3.1" 11 | 12 | before_install: 13 | - docker pull elasticsearch:2.3.1 14 | 15 | install: 16 | - docker run -d -p 127.0.0.1:9200:9200 -p 127.0.0.1:9300:9300 --name="elastisch-test" -v "$PWD/resources/config/elasticsearch.yml":/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:2.3.1 17 | - lein deps 18 | 19 | before_script: 20 | - sleep 10 21 | 22 | script: 23 | - docker logs elastisch-test 24 | - curl -vvvv http://127.0.0.1:9200/_stats 25 | - lein all test :ci 26 | 27 | after_script: 28 | - docker stop elastisch-test 29 | - docker rm elastisch-test 30 | -------------------------------------------------------------------------------- /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Pre-requisites 2 | 3 | The project uses [Leiningen 2](https://leiningen.org) and requires Elasticsearch `1.4.x` or more recent to be running 4 | locally. 5 | 6 | `script.disable_dynamic` must be `false` for scripting tests to pass. To set this, add the following to your `elasticsearch.yml` file: 7 | 8 | script: 9 | disable_dynamic: false 10 | 11 | Make 12 | sure you have those two installed and then run tests against all supported Clojure versions using 13 | 14 | lein all test 15 | 16 | 17 | #### Testing with docker 18 | 19 | * pull [Elasticsearch](https://hub.docker.com/_/elasticsearch/) image 20 | 21 | ``` 22 | $> docker pull elasticsearch:2.0.2 23 | ``` 24 | nb! Tag must match with Elasticsearch version in the `project.clj` 25 | 26 | * build and run instance with custom config file 27 | 28 | The custom file in the `resources/config/elasticsearch.yml` has all the required settings for full-scale test activated, so you dont have 29 | manually tweak them. 30 | 31 | ``` 32 | docker run -d -p 9200:9200 -p 9300:9300 --name="elastisch-test" \ 33 | -v "$PWD/resources/config":/usr/share/elasticsearch/config \ 34 | elasticsearch:2.0.2 \ 35 | ``` 36 | 37 | * set environment variables 38 | 39 | ``` 40 | $> export ES_URL="http://192.168.99.100:9200" ;;for rest-client 41 | $> export ES_CLUSTER_NAME="elasticsearch" 42 | $> export ES_CLUSTER_HOST="192.168.99.100" 43 | ``` 44 | 45 | * run tests 46 | 47 | 48 | ``` 49 | lein with-profile dev,1.8 test :only clojurewerkz.elastisch.native-api.count-test 50 | lein with-profile dev,1.8 test :native ;;only native client 51 | lein with-profile dev,1.8 test ;;all the tests with clj1.8 52 | ``` 53 | 54 | ## Pull Requests 55 | 56 | Then create a branch and make your changes on it. Once you are done with your changes and all 57 | tests pass, write a [good, detailed commit message](http://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html) and submit a pull request on GitHub. 58 | 59 | Don't forget to add your changes to `ChangeLog.md` and credit yourself! 60 | 61 | -------------------------------------------------------------------------------- /docs/guides/README.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Elastisch, a minimalistic Clojure client for ElasticSearch: all documentation guides" 3 | layout: article 4 | --- 5 | 6 | ## Guide list 7 | 8 | Elastisch documentation is organized as a number of guides, covering all kinds of topics. 9 | 10 | We recommend that you read these guides, if possible, in this order: 11 | 12 | 13 | ### [Getting started](./getting_started.md) 14 | 15 | An overview of Elastisch with a quick tutorial that helps you to get started with it. It should take about 16 | 10 minutes to read and study the provided code examples 17 | 18 | 19 | ### [Mappings and Indexing](./indexing.md) 20 | 21 | This guide covers: 22 | 23 | * What are indexes 24 | * What are mappings 25 | * How to index documents 26 | * How to define mappings with Elastisch 27 | * Advanced mapping settings 28 | * Different kinds of analyzers 29 | * Operations on indexes: refresh, optimize, snapshot, etc 30 | * Other topics related to indexing 31 | 32 | 33 | ### [Querying](./querying.md) 34 | 35 | This guide covers: 36 | 37 | * How to perform search queries with Elastisch 38 | * Various kinds of queries 39 | * Various kinds of filters 40 | * Highlighting of search results 41 | * Other topics related to querying 42 | 43 | ### [Aggregation](./aggregation.md) 44 | 45 | This guide covers: 46 | 47 | * An overview of aggregation features 48 | * How to perform aggregation queries 49 | 50 | ### [Facets](./facets.md) (Faceted Search) 51 | 52 | This guide covers: 53 | 54 | * An overview of faceted search 55 | * How to perform queries with facets 56 | * Other topics related to facets 57 | 58 | ### [Suggestions](./suggestions.md) (Autocomplete) 59 | 60 | This guide covers: 61 | 62 | * A simple term completion 63 | * A term completion with context 64 | * A fuzzy completion 65 | * A fuzzy completion with context 66 | 67 | 68 | ### [Percolation](./percolation.md) 69 | 70 | This guide covers: 71 | 72 | * What is percolation 73 | * How to register a query for percolation 74 | * How to perform a query percolation 75 | * How to unregister a query from percolation 76 | 77 | 78 | ### [Routing and Distribution](./distribution.md) 79 | 80 | TBD 81 | -------------------------------------------------------------------------------- /docs/guides/community.md: -------------------------------------------------------------------------------- 1 | --- 2 | title: "Elastisch, a minimalistic Clojure client for ElasticSearch: Community" 3 | layout: article 4 | --- 5 | 6 | ## Mailing List 7 | 8 | [Elastisch has a mailing list](https://groups.google.com/forum/?fromgroups#!forum/clojure-elasticsearch). Feel free to join it and ask any questions you may have. 9 | 10 | 11 | ## IRC 12 | 13 | Feel free to stop by `#clojurewerkz` on `irc.freenode.net`. There is a [Web IRC client](http://webchat.freenode.net/) 14 | if you need one. 15 | 16 | 17 | ## News & Announcements on Twitter 18 | 19 | To subscribe for announcements of releases, important changes and so on, please follow [@ClojureWerkz](https://twitter.com/clojurewerkz) on Twitter. 20 | 21 | 22 | ## What is ClojureWerkz? 23 | 24 | Elastisch is part of the group of libraries known as ClojureWerkz, together with 25 | [Monger](http://clojuremongodb.info), [Welle](http://clojureriak.info), [Quartzite](http://clojurequartz.info), [Neocons](https://github.com/michaelklishin/neocons), [Langohr](https://github.com/michaelklishin/langohr), [Validateur](https://github.com/michaelklishin/validateur), [Spyglass](https://github.com/clojurewerkz/spyglass) and several others. 26 | 27 | 28 | ## Reporting Issues 29 | 30 | If you find a bug, poor default, missing feature or find any part of the API inconvenient, please [file an issue](http://github.com/clojurewerkz/elastisch/issues) on Github. 31 | If possible, try to explain what behavior you expected and why. Bonus points for contributing failing test cases. 32 | 33 | 34 | ## Contributing 35 | 36 | Elastisch uses [Leiningen 2](https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md). Make sure you have it installed and then run tests against 37 | supported Clojure versions using 38 | 39 | lein2 all test 40 | 41 | Then create a branch and make your changes on it. Once you are done with your changes and all tests pass, submit a pull request 42 | on Github. 43 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clojurewerkz/elastisch "5.0.0-SNAPSHOT" 2 | :url "http://clojureelasticsearch.info" 3 | :description "Minimalistic fully featured well documented Clojure Elasticsearch client" 4 | :license {:name "Eclipse Public License"} 5 | :dependencies [[org.clojure/clojure "1.9.0"] 6 | [cheshire "5.6.1"] 7 | [clj-http "2.2.0" :exclusions [org.clojure/clojure]] 8 | [clojurewerkz/support "1.1.0" :exclusions [com.google.guava/guava]] 9 | ;; used by the native client 10 | [org.elasticsearch/elasticsearch "2.4.6"]] 11 | :min-lein-version "2.5.1" 12 | :profiles {:dev {:resource-paths ["test/resources"] 13 | :dependencies [[clj-time "0.11.0" :exclusions [org.clojure/clojure]]] 14 | :plugins [[lein-codox "0.9.0"] 15 | [jonase/eastwood "0.2.3"]] 16 | :codox {:source-paths ["src"] 17 | :metadata {:doc/format :markdown}}} 18 | ;; this version of clj-http depends on HTTPCore 4.2.x which 19 | ;; some projects (e.g. using Spring's RestTemplate) can rely on, 20 | ;; so we test for compatibility with it. MK. 21 | :cljhttp076 {:dependencies [[clj-http "0.7.6"]]} 22 | :1.8 {:dependencies [[org.clojure/clojure "1.8.0"]]} 23 | :1.10 {:dependencies [[org.clojure/clojure "1.10.0"]]} 24 | :master {:dependencies [[org.clojure/clojure "1.10.0-master-SNAPSHOT"]]}} 25 | :aliases {"all" ["with-profile" "dev:1.8,dev:1.10,dev:master,dev:cljhttp076,dev"]} ;try dev, then dev+1.8, then dev+master 26 | :repositories {"sonatype" {:url "http://oss.sonatype.org/content/repositories/releases" 27 | :snapshots false 28 | :releases {:checksum :fail :update :always}} 29 | "sonatype-snapshots" {:url "http://oss.sonatype.org/content/repositories/snapshots" 30 | :snapshots true 31 | :releases {:checksum :fail :update :always}}} 32 | :global-vars {*warn-on-reflection* true} 33 | :test-selectors {:focus :focus 34 | :indexing :indexing 35 | :query :query 36 | :facets :facets 37 | :percolation :percolation 38 | :scroll :scroll 39 | :snapshots :snapshots 40 | :native :native 41 | :rest :rest 42 | :version-dependent :version-dependent 43 | :all (constantly true) 44 | :default (fn [m] (not (or (:version-dependent m) 45 | (:scripting m) 46 | (:plugin m)))) 47 | :ci (fn [m] (and (not (:native m)) 48 | (not (:version-dependent m))))} 49 | :mailing-list {:name "clojure-elasticsearch" 50 | :archive "https://groups.google.com/group/clojure-elasticsearch" 51 | :post "clojure-elasticsearch@googlegroups.com"}) 52 | -------------------------------------------------------------------------------- /resources/config/elasticsearch.yml: -------------------------------------------------------------------------------- 1 | cluster: 2 | name: "elasticsearch" 3 | node: 4 | name: "elastisch-test" 5 | 6 | script.inline: on 7 | script.indexed: on 8 | 9 | network: 10 | bind_host: "0.0.0.0" 11 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/aggregation.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.aggregation 16 | "Convenience functions that build various aggregation types. 17 | 18 | All functions return maps and are completely optional (but recommended)." 19 | (:refer-clojure :exclude [min max sum filter range])) 20 | 21 | (defn min 22 | [field] 23 | {"min" {"field" field}}) 24 | 25 | (defn max 26 | [field] 27 | {"max" {"field" field}}) 28 | 29 | (defn sum 30 | [field] 31 | {"sum" {"field" field}}) 32 | 33 | (defn avg 34 | [field] 35 | {"avg" {"field" field}}) 36 | 37 | (defn stats 38 | [field] 39 | {"stats" {"field" field}}) 40 | 41 | (defn extended-stats 42 | [field] 43 | {"extended_stats" {"field" field}}) 44 | 45 | (defn value-count 46 | [field] 47 | {"value_count" {"field" field}}) 48 | 49 | (defn percentiles 50 | [field] 51 | {"percentiles" {"field" field}}) 52 | 53 | (defn global 54 | [] 55 | {}) 56 | 57 | (defn cardinality 58 | ([field] 59 | {"cardinality" {"field" field}}) 60 | ([field opts] 61 | {"cardinality" (merge {"field" field} opts)})) 62 | 63 | (defn filter 64 | [opts] 65 | {"filter" opts}) 66 | 67 | (defn missing 68 | [field] 69 | {"missing" {"field" field}}) 70 | 71 | (defn nested 72 | [opts] 73 | {"nested" opts}) 74 | 75 | (defn terms 76 | ([field] 77 | {"terms" {"field" field}}) 78 | ([field opts] 79 | {"terms" (merge {"field" field} opts)})) 80 | 81 | (defn range 82 | ([field ranges] 83 | {"range" {"field" field 84 | "ranges" ranges}}) 85 | ([field ranges opts] 86 | {"range" (merge {"field" field 87 | "ranges" ranges} opts)})) 88 | 89 | (defn date-range 90 | [field ^String format ranges] 91 | {"date_range" {"field" field 92 | "ranges" ranges 93 | "format" format}}) 94 | 95 | (defn ip-range 96 | [field ranges] 97 | {"ip_range" {"field" field 98 | "ranges" ranges}}) 99 | 100 | (defn histogram 101 | ([field ^long interval] 102 | {"histogram" {"field" field 103 | "interval" interval}}) 104 | ([field ^long interval opts] 105 | {"histogram" (merge {"field" field 106 | "interval" interval} 107 | opts)})) 108 | 109 | (defn date-histogram 110 | ([field ^String interval] 111 | {"date_histogram" {"field" field 112 | "interval" interval}}) 113 | ([field ^String interval opts] 114 | {"date_histogram" (merge {"field" field 115 | "interval" interval} 116 | opts)})) 117 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/common/bulk.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.common.bulk) 11 | 12 | (def ^:private special-operation-keys [:_doc_as_upsert 13 | :_index 14 | :_type 15 | :_id 16 | :_retry_on_conflict 17 | :_routing 18 | :_percolate 19 | :_parent 20 | :_script 21 | :_script_params 22 | :_scripted_upsert 23 | :_timestamp 24 | :_ttl 25 | :_version 26 | :_version_type]) 27 | 28 | (defn index-operation 29 | [doc] 30 | {"index" (select-keys doc special-operation-keys)}) 31 | 32 | (defn create-operation 33 | [doc] 34 | {"create" (select-keys doc special-operation-keys)}) 35 | 36 | (defn update-operation 37 | [doc] 38 | {"update" (select-keys doc special-operation-keys)}) 39 | 40 | (defn delete-operation 41 | [doc] 42 | {"delete" (select-keys doc special-operation-keys)}) 43 | 44 | (defn bulk-index 45 | "generates the content for a bulk insert operation" 46 | ([documents] 47 | (let [operations (map index-operation documents) 48 | documents (map #(apply dissoc % special-operation-keys) documents)] 49 | (interleave operations documents)))) 50 | 51 | (defn bulk-create 52 | "generates the content for a bulk create operation" 53 | ([documents] 54 | (let [operations (map create-operation documents) 55 | documents (map #(apply dissoc % special-operation-keys) documents)] 56 | (interleave operations documents)))) 57 | 58 | (defn bulk-update 59 | "generates the content for a bulk update operation" 60 | ([documents] 61 | (let [operations (map update-operation documents) 62 | documents (map #(apply dissoc % special-operation-keys) documents)] 63 | (interleave operations documents)))) 64 | 65 | (defn bulk-delete 66 | "generates the content for a bulk delete operation" 67 | ([documents] 68 | (map delete-operation documents))) 69 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/escape.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.escape 16 | (:require [clojure.string :as string])) 17 | 18 | (defn escape-query-string-characters 19 | "Escapes Lucene special characters 20 | 21 | For more information, please refer to " 22 | [s] 23 | (-> 24 | (string/replace s #"[*\-+!(){}\[\]^\"~?:\\]" #(str "\\" %1)) 25 | (string/replace #"(&&|\|\|)" #(str "\\" (%1 1))))) 26 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/native/admin.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.native.admin 16 | (:require [clojurewerkz.elastisch.native :as es] 17 | [clojurewerkz.elastisch.native.conversion :as cnv]) 18 | (:import org.elasticsearch.client.Client 19 | org.elasticsearch.action.admin.cluster.repositories.put.PutRepositoryResponse 20 | org.elasticsearch.action.admin.cluster.snapshots.create.CreateSnapshotResponse 21 | org.elasticsearch.action.admin.cluster.snapshots.delete.DeleteSnapshotResponse)) 22 | 23 | (defn register-snapshot-repository 24 | ([^Client conn ^String name] (register-snapshot-repository conn name nil)) 25 | ([^Client conn ^String name opts] 26 | (let [ft (es/admin-put-repository conn (cnv/->put-repository-request name opts)) 27 | ^PutRepositoryResponse res (.actionGet ft)] 28 | (cnv/acknowledged-response->map res)))) 29 | 30 | (defn take-snapshot 31 | "Takes a snapshot" 32 | ([^Client conn ^String repository ^String snapshot] (take-snapshot conn repository snapshot nil)) 33 | ([^Client conn ^String repository ^String snapshot opts] 34 | (let [ft (es/admin-create-snapshot conn (cnv/->create-snapshot-request repository snapshot opts)) 35 | ^CreateSnapshotResponse res (.actionGet ft)] 36 | ;; TODO: actually calculate this using RestStatus 37 | {:accepted true}))) 38 | 39 | (defn delete-snapshot 40 | "Deletes a snapshot" 41 | [^Client conn ^String repository ^String snapshot] 42 | (let [ft (es/admin-delete-snapshot conn (cnv/->delete-snapshot-request repository snapshot)) 43 | ^DeleteSnapshotResponse res (.actionGet ft)] 44 | (cnv/acknowledged-response->map res))) 45 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/native/bulk.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.native.bulk 16 | (:refer-clojure :exclude [get replace count sort]) 17 | (:require [clojurewerkz.elastisch.native :as native] 18 | [clojure.string :as string] 19 | [clojure.set :refer :all] 20 | [clojurewerkz.elastisch.native.conversion :as cnv] 21 | [clojurewerkz.elastisch.common.bulk :as common-bulk]) 22 | (:import org.elasticsearch.client.Client 23 | org.elasticsearch.action.bulk.BulkRequestBuilder 24 | org.elasticsearch.action.bulk.BulkResponse 25 | org.elasticsearch.action.index.IndexRequest 26 | org.elasticsearch.action.update.UpdateRequest 27 | org.elasticsearch.action.delete.DeleteRequest)) 28 | 29 | (defprotocol AddOperation 30 | (add-operation [operation bulk-builder])) 31 | 32 | (extend-protocol AddOperation 33 | IndexRequest 34 | (add-operation [^IndexRequest operation ^BulkRequestBuilder bulk-builder] 35 | (.add bulk-builder operation)) 36 | 37 | UpdateRequest 38 | (add-operation [^UpdateRequest operation ^BulkRequestBuilder bulk-builder] 39 | (.add bulk-builder operation)) 40 | 41 | DeleteRequest 42 | (add-operation [^DeleteRequest operation ^BulkRequestBuilder bulk-builder] 43 | (.add bulk-builder operation))) 44 | 45 | (defn add-default [doc default] 46 | (if-let [action (cnv/get-bulk-item-action doc)] 47 | (update-in doc [action] #(merge default %)) 48 | doc)) 49 | 50 | (defn bulk 51 | "Performs a bulk operation" 52 | ([^Client conn operations] (bulk conn operations nil)) 53 | ([^Client conn operations params] 54 | (let [^BulkRequestBuilder req (reduce #(add-operation %2 %1) (.prepareBulk conn) 55 | (cnv/->action-requests operations))] 56 | (when (:refresh params) 57 | (.setRefresh req true)) 58 | (-> req 59 | .execute 60 | ^BulkResponse .actionGet 61 | cnv/bulk-response->map)))) 62 | 63 | (defn bulk-with-index 64 | "Performs a bulk operation defaulting to the index specified" 65 | ([^Client conn index operations] (bulk-with-index conn index operations nil)) 66 | ([^Client conn index operations params] 67 | (bulk conn (map #(add-default % {:_index index}) operations) params))) 68 | 69 | (defn bulk-with-index-and-type 70 | "Performs a bulk operation defaulting to the index and type specified" 71 | ([^Client conn index mapping-type operations] (bulk-with-index-and-type conn index mapping-type operations nil)) 72 | ([^Client conn index mapping-type operations params] 73 | (bulk conn 74 | (map #(add-default % {:_index index :_type mapping-type}) operations) 75 | params))) 76 | 77 | (def index-operation common-bulk/index-operation) 78 | 79 | (def delete-operation common-bulk/delete-operation) 80 | 81 | (def bulk-index common-bulk/bulk-index) 82 | 83 | (def bulk-update common-bulk/bulk-update) 84 | 85 | (def bulk-delete common-bulk/bulk-delete) 86 | 87 | (def bulk-create common-bulk/bulk-create) 88 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/native/multi.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.native.multi 16 | (:require [clojurewerkz.elastisch.native :as es] 17 | [clojurewerkz.elastisch.native.conversion :as cnv]) 18 | (:import org.elasticsearch.client.Client)) 19 | 20 | (defn search 21 | "Performs multi search" 22 | ([^Client conn queries] (search conn queries nil)) 23 | ([^Client conn queries opts] 24 | (let [res (es/multi-search conn (cnv/->multi-search-request conn queries opts))] 25 | (cnv/multi-search-response->seq (.actionGet res))))) 26 | 27 | (defn search-with-index 28 | "Performs multi search defaulting to the index specified" 29 | ([^Client conn index queries] (search-with-index conn index queries nil)) 30 | ([^Client conn index queries opts] 31 | (let [res (es/multi-search conn (cnv/->multi-search-request conn index queries opts))] 32 | (cnv/multi-search-response->seq (.actionGet res))))) 33 | 34 | (defn search-with-index-and-type 35 | "Performs multi search defaulting to the index and type specified" 36 | ([^Client conn index mapping-type queries] (search-with-index-and-type conn index mapping-type queries nil)) 37 | ([^Client conn index mapping-type queries opts] 38 | (let [res (es/multi-search conn (cnv/->multi-search-request conn 39 | index 40 | mapping-type 41 | queries opts))] 42 | (cnv/multi-search-response->seq (.actionGet res))))) 43 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/native/percolation.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.native.percolation 16 | (:require [clojurewerkz.elastisch.native :as es] 17 | [clojurewerkz.elastisch.native.conversion :as cnv] 18 | [clojure.walk :as wlk]) 19 | (:import org.elasticsearch.action.index.IndexResponse 20 | org.elasticsearch.action.delete.DeleteResponse 21 | org.elasticsearch.action.percolate.PercolateResponse 22 | org.elasticsearch.action.index.IndexRequestBuilder 23 | java.util.Map 24 | org.elasticsearch.client.Client 25 | org.elasticsearch.percolator.PercolatorService)) 26 | 27 | ;; 28 | ;; API 29 | ;; 30 | 31 | (defn register-query 32 | "Registers a percolator for the given index" 33 | ([^Client conn index query-name] (register-query conn index query-name nil)) 34 | ([^Client conn index query-name opts] 35 | (let [^IndexRequestBuilder irb (doto (.prepareIndex ^Client conn 36 | (name index) 37 | PercolatorService/TYPE_NAME 38 | query-name) 39 | (.setSource ^Map (wlk/stringify-keys opts))) 40 | ft (.execute irb) 41 | ^IndexResponse res (.actionGet ft)] 42 | (merge (cnv/index-response->map res) {:ok true})))) 43 | 44 | (defn unregister-query 45 | "Unregisters a percolator query for the given index" 46 | [^Client conn index percolator] 47 | (let [ft (es/delete conn (cnv/->delete-request index 48 | PercolatorService/TYPE_NAME 49 | percolator)) 50 | ^DeleteResponse res (.actionGet ft)] 51 | (merge (cnv/delete-response->map res) {:ok (.isFound res) :found (.isFound res)}))) 52 | 53 | (defn percolate 54 | "Percolates a document and see which queries match on it. The document is not indexed, just 55 | matched against the queries you register with [[register-query]]." 56 | ([^Client conn index mapping-type] (percolate conn index mapping-type nil)) 57 | ([^Client conn index mapping-type opts] 58 | (let [prb (doto (.preparePercolate ^Client conn) 59 | (.setIndices (cnv/->string-array index)) 60 | (.setDocumentType (name mapping-type)) 61 | (.setSource ^Map (wlk/stringify-keys opts))) 62 | ft (.execute prb) 63 | ^PercolateResponse res (.actionGet ft)] 64 | (cnv/percolate-response->map res)))) 65 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/native/response.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.native.response) 16 | 17 | ;; 18 | ;; API 19 | ;; 20 | 21 | (defn created? 22 | [m] 23 | (:created m)) 24 | 25 | (defn acknowledged? 26 | [m] 27 | (:acknowledged m)) 28 | 29 | (defn created-or-acknowledged? 30 | [m] 31 | (or (created? m) 32 | (acknowledged? m))) 33 | 34 | (defn all-shards-report-success? 35 | [m] 36 | (= (get-in m [:_shards :failed]) 0)) 37 | 38 | (defn ok? 39 | [m] 40 | (or (created-or-acknowledged? m) 41 | (and (all-shards-report-success? m) 42 | (nil? (:error m))))) 43 | 44 | (defn found? 45 | [m] 46 | (true? (get m :found))) 47 | 48 | (defn not-found? 49 | [m] 50 | (false? (:found m))) 51 | 52 | (defn accepted? 53 | [m] 54 | (:accepted m)) 55 | 56 | (defn valid? 57 | "Returns `true` if a validation query response indicates valid query, `false` otherwise" 58 | [m] 59 | (:valid m)) 60 | 61 | (defn timed-out? 62 | [m] 63 | (:timed_out m)) 64 | 65 | (defn total-hits 66 | "Returns number of search hits from a response" 67 | [m] 68 | (get-in m [:hits :total])) 69 | 70 | (defn count-from 71 | "Returns total number of search hits from a response" 72 | [m] 73 | (get m :count)) 74 | 75 | (defn any-hits? 76 | "Returns `true` if a response has any search hits, `false` otherwise" 77 | [m] 78 | (> (total-hits m) 0)) 79 | 80 | (def no-hits? (complement any-hits?)) 81 | 82 | (defn hits-from 83 | "Returns search hits from a response as a collection. To retrieve hits overview, get the `:hits` 84 | key from the response" 85 | [m] 86 | (get-in m [:hits :hits])) 87 | 88 | (defn ids-from 89 | "Returns search hit ids from a response" 90 | [m] 91 | (if (any-hits? m) 92 | (set (map :_id (hits-from m))) 93 | #{})) 94 | 95 | (defn matches-from 96 | "Returns matches from a percolation response as a collection." 97 | [m] 98 | (get m :matches [])) 99 | 100 | (defn aggregations-from 101 | "Returns aggregations from a search response" 102 | [m] 103 | (get m :aggregations [])) 104 | 105 | (defn aggregation-from 106 | "Return a single aggregation from a search response" 107 | [m name] 108 | (get-in m [:aggregations name] [])) 109 | 110 | (defn source-from 111 | "Returns document source from a get response" 112 | [m] 113 | (get m :_source)) 114 | 115 | (defn sources-from 116 | "Returns search hit sources from a response as a collection" 117 | [m] 118 | (map source-from (hits-from m))) 119 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/rest/bulk.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.rest.bulk 16 | (:refer-clojure :exclude [get replace count sort]) 17 | (:require [clojurewerkz.elastisch.rest :as rest] 18 | [cheshire.core :as json] 19 | [clojure.string :as string] 20 | [clojure.set :refer :all] 21 | [clojurewerkz.elastisch.common.bulk :as common-bulk]) 22 | (:import clojurewerkz.elastisch.rest.Connection)) 23 | 24 | (defn ^:private bulk-with-url 25 | ([conn url operations] (bulk-with-url conn url operations nil)) 26 | ([conn url operations opts] 27 | (let [bulk-json (map json/encode operations) 28 | bulk-json (-> bulk-json 29 | (interleave (repeat "\n")) 30 | (string/join))] 31 | (rest/post-string conn url 32 | {:body bulk-json 33 | :query-params opts})))) 34 | (defn bulk 35 | "Performs a bulk operation" 36 | ([^Connection conn operations] (bulk conn operations nil)) 37 | ([^Connection conn operations params] 38 | (when (not-empty operations) 39 | (bulk-with-url conn (rest/bulk-url conn) operations params)))) 40 | 41 | (defn bulk-with-index 42 | "Performs a bulk operation defaulting to the index specified" 43 | ([^Connection conn index operations] (bulk-with-index conn index operations nil)) 44 | ([^Connection conn index operations params] 45 | (bulk-with-url conn (rest/bulk-url conn 46 | index) operations params))) 47 | 48 | (defn bulk-with-index-and-type 49 | "Performs a bulk operation defaulting to the index and type specified" 50 | ([^Connection conn index mapping-type operations] (bulk-with-index-and-type conn index mapping-type operations nil)) 51 | ([^Connection conn index mapping-type operations params] 52 | (bulk-with-url conn (rest/bulk-url conn 53 | index mapping-type) operations params))) 54 | 55 | (def index-operation common-bulk/index-operation) 56 | 57 | (def delete-operation common-bulk/delete-operation) 58 | 59 | (def bulk-index common-bulk/bulk-index) 60 | 61 | (def bulk-update common-bulk/bulk-update) 62 | 63 | (def bulk-delete common-bulk/bulk-delete) 64 | 65 | (def bulk-create common-bulk/bulk-create) 66 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/rest/multi.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.rest.multi 16 | (:require [clojurewerkz.elastisch.rest :as rest] 17 | [cheshire.core :as json] 18 | [clojure.string :as string]) 19 | (:import clojurewerkz.elastisch.rest.Connection)) 20 | 21 | (defn ^:private msearch-with-url 22 | [conn url queries opts] 23 | (let [body (string/join "\n" (doall (map json/encode queries)))] 24 | (rest/get conn url 25 | ;; multi-search is sensitive to trailing new line. MK. 26 | {:body (str body "\n") 27 | :query-params opts}))) 28 | 29 | (defn search 30 | "Performs multi search" 31 | ([conn queries] (search conn queries nil)) 32 | ([conn queries params] 33 | (:responses (msearch-with-url conn (rest/multi-search-url conn) queries params)))) 34 | 35 | (defn search-with-index 36 | "Performs multi search defaulting to the index specified" 37 | ([^Connection conn index queries] (search-with-index conn index queries nil)) 38 | ([^Connection conn index queries params] 39 | (:responses (msearch-with-url conn (rest/multi-search-url conn 40 | index) queries params)))) 41 | 42 | (defn search-with-index-and-type 43 | "Performs multi search defaulting to the index and type specified" 44 | ([^Connection conn index mapping-type queries] (search-with-index-and-type conn index mapping-type queries nil)) 45 | ([^Connection conn index mapping-type queries params] 46 | (:responses (msearch-with-url conn (rest/multi-search-url conn 47 | index mapping-type) 48 | queries params)))) 49 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/rest/percolation.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.rest.percolation 16 | (:require [clojurewerkz.elastisch.rest :as rest] 17 | [cheshire.core :as json]) 18 | (:import clojurewerkz.elastisch.rest.Connection)) 19 | 20 | ;; 21 | ;; API 22 | ;; 23 | 24 | (defn register-query 25 | "Registers a percolator for the given index" 26 | ([^Connection conn index percolator] (register-query conn index percolator nil)) 27 | ([^Connection conn index percolator opts] 28 | (rest/put conn (rest/percolator-url conn 29 | index percolator) 30 | {:body opts}))) 31 | 32 | (defn unregister-query 33 | "Unregisters a percolator query for the given index" 34 | [^Connection conn index percolator] 35 | (rest/delete conn (rest/percolator-url conn 36 | index percolator))) 37 | 38 | (defn percolate 39 | "Percolates a document and see which queries match on it. The document is not indexed, just 40 | matched against the queries you register with [[register-query]]." 41 | ([^Connection conn index percolator] (percolate conn index percolator nil)) 42 | ([^Connection conn index percolator opts] 43 | ;; rest/get won't serialize the body for us. MK. 44 | (rest/get conn (rest/index-percolation-url conn 45 | index percolator) 46 | {:body (json/encode opts)}))) 47 | 48 | (defn percolate-existing 49 | "Percolates an existing document and sees which queries match on it." 50 | [^Connection conn index percolator id] 51 | (rest/get conn (rest/existing-doc-index-percolation-url conn 52 | index percolator id))) 53 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/rest/response.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.rest.response 16 | (:require [clojurewerkz.support.http.statuses :as statuses])) 17 | 18 | ;; 19 | ;; API 20 | ;; 21 | 22 | (defn created? 23 | [m] 24 | (true? (or (get m :created) 25 | (= 201 (get m :status))))) 26 | 27 | (defn acknowledged? 28 | [m] 29 | (:acknowledged m)) 30 | 31 | (defn created-or-acknowledged? 32 | [m] 33 | (or (created? m) 34 | (acknowledged? m))) 35 | 36 | 37 | (defn all-shards-report-success? 38 | [m] 39 | (= (get-in m [:_shards :failed]) 0)) 40 | 41 | (defn ok? 42 | [m] 43 | (or (created-or-acknowledged? m) 44 | (and (all-shards-report-success? m) 45 | (nil? (:error m))))) 46 | 47 | (defn conflict? 48 | [m] 49 | (let [s (:status m)] 50 | (and s (statuses/conflict? s)))) 51 | 52 | (defn found? 53 | [m] 54 | (true? (get m :found))) 55 | 56 | (defn not-found? 57 | [m] 58 | (let [s (:status m)] 59 | (or (false? (:found m)) 60 | (and s (statuses/missing? s))))) 61 | 62 | (defn accepted? 63 | [m] 64 | (:accepted m)) 65 | 66 | (defn valid? 67 | "Returns `true` if a validation query response indicates valid query, `false` otherwise" 68 | [m] 69 | (:valid m)) 70 | 71 | (defn timed-out? 72 | [m] 73 | (:timed_out m)) 74 | 75 | 76 | (defn total-hits 77 | "Returns number of search hits from a response" 78 | [m] 79 | (get-in m [:hits :total])) 80 | 81 | (defn count-from 82 | "Returns total number of search hits from a response" 83 | [m] 84 | (get m :count)) 85 | 86 | (defn any-hits? 87 | "Returns `true` if a response has any search hits, `false` otherwise" 88 | [m] 89 | (> (total-hits m) 0)) 90 | 91 | (def no-hits? (complement any-hits?)) 92 | 93 | (defn hits-from 94 | "Returns search hits from a response as a collection. To retrieve hits overview, get the `:hits` 95 | key from the response" 96 | [m] 97 | (get-in m [:hits :hits])) 98 | 99 | (defn ids-from 100 | "Returns search hit ids from a response" 101 | [m] 102 | (if (any-hits? m) 103 | (set (map :_id (hits-from m))) 104 | #{})) 105 | 106 | (defn matches-from 107 | "Returns matches from a percolation response as a collection." 108 | [m] 109 | (get m :matches [])) 110 | 111 | (defn aggregations-from 112 | "Returns aggregations from a search response" 113 | [m] 114 | (get m :aggregations [])) 115 | 116 | (defn aggregation-from 117 | "Return a single aggregation from a search response" 118 | [m name] 119 | (get-in m [:aggregations name] [])) 120 | 121 | (defn source-from 122 | "Returns document source from a get response" 123 | [m] 124 | (get m :_source)) 125 | 126 | (defn sources-from 127 | "Returns search hit sources from a response as a collection" 128 | [m] 129 | (map source-from (hits-from m))) 130 | -------------------------------------------------------------------------------- /src/clojurewerkz/elastisch/rest/utils.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; Licensed under the Apache License, Version 2.0 (the "License"); 4 | ;; you may not use this file except in compliance with the License. 5 | ;; You may obtain a copy of the License at 6 | ;; 7 | ;; http://www.apache.org/licenses/LICENSE-2.0 8 | ;; 9 | ;; Unless required by applicable law or agreed to in writing, software 10 | ;; distributed under the License is distributed on an "AS IS" BASIS, 11 | ;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | ;; See the License for the specific language governing permissions and 13 | ;; limitations under the License. 14 | 15 | (ns clojurewerkz.elastisch.rest.utils 16 | (:require clojure.string)) 17 | 18 | ;; 19 | ;; API 20 | ;; 21 | 22 | (defn join-names 23 | [name-or-names] 24 | (clojure.string/join "," (flatten [name-or-names]))) 25 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/escape_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.escape-test 11 | (:require [clojurewerkz.elastisch.escape :as escape] 12 | [clojure.test :refer :all] 13 | [clojurewerkz.elastisch.test.helpers :refer [ci?]])) 14 | 15 | (when-not (ci?) 16 | (deftest escape-query-string-characters-test 17 | (testing "escapes Lucene special chars" 18 | (is (= "\\+ \\- \\&& & \\|| | \\! \\( \\) \\{ \\} \\[ \\] \\^ \\\" \\~ \\* \\? \\: \\\\" 19 | (escape/escape-query-string-characters "+ - && & || | ! ( ) { } [ ] ^ \" ~ * ? : \\")))) 20 | 21 | (testing "does not escape non-special chars" 22 | (let [s "John %@= Doe"] 23 | (is (= s (escape/escape-query-string-characters s))))))) 24 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/internal/rest_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.internal.rest-test 11 | (:require [clojurewerkz.elastisch.rest :as rest] 12 | [clojure.test :refer :all]) 13 | (:import clojurewerkz.elastisch.rest.Connection)) 14 | 15 | (println (str "Using Clojure version " *clojure-version*)) 16 | 17 | (def es-url (or (System/getenv "ES_URL") 18 | (System/getenv "ELASTICSEARCH_URL") 19 | "http://localhost:9200")) 20 | 21 | (deftest test-successful-connection 22 | (is (= es-url (.uri ^Connection rest/*endpoint*)))) 23 | 24 | (deftest test-mget-path 25 | (let [conn (rest/connect es-url)] 26 | (is (= (str es-url "/_mget") 27 | (rest/index-mget-url conn))) 28 | (is (= (str es-url "/index_name/_mget") 29 | (rest/index-mget-url conn "index_name"))) 30 | (is (= (str es-url "/index_name/type_name/_mget") 31 | (rest/index-mget-url conn "index_name" "type_name"))))) 32 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/internal/utils_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.internal.utils-test 11 | (:require [clojurewerkz.elastisch.rest.utils :as utils] 12 | [clojure.test :refer :all])) 13 | 14 | (deftest join-names-test 15 | (is (= "name" (utils/join-names "name"))) 16 | (is (= "name1,name2" (utils/join-names ["name1", "name2"])))) 17 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/avg_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.avg-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-avg-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:avg_age (a/avg "age")}}) 28 | agg (aggregation-from response :avg_age)] 29 | (is (= {:value 29.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/cardinality_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.cardinality-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-cardinality-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:username_count {:cardinality {:field "username"}}}}) 28 | agg (aggregation-from response :username_count)] 29 | (is (>= (:value agg) 4))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/date_histogram_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.date-histogram-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:rest true :aggregation true} test-date-histogram-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/date-histogram "signed_up_at" "1d")}}) 28 | agg (aggregation-from response :age_ranges)] 29 | (is (:buckets agg)))) 30 | 31 | (deftest ^{:rest true :aggregation true} test-nested-date-histogram-aggregation 32 | (let [index-name "people" 33 | mapping-type "person" 34 | response (doc/search conn index-name mapping-type 35 | {:query (q/match-all) 36 | :aggregations {:age_ranges 37 | (merge 38 | {:aggs {:avg_age (a/avg "age")}} 39 | (a/date-histogram "signed_up_at" "1d"))}}) 40 | agg (aggregation-from response :age_ranges)] 41 | (is (= (count (:buckets agg)) (count (filter #(contains? % :avg_age) (:buckets agg)))))))) 42 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/date_range_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.date-range-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-date-range-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/date-range "signed_up_at" 28 | "date_hour_minute_second" 29 | [{:from "2012-02-01T00:00:00" :to "2012-02-29T23:59:59"} 30 | {:from "2012-03-01T00:00:00"}])}}) 31 | agg (aggregation-from response :age_ranges)] 32 | (is (:buckets agg)))) 33 | 34 | (deftest ^{:native true :aggregation true} test-nested-date-range-aggregation 35 | (let [index-name "people" 36 | mapping-type "person" 37 | response (doc/search conn index-name mapping-type 38 | {:query (q/match-all) 39 | :aggregations {:age_ranges (merge 40 | {:aggs {:avg_age (a/avg "age")}} 41 | (a/date-range "signed_up_at" 42 | "date_hour_minute_second" 43 | [{:from "2012-02-01T00:00:00" :to "2012-02-29T23:59:59"} 44 | {:from "2012-03-01T00:00:00"}]))}}) 45 | agg (aggregation-from response :age_ranges)] 46 | (is (= (count (:buckets agg)) (count (filter #(contains? % :avg_age) (:buckets agg)))))))) 47 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/extended_stats_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.extended-stats-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-extended-stats-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_stats (a/extended-stats "age")}}) 28 | agg (aggregation-from response :age_stats)] 29 | (is (= #{:count :min :max :avg :sum :std_deviation :sum_of_squares :variance} 30 | (set (keys agg))))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/histogram_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.histogram-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-histogram-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_histograms (a/histogram "age" 5)}}) 28 | agg (aggregation-from response :age_histograms)] 29 | (is (:buckets agg)))) 30 | 31 | (deftest ^{:native true :aggregation true} test-nested-histogram-aggregation 32 | (let [index-name "people" 33 | mapping-type "person" 34 | response (doc/search conn index-name mapping-type 35 | {:query (q/match-all) 36 | :aggregations {:age_histograms (merge 37 | {:aggs {:avg_age (a/avg "age")}} 38 | (a/histogram "age" 5))}}) 39 | agg (aggregation-from response :age_histograms)] 40 | (is (= (count (:buckets agg)) (count (filter #(contains? % :avg_age) (:buckets agg)))))))) 41 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/max_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.max-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-max-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:max_age (a/max "age")}}) 28 | agg (aggregation-from response :max_age)] 29 | (is (= {:value 37.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/min_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.min-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-min-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:min_age (a/min "age")}}) 28 | agg (aggregation-from response :min_age)] 29 | (is (= {:value 22.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/missing_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.missing-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-missing-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:no_country (a/missing :country)}}) 28 | agg (aggregation-from response :no_country)] 29 | (is (>= (:doc_count agg) 3))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/percentiles_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.percentiles-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-percentiles-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:percentiles_age (a/percentiles "age")}}) 28 | agg (aggregation-from response :percentiles_age)] 29 | (is (= #{:1.0 :5.0 :25.0 :50.0 :75.0 :95.0 :99.0} 30 | (set (keys (get agg :values)))))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/range_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.range-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-range-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/range "age" [{:from 15 :to 20} 28 | {:from 21 :to 25} 29 | {:from 26 :to 30} 30 | {:from 31}])}}) 31 | agg (aggregation-from response :age_ranges)] 32 | (is (:buckets agg)))) 33 | 34 | (deftest ^{:native true :aggregation true} test-nested-range-aggregation 35 | (let [index-name "people" 36 | mapping-type "person" 37 | response (doc/search conn index-name mapping-type 38 | {:query (q/match-all) 39 | :aggregations {:age_ranges (merge 40 | {:aggs {:avg_age (a/avg "age")}} 41 | (a/range "age" [{:from 15 :to 20} 42 | {:from 21 :to 25} 43 | {:from 26 :to 30} 44 | {:from 31}]))}}) 45 | agg (aggregation-from response :age_ranges)] 46 | (is (= (count (:buckets agg)) (count (filter #(contains? % :avg_age) (:buckets agg)))))))) 47 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/stats_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.stats-aggregation-test 11 | (:refer-clojure :exclude [replace]) 12 | (:require [clojurewerkz.elastisch.native.document :as doc] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.test.helpers :as th] 17 | [clojure.test :refer :all] 18 | [clojurewerkz.elastisch.native.response :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:native true :aggregation true} test-stats-aggregation 24 | (let [index-name "people" 25 | mapping-type "person" 26 | response (doc/search conn index-name mapping-type 27 | {:query (q/match-all) 28 | :aggregations {:age_stats (a/stats "age")}}) 29 | agg (aggregation-from response :age_stats)] 30 | (is (= #{:count :min :max :avg :sum} (set (keys agg))))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/sum_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.sum-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-sum-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:sum_age (a/sum "age")}}) 28 | agg (aggregation-from response :sum_age)] 29 | (is (>= (:value agg) 116))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/terms_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.terms-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-terms-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:title_terms (a/terms "title")}}) 28 | agg (aggregation-from response :title_terms)] 29 | (is (= 6 (count (:buckets agg)))))) 30 | 31 | (deftest ^{:native true :aggregation true} test-nested-terms-aggregation 32 | (let [index-name "people" 33 | mapping-type "person" 34 | response (doc/search conn index-name mapping-type 35 | {:query (q/match-all) 36 | :aggregations {:title_terms (merge 37 | {:aggs {:avg_age (a/avg "age")}} 38 | (a/terms "title"))}}) 39 | agg (aggregation-from response :title_terms)] 40 | (is (= 6 (count (filter #(contains? % :avg_age) (:buckets agg)))))))) 41 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/top_hits_aggregations_test.clj: -------------------------------------------------------------------------------- 1 | (ns clojurewerkz.elastisch.native-api.aggregations.top-hits-aggregations-test 2 | (:require [clojurewerkz.elastisch.native.document :as doc] 3 | [clojurewerkz.elastisch.query :as q] 4 | [clojurewerkz.elastisch.aggregation :as a] 5 | [clojurewerkz.elastisch.fixtures :as fx] 6 | [clojurewerkz.elastisch.test.helpers :as th] 7 | [clojure.test :refer :all] 8 | [clojurewerkz.elastisch.native.response :refer :all])) 9 | 10 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 11 | 12 | (let [conn (th/connect-native-client)] 13 | (deftest ^{:native true :aggregation true} test-top-hits-aggregation 14 | (let [index-name "articles" 15 | mapping-type "article" 16 | response (doc/search conn index-name mapping-type 17 | {:query (q/match-all) 18 | :aggregations 19 | {:top-hits 20 | {:top_hits {:sort [{:title {:order "asc"}}] 21 | :size 4}}}}) 22 | agg (aggregation-from response :top-hits)] 23 | (is (= 4 (get-in agg [:hits :total]))) 24 | (is (= 4 (count (get-in agg [:hits :hits]))))))) 25 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/aggregations/value_count_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.aggregations.value-count-aggregation-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.aggregation :as a] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true :aggregation true} test-value-count-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:agg1 (a/value-count "age")}}) 28 | agg (aggregation-from response :agg1)] 29 | (is (>= (:value agg) 4))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/count_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.count-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.native.response :refer [count-from]])) 18 | 19 | (use-fixtures :each fx/reset-indexes) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:native true} test-count-with-the-default-query 23 | (let [index-name "people" 24 | index-type "person"] 25 | (idx/create conn index-name {:mappings fx/people-mapping}) 26 | (doc/create conn index-name index-type fx/person-jack) 27 | (doc/create conn index-name index-type fx/person-joe) 28 | (idx/refresh conn index-name) 29 | (are [c r] (= c (count-from r)) 30 | 2 (doc/count conn index-name index-type)))) 31 | 32 | (deftest ^{:native true} test-count-with-a-term-query 33 | (let [index-name "people" 34 | index-type "person"] 35 | (idx/create conn index-name {:mappings fx/people-mapping}) 36 | (doc/create conn index-name index-type fx/person-jack) 37 | (doc/create conn index-name index-type fx/person-joe) 38 | (idx/refresh conn index-name) 39 | (are [c r] (= c (count-from r)) 40 | 1 (doc/count conn index-name index-type (q/term :username "esjack")) 41 | 1 (doc/count conn index-name index-type (q/term :username "esjoe")) 42 | 0 (doc/count conn index-name index-type (q/term :username "esmary"))))) 43 | 44 | (deftest ^{:native true} test-count-with-mixed-mappings 45 | (let [index-name "people" 46 | index-type "person" 47 | alt-index-type "altperson"] 48 | (idx/create conn index-name {:mappings fx/people-mapping}) 49 | (doc/create conn index-name index-type fx/person-jack) 50 | (doc/create conn index-name index-type fx/person-joe) 51 | (doc/create conn index-name "altperson" fx/person-jack) 52 | (idx/refresh conn index-name) 53 | (are [c r] (= c (count-from r)) 54 | 1 (doc/count conn index-name index-type (q/term :username "esjack")) 55 | 1 (doc/count conn index-name alt-index-type (q/term :username "esjack")) 56 | 0 (doc/count conn index-name alt-index-type (q/term :username "esjoe")))))) 57 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/delete_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.delete-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native :as es] 13 | [clojurewerkz.elastisch.native.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.test.helpers :as th] 17 | [clojurewerkz.elastisch.native.response :refer [count-from found?]] 18 | [clojure.test :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:native true} test-delete-when-a-document-exists 24 | (let [id "1" 25 | index-name "people" 26 | mapping-type "person"] 27 | (doc/put conn index-name mapping-type id fx/person-jack) 28 | (is (doc/present? conn index-name mapping-type id)) 29 | (is (found? (doc/delete conn index-name mapping-type id))) 30 | (is (not (doc/present? conn index-name mapping-type id)))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/filtering_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.filtering-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native :as es] 13 | [clojurewerkz.elastisch.native.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.test.helpers :as th] 17 | [clojurewerkz.elastisch.native.response :refer :all] 18 | [clojure.test :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index 21 | fx/prepopulate-articles-index fx/prepopulate-tweets-index) 22 | 23 | (let [conn (th/connect-native-client)] 24 | (deftest ^{:native true} test-term-filtering 25 | (let [index-name "people" 26 | mapping-type "person" 27 | sources (sources-from (doc/search conn index-name mapping-type 28 | {:query (q/match-all) 29 | :filter {:term {:username "esmary"}}}))] 30 | (is (= 1 (count sources))) 31 | (is (= "Lindey" (-> sources first :last-name))))) 32 | 33 | (deftest ^{:native true} test-range-filtering 34 | (let [index-name "people" 35 | mapping-type "person" 36 | sources (sources-from (doc/search conn index-name mapping-type 37 | {:query (q/match-all) 38 | :filter {:range {:age {:from 26 :to 30}}}}))] 39 | (is (= 2 (count sources))) 40 | (is (#{28 29} (-> sources first :age)))))) 41 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/highlighting_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.highlighting-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native :as es] 13 | [clojurewerkz.elastisch.native.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.test.helpers :as th] 17 | [clojurewerkz.elastisch.native.response :refer :all] 18 | [clojure.test :refer :all])) 19 | 20 | 21 | (use-fixtures :each fx/reset-indexes) 22 | 23 | (let [conn (th/connect-native-client)] 24 | (deftest ^{:native true} test-highlighting-with-all-defaults 25 | (let [index "articles" 26 | type "article"] 27 | (idx/create conn index {:mappings fx/articles-mapping}) 28 | (doc/put conn index type "1" fx/article-on-elasticsearch) 29 | (doc/put conn index type "2" fx/article-on-lucene) 30 | (doc/put conn index type "3" fx/article-on-nueva-york) 31 | (doc/put conn index type "4" fx/article-on-austin) 32 | (idx/refresh conn index) 33 | (let [resp (doc/search conn index type 34 | {:query (q/query-string {:query "software" :default_field "summary"}) 35 | :highlight {:fields {:summary {}}}}) 36 | hits (hits-from resp)] 37 | (is (re-find #"software" (-> hits first :highlight :summary first))))))) 38 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/indices_settings_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.indices-settings-test 11 | (:require [clojurewerkz.elastisch.native :as es] 12 | [clojurewerkz.elastisch.native 13 | [document :as doc] 14 | [index :as idx] 15 | [response :refer [acknowledged?]]] 16 | [clojurewerkz.elastisch.fixtures :as fx] 17 | [clojurewerkz.elastisch.test.helpers :as th] 18 | [clojure.test :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 21 | 22 | (let [conn (th/connect-native-client) 23 | index-name "people"] 24 | (deftest ^{:indexing true :native true} test-closing-and-opening-existing-index 25 | (testing "it should close people's index" 26 | (let [res (idx/close conn index-name)] 27 | (is (acknowledged? res)))) 28 | 29 | (testing "it should open people's index" 30 | (let [res (idx/open conn index-name)] 31 | (is (acknowledged? res)))))) 32 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/mappings_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.mappings-test 11 | (:require [clojurewerkz.elastisch.native.index :as idx] 12 | [clojurewerkz.elastisch.fixtures :as fx] 13 | [clojurewerkz.elastisch.test.helpers :as th] 14 | [clojurewerkz.elastisch.native.response :as resp] 15 | [clojure.test :refer :all])) 16 | 17 | (use-fixtures :each fx/reset-indexes) 18 | 19 | (let [conn (th/connect-native-client)] 20 | (deftest ^{:native true} test-updating-index-mapping 21 | (let [index "people1" 22 | mapping fx/people-mapping 23 | orig-mapping {:person {:properties {:first-name {:type "string" :store "yes"}}}} 24 | _ (idx/create conn index {:mappings orig-mapping}) 25 | response (idx/update-mapping conn index "person" {:mapping mapping})] 26 | (is (resp/created-or-acknowledged? response)) 27 | (is (get-in (idx/get-mapping conn index) [:people1 :mappings :person :properties :username :store])))) 28 | 29 | 30 | (deftest ^{:native true} test-updating-blank-index-mapping 31 | (let [index "people3" 32 | mapping fx/people-mapping 33 | _ (idx/create conn index {:mappings {}}) 34 | response (idx/update-mapping conn index "person" {:mapping mapping})] 35 | (is (resp/created-or-acknowledged? response))))) 36 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/multi_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.multi-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native :as es] 13 | [clojurewerkz.elastisch.native.multi :as multi] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.test.helpers :as th] 17 | [clojurewerkz.elastisch.native.response :refer :all] 18 | [clojure.test :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-articles-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:rest true} test-multi-search 24 | (let [res1 (doc/search conn "people" "person" {:query (q/match-all) :size 1}) 25 | res2 (doc/search conn "articles" "article" {:query (q/match-all) :size 1}) 26 | multires (multi/search conn [{:index "people" :type "person"} {:query (q/match-all) :size 1} 27 | {:index "articles" :type "article"} {:query (q/match-all) :size 1}])] 28 | (is (= (-> res1 sources-from first) 29 | (-> multires first sources-from first))) 30 | (is (= (-> res2 sources-from first) 31 | (-> multires second sources-from first))))) 32 | 33 | (deftest ^{:rest true} test-multi-with-index-and-type 34 | (let [res1 (doc/search conn "people" "person" {:query (q/term :planet "earth")}) 35 | res2 (doc/search conn "people" "person" {:query (q/term :first-name "mary")}) 36 | multires (multi/search-with-index-and-type conn 37 | "people" "person" 38 | [{} {:query (q/term :planet "earth")} 39 | {} {:query (q/term :first-name "mary")}])] 40 | (is (= (res1 :hits) (-> multires first :hits))) 41 | (is (= (res2 :hits) (-> multires second :hits)))))) 42 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/percolation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.percolation-test 11 | (:require [clojurewerkz.elastisch.native :as es] 12 | [clojurewerkz.elastisch.native.document :as doc] 13 | [clojurewerkz.elastisch.native.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.native.percolation :as pcl] 17 | [clojurewerkz.elastisch.test.helpers :as th] 18 | [clojure.walk :as wlk] 19 | [clojurewerkz.elastisch.native.response :refer :all] 20 | [clojure.test :refer :all]) 21 | (:import java.util.Map 22 | org.elasticsearch.client.Client)) 23 | 24 | (use-fixtures :each fx/reset-indexes ) 25 | 26 | (let [conn (th/connect-native-client)] 27 | (deftest ^{:native true :percolation true} test-percolation-case-1 28 | (let [index-name "articles" 29 | query-name "kuku" 30 | _ (idx/create conn index-name 31 | {:mappings fx/articles-mapping 32 | :settings {"index.number_of_shards" 1}}) 33 | result1 (pcl/register-query conn index-name query-name {:query {:term {:title "search"}}}) 34 | result2 (pcl/percolate conn index-name "article" {:doc {:title "You know, for search"} :refresh true}) 35 | result3 (pcl/unregister-query conn index-name query-name)] 36 | (is (= [query-name] (matches-from result2))) 37 | (is (= index-name (:_index result3))) 38 | (is (= ".percolator" (:_type result3)))))) 39 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/bool_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.bool-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-basic-bool-query 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type {:query (q/bool {:must {:term {:planet "earth"}} 26 | :should {:range {:age {:from 20 :to 30}}} 27 | :minimum_number_should_match 1})})] 28 | (is (any-hits? response)) 29 | (is (= (sort (ids-from response)) (sort ["1" "2" "4"]))) 30 | (is (= 3 (total-hits response)))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/filtered_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.filtered-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:query true :native true} test-basic-filtered-query 24 | (let [index-name "people" 25 | mapping-type "person" 26 | response (doc/search conn index-name mapping-type {:query (q/filtered {:query (q/term :planet "earth") 27 | :filter {:range {:age {:from 20 :to 30}}}})})] 28 | (is (any-hits? response)) 29 | (is (= 3 (total-hits response)))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/fuzzy_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.fuzzy-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-basic-fuzzy-query-with-string-fields 23 | (let [index-name "articles" 24 | mapping-type "article" 25 | response (doc/search conn index-name mapping-type {:query (q/fuzzy {:title "Nueva"})}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 1 (total-hits response))) 29 | (is (= #{"3"} (ids-from response))))) 30 | 31 | (deftest ^{:query true :native true} test-basic-fuzzy-query-with-numeric-fields 32 | (let [index-name "articles" 33 | mapping-type "article" 34 | response (doc/search conn index-name mapping-type {:query (q/fuzzy {:number-of-edits {:value 13000 :fuzziness 3}})}) 35 | hits (hits-from response)] 36 | (is (any-hits? response)) 37 | (is (= 1 (total-hits response))) 38 | (is (= #{"4"} (ids-from response)))))) 39 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/ids_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.ids-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojurewerkz.elastisch.test.helpers :as th] 15 | [clojure.set :as cs] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-basic-ids-query 23 | (let [response (doc/search conn "tweets" "tweet" {:query (q/ids "tweet" ["1" "2" "8ska88"])})] 24 | (is (any-hits? response)) 25 | (is (= 2 (total-hits response))) 26 | (is (= #{"1" "2"} (set (map :_id (hits-from response)))))))) 27 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/match_all_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.match-all-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-basic-match-all-query 23 | (let [index-name "articles" 24 | mapping-type "article" 25 | response (doc/search conn index-name mapping-type {:query (q/match-all)}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 4 (total-hits response)))))) 29 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/mlt_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.mlt-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:query true :native true} test-more-like-this-query 24 | (let [index-name "articles" 25 | mapping-type "article" 26 | response (doc/search conn index-name mapping-type {:query (q/mlt {:like_text "technology, opensource, search, full-text search, distributed, software, lucene" 27 | :fields ["tags"] :min_term_freq 1 :min_doc_freq 1})})] 28 | (is (= 2 (total-hits response))) 29 | (is (= #{"1" "2"} (ids-from response)))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/prefix_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.prefix-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-basic-prefix-query 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type {:query (q/prefix {:username "esj"})}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 2 (total-hits response))) 29 | (is (= #{"1" "3"} (set (map :_id hits)))))) 30 | 31 | (deftest ^{:query true :native true} test-full-word-prefix-query-over-a-text-field-analyzed-with-the-standard-analyzer 32 | (let [index-name "tweets" 33 | mapping-type "tweet" 34 | response (doc/search conn index-name mapping-type {:query (q/prefix {:text "why"})}) 35 | hits (hits-from response)] 36 | (is (= 1 (total-hits response))) 37 | (is (= "4" (-> hits first :_id))))) 38 | 39 | (deftest ^{:query true :native true} test-partial-prefix-query-over-a-text-field 40 | (let [index-name "tweets" 41 | mapping-type "tweet" 42 | response (doc/search conn index-name mapping-type {:query (q/prefix {:text "congr"})}) 43 | hits (hits-from response)] 44 | (is (= 1 (total-hits response))) 45 | (is (= "3" (-> hits first :_id)))))) 46 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/query_string_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.query-string-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:query true :native true} test-query-string-query 24 | (let [index-name "articles" 25 | mapping-type "article" 26 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "Austin" :default_field "title"})})] 27 | (is (= 1 (total-hits response))) 28 | (is (= #{"4"} (ids-from response))))) 29 | 30 | ;; ES native client seems to ignore special index and type names such as _all. MK. 31 | (deftest ^{:query true :native true} test-query-string-query-across-all-mapping-types 32 | (let [index-name "articles" 33 | response (doc/search-all-types conn index-name {:query (q/query-string {:query "Austin" :default_field "title"})})] 34 | (is (= 1 (total-hits response))) 35 | (is (= #{"4"} (ids-from response))))) 36 | 37 | (deftest ^{:query true :native true} test-query-string-query-across-all-indexes-and-mapping-types 38 | (let [response (doc/search-all-indexes-and-types conn {:query (q/query-string {:query "Austin" :default_field "title"})})] 39 | (is (= 1 (total-hits response))) 40 | (is (= #{"4"} (ids-from response))))) 41 | 42 | (deftest ^{:query true :native true} test-query-string-query-over-a-text-field-analyzed-with-the-standard-analyzer-case1 43 | (let [index-name "tweets" 44 | mapping-type "tweet" 45 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "cloud+"})}) 46 | hits (hits-from response)] 47 | (is (= 1 (total-hits response))) 48 | (is (= "5" (-> hits first :_id))))) 49 | 50 | (deftest ^{:query true :native true} test-query-string-query-over-a-text-field-analyzed-with-the-standard-analyzer-case2 51 | (let [index-name "tweets" 52 | mapping-type "tweet" 53 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "cloud AND (NOT adoption)"})}) 54 | hits (hits-from response)] 55 | (is (= 0 (total-hits response)))))) 56 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/range_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.range-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-range-query-over-numerical-field 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type {:query (q/range :age {:from 27 :to 29})}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 2 (total-hits response))) 29 | (is (= #{"2" "4"} (set (map :_id hits)))))) 30 | 31 | 32 | (let [index-name "tweets" 33 | mapping-type "tweet"] 34 | (deftest ^{:query true :native true} test-range-query-over-string-field 35 | (let [response (doc/search conn index-name mapping-type {:query (q/range :username {:from "c" :to "j"})}) 36 | ids (ids-from response)] 37 | (is (= 2 (total-hits response))) 38 | (is (= #{"1" "2"} ids)))) 39 | 40 | (deftest ^{:query true :native true} test-range-query-over-date-time-field-with-from 41 | (let [response (doc/search conn index-name mapping-type {:query (q/range :timestamp {:from "20120801T160000+0100"})}) 42 | ids (ids-from response)] 43 | (is (= 2 (total-hits response))) 44 | (is (= #{"1" "2"} ids)))) 45 | 46 | (deftest ^{:query true :native true} test-range-query-over-date-time-field-with-from-and-to 47 | (let [response (doc/search conn index-name mapping-type {:query (q/range :timestamp {:from "20120801T160000+0100" :to "20120801T180000+0100"})}) 48 | ids (ids-from response)] 49 | (is (= 1 (total-hits response))) 50 | (is (= #{"2"} ids)))))) 51 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/term_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.term-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (th/connect-native-client)] 23 | (deftest ^{:query true :native true} test-basic-term-query-with-person-mapping 24 | (let [result (doc/search conn "people" "person" {:query (q/term :biography "avoid")})] 25 | (is (any-hits? result)) 26 | (is (= fx/person-jack (-> result hits-from first source-from))))) 27 | 28 | 29 | (deftest ^{:query true :native true} test-term-query-with-person-mapping-and-a-limit 30 | (let [result (doc/search conn "people" "person" {:query (q/term :planet "earth") :size 2})] 31 | (is (any-hits? result)) 32 | (is (= 2 (count (hits-from result)))) 33 | ;; but total # of hits is reported w/o respect to the limit. MK. 34 | (is (= 4 (total-hits result))))) 35 | 36 | (deftest ^{:query true :native true} test-basic-term-query-with-tweets-mapping 37 | (let [result (doc/search conn "tweets" "tweet" {:query (q/term :text "improved")})] 38 | (is (any-hits? result)) 39 | (is (= fx/tweet1 (-> result hits-from first source-from))))) 40 | 41 | (deftest ^{:query true :native true} test-basic-terms-query-with-tweets-mapping 42 | (let [result (doc/search conn "tweets" "tweet" {:query (q/term :text ["supported" "improved"])})] 43 | (is (any-hits? result)) 44 | (is (= fx/tweet1 (-> result hits-from first source-from))))) 45 | 46 | (deftest ^{:query true :native true} test-basic-term-query-over-non-analyzed-usernames 47 | (are [username id] (= id (-> (doc/search conn "tweets" "tweet" {:query (q/term :username username) :sort {:timestamp "asc"}}) 48 | hits-from 49 | first 50 | :_id)) 51 | "clojurewerkz" "1" 52 | "ifesdjeen" "2" 53 | "michaelklishin" "4" 54 | "DEVOPS_BORAT" "5")) 55 | 56 | (deftest ^{:query true :native true} test-basic-term-query-over-non-analyzed-embedded-fields 57 | (are [state id] (= id (-> (doc/search conn "tweets" "tweet" {:query (q/term "location.state" state) :sort {:timestamp "asc"}}) 58 | hits-from 59 | first 60 | :_id)) 61 | "Moscow" "4" 62 | "CA" "5"))) 63 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/type_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.type-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.query :as q] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojurewerkz.elastisch.test.helpers :as th] 15 | [clojurewerkz.elastisch.native.response :refer :all] 16 | [clojure.test :refer :all])) 17 | 18 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 19 | 20 | (let [conn (th/connect-native-client)] 21 | (deftest ^{:query true :native true} test-basic-type-query 22 | (let [response (doc/search conn "tweets" "tweet" 23 | {:query (q/type "tweet")})] 24 | (is (any-hits? response)) 25 | (is (= 5 (total-hits response))) 26 | (is (= #{"tweet"} (set (map :_type (hits-from response)))))))) 27 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/queries/wildcard_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.queries.wildcard-query-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native.index :as idx] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.test.helpers :as th] 16 | [clojurewerkz.elastisch.native.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (th/connect-native-client)] 22 | (deftest ^{:query true :native true} test-trailing-wildcard-query-with-nested-fields 23 | (let [response (doc/search conn "articles" "article" {:query (q/wildcard {"latest-edit.author" "Thorw*"})}) 24 | hits (hits-from response)] 25 | (is (any-hits? response)) 26 | (is (= 1 (total-hits response))) 27 | (is (= "2" (-> hits first :_id))))) 28 | 29 | 30 | (deftest ^{:query true :native true} test-leading-wildcard-query-with-non-analyzd-field 31 | (let [response (doc/search conn "tweets" "tweet" {:query (q/wildcard {:username "*werkz"})}) 32 | hits (hits-from response)] 33 | (is (any-hits? response)) 34 | (is (= 1 (total-hits response))) 35 | (is (= "1" (-> hits first :_id)))))) 36 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/native_api/suggest_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.native-api.suggest-test 11 | (:require [clojurewerkz.elastisch.native.document :as doc] 12 | [clojurewerkz.elastisch.native :as es] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojurewerkz.elastisch.test.helpers :as th] 15 | [clojure.test :refer :all])) 16 | 17 | (let [conn (th/connect-native-client)] 18 | (use-fixtures :each fx/reset-indexes 19 | fx/prepopulate-people-suggestion 20 | fx/prepopulate-people-category-suggestion 21 | fx/prepopulate-people-location-suggestion) 22 | 23 | (deftest ^{:native true} test-suggest-complete-people-names 24 | (testing "simple autocomplete returns mary's username" 25 | (let [index-name "people_suggestions" 26 | res (doc/suggest conn index-name :completion "esma" {})] 27 | (is (map? (:hits res))) 28 | (let [payload (-> res :hits :options first :payload)] 29 | (is (= "esma" (-> res :hits :text))) 30 | (is (= "esmary" (:username payload))) 31 | (is (= "Mary" (:first-name payload))) 32 | (is (= "Lindey" (:last-name payload))))))) 33 | 34 | (deftest ^{:native true} test-fuzzy-suggest-complete-people-names 35 | (testing "simple fuzzy autocomplete returns mary's username even i had typo" 36 | (let [index-name "people_suggestions" 37 | res (doc/suggest conn index-name :fuzzy "esmor" {:fuzziness 1 :min-length 2})] 38 | (is (map? (:hits res))) 39 | (let [payload (-> res :hits :options first :payload)] 40 | (is (= "esmor" (-> res :hits :text))) 41 | (is (= "esmary" (:username payload))) 42 | (is (= "Mary" (:first-name payload))) 43 | (is (= "Lindey" (:last-name payload))))))) 44 | 45 | (deftest ^{:native true} test-suggest-complete-with-category-context 46 | (testing "autocomplete filters results by person's gender" 47 | (let [index-name "people_with_category" 48 | res (doc/suggest conn index-name :completion "e" {:context {:gender "female"}})] 49 | (is (map? (:hits res))) 50 | (is (= 2 (-> res :hits :options count))) 51 | (is (= "esmary" (-> res :hits :options first :text)))))) 52 | 53 | (deftest ^{:native true} test-suggest-complete-with-location-context 54 | (testing "autocomplete returns only matches nearby" 55 | (let [index-name "people_with_locations" 56 | opts {:context {:location {:lat 90.0 :lon 90.1}}} 57 | res (doc/suggest conn index-name :completion "es" opts)] 58 | (is (map? (:hits res))) 59 | (is (= 1 (-> res :hits :options count))) 60 | (is (= "esjack" (-> res :hits :options first :text)))))) 61 | 62 | (deftest ^{:native true} test-fuzzy-suggest-complete-with-category-context 63 | (testing "autocomplete filters results by person's gender" 64 | (let [index-name "people_with_category" 65 | res (doc/suggest conn index-name :fuzzy "es" {:context {:gender "female"}})] 66 | (is (map? (:hits res))) 67 | (is (= 2 (-> res :hits :options count))) 68 | (is (= "esmary" (-> res :hits :options first :text)))))) 69 | 70 | (deftest ^{:native true} test-fuzzy-suggest-complete-with-location-context 71 | (testing "autocomplete returns only matches nearby" 72 | (let [index-name "people_with_locations" 73 | opts {:context {:location {:lat 90.23 :lon 90.56}}} 74 | res (doc/suggest conn index-name :fuzzy "es" opts)] 75 | (is (map? (:hits res))) 76 | (is (= 1 (-> res :hits :options count))) 77 | (is (= "esjack" (-> res :hits :options first :text))))))) 78 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/avg_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.avg-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-avg-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:avg_age (a/avg "age")}}) 28 | agg (aggregation-from response :avg_age)] 29 | (is (= {:value 29.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/cardinality_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.cardinality-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-cardinality-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:username_count {:cardinality {:field "username"}}}}) 28 | agg (aggregation-from response :username_count)] 29 | (is (>= (:value agg) 4))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/date_histogram_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.date-histogram-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-date-histogram-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/date-histogram "signed_up_at" "1d")}}) 28 | agg (aggregation-from response :age_ranges)] 29 | (is (:buckets agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/date_range_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.date-range-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-date-range-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/date-range "signed_up_at" 28 | "date_hour_minute_second" 29 | [{:from "2012-02-01T00:00:00" :to "2012-02-29T23:59:59"} 30 | {:from "2012-03-01T00:00:00"}])}}) 31 | agg (aggregation-from response :age_ranges)] 32 | (is (:buckets agg))))) 33 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/extended_stats_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.extended-stats-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-extended-stats-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_stats (a/extended-stats "age")}}) 28 | agg (aggregation-from response :age_stats) 29 | keys (set (keys agg))] 30 | (doseq [k #{:count :min :max :avg :sum :std_deviation :sum_of_squares :variance}] 31 | (is (.contains keys k)))))) 32 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/histogram_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.histogram-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-histogram-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_histograms (a/histogram "age" 5)}}) 28 | agg (aggregation-from response :age_histograms)] 29 | (is (:buckets agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/max_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.max-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-max-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:max_age (a/max "age")}}) 28 | agg (aggregation-from response :max_age)] 29 | (is (= {:value 37.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/min_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.min-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-min-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:min_age (a/min "age")}}) 28 | agg (aggregation-from response :min_age)] 29 | (is (= {:value 22.0} agg))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/missing_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.missing-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-missing-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:no_country (a/missing :country)}}) 28 | agg (aggregation-from response :no_country)] 29 | (is (>= (:doc_count agg) 3))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/percentiles_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.percentiles-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-percentiles-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:percentiles_age (a/percentiles "age")}}) 28 | agg (aggregation-from response :percentiles_age)] 29 | (is (= #{:1.0 :5.0 :25.0 :50.0 :75.0 :95.0 :99.0} 30 | (set (keys (get agg :values)))))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/range_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.range-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-range-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:age_ranges (a/range "age" [{:from 15 :to 20} 28 | {:from 21 :to 25} 29 | {:from 26 :to 30} 30 | {:from 31}])}}) 31 | agg (aggregation-from response :age_ranges)] 32 | (is (:buckets agg))))) 33 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/stats_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.stats-aggregation-test 11 | (:refer-clojure :exclude [replace]) 12 | (:require [clojurewerkz.elastisch.rest.document :as doc] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.aggregation :as a] 16 | [clojurewerkz.elastisch.fixtures :as fx] 17 | [clojure.test :refer :all] 18 | [clojurewerkz.elastisch.rest.response :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :aggregation true} test-stats-aggregation 24 | (let [index-name "people" 25 | mapping-type "person" 26 | response (doc/search conn index-name mapping-type 27 | {:query (q/match-all) 28 | :aggregations {:age_stats (a/stats "age")}}) 29 | agg (aggregation-from response :age_stats)] 30 | (is (= #{:count :min :max :avg :sum} (set (keys agg))))))) 31 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/sum_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.sum-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-sum-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:sum_age (a/sum "age")}}) 28 | agg (aggregation-from response :sum_age)] 29 | (is (>= (:value agg) 116))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/terms_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.terms-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-terms-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:title_terms (a/terms "title")}}) 28 | agg (aggregation-from response :title_terms)] 29 | (is (= 6 (count (:buckets agg))))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/aggregations/value_count_aggregation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.aggregations.value-count-aggregation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.aggregation :as a] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :aggregation true} test-value-count-aggregation 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :aggregations {:agg1 (a/value-count "age")}}) 28 | agg (aggregation-from response :agg1)] 29 | (is (>= (:value agg) 4))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/analyze_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.analyze-test 11 | (:refer-clojure :exclude [replace]) 12 | (:require [clojurewerkz.elastisch.rest.document :as doc] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.rest.index :as idx] 15 | [clojurewerkz.elastisch.query :as q] 16 | [clojurewerkz.elastisch.fixtures :as fx] 17 | [clojurewerkz.elastisch.rest.response :refer :all] 18 | [clojure.test :refer :all]) 19 | (:import java.util.UUID)) 20 | 21 | ;; 22 | ;; Analysis 23 | ;; 24 | 25 | (let [conn (rest/connect)] 26 | (deftest ^{:rest true} test-analyze 27 | (is (= {:tokens 28 | [{:token "foo", 29 | :start_offset 0, 30 | :end_offset 3, 31 | :type "", 32 | :position 0} 33 | {:token "bar", 34 | :start_offset 4, 35 | :end_offset 7, 36 | :type "", 37 | :position 1} 38 | {:token "baz", 39 | :start_offset 8, 40 | :end_offset 11, 41 | :type "", 42 | :position 2}]} 43 | (doc/analyze conn "foo bar-baz"))) 44 | (is (= {:tokens 45 | [{:token "foo", 46 | :start_offset 0, 47 | :end_offset 3, 48 | :type "word", 49 | :position 0} 50 | {:token "bar-baz", 51 | :start_offset 4, 52 | :end_offset 11, 53 | :type "word", 54 | :position 1}]} 55 | (doc/analyze conn "foo bar-baz" {:analyzer "whitespace"})))) 56 | 57 | (deftest ^{:rest true} test-analyze-with-map-options 58 | (is (= {:tokens 59 | [{:token "foo", 60 | :start_offset 0, 61 | :end_offset 3, 62 | :type "", 63 | :position 0} 64 | {:token "bar", 65 | :start_offset 4, 66 | :end_offset 7, 67 | :type "", 68 | :position 1} 69 | {:token "baz", 70 | :start_offset 8, 71 | :end_offset 11, 72 | :type "", 73 | :position 2}]} 74 | (doc/analyze conn "foo bar-baz"))) 75 | (is (= {:tokens 76 | [{:token "foo", 77 | :start_offset 0, 78 | :end_offset 3, 79 | :type "word", 80 | :position 0} 81 | {:token "bar-baz", 82 | :start_offset 4, 83 | :end_offset 11, 84 | :type "word", 85 | :position 1}]} 86 | (doc/analyze conn "foo bar-baz" {:analyzer "whitespace"}))))) 87 | 88 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/bulk_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.bulk-test 11 | (:refer-clojure :exclude [replace]) 12 | (:require [clojurewerkz.elastisch.rest.document :as doc] 13 | [clojurewerkz.elastisch.rest.bulk :as bulk] 14 | [clojurewerkz.elastisch.rest.index :as idx] 15 | [clojurewerkz.elastisch.rest :as rest] 16 | [clojurewerkz.elastisch.query :as q] 17 | [clojurewerkz.elastisch.fixtures :as fx] 18 | [cheshire.core :as json] 19 | [clj-http.client :as http] 20 | [clojure.test :refer :all] 21 | [clojurewerkz.elastisch.rest.response :refer [created? acknowledged? conflict? hits-from any-hits? no-hits?]] 22 | [clojure.string :refer [join]])) 23 | 24 | (use-fixtures :each fx/reset-indexes) 25 | 26 | (def ^{:const true} index-name "people") 27 | (def ^{:const true} index-type "person") 28 | 29 | (defn are-all-successful 30 | [xs] 31 | (is (every? (fn [m] (and (:_index m) 32 | (:_type m) 33 | (:_id m) 34 | (:status m))) xs)) 35 | (is (every? created? xs))) 36 | 37 | (let [conn (rest/connect)] 38 | (deftest ^{:rest true :indexing true} test-bulk-with-index 39 | (let [document fx/person-jack 40 | for-index (assoc document :_type index-type) 41 | insert-operations (bulk/bulk-index (repeat 10 for-index)) 42 | response (bulk/bulk-with-index conn index-name insert-operations {:refresh true}) 43 | first-id (-> response :items first :create :_id) 44 | get-result (doc/get conn index-name index-type first-id)] 45 | (are-all-successful (->> response :items (map :create))) 46 | (is (= 10 (:count (doc/count conn index-name index-type)))) 47 | (is (idx/exists? conn index-name)) 48 | (are [expected actual] (= expected (actual get-result)) 49 | document :_source 50 | index-name :_index 51 | index-type :_type 52 | first-id :_id))) 53 | 54 | (deftest ^{:rest true :indexing true} test-bulk-with-index-and-type 55 | (let [document fx/person-jack 56 | insert-operations (bulk/bulk-index (repeat 10 document)) 57 | response (bulk/bulk-with-index-and-type conn index-name index-type insert-operations {:refresh true}) 58 | first-id (-> response :items first :create :_id) 59 | get-result (doc/get conn index-name index-type first-id)] 60 | (are-all-successful (->> response :items (map :create))) 61 | (is (= 10 (:count (doc/count conn index-name index-type)))) 62 | (is (idx/exists? conn index-name)) 63 | (are [expected actual] (= expected (actual get-result)) 64 | document :_source 65 | index-name :_index 66 | index-type :_type 67 | first-id :_id))) 68 | 69 | (deftest ^{:rest true :indexing true} test-bulk-delete 70 | (let [insert-ops (bulk/bulk-index (repeat 10 fx/person-jack)) 71 | response (bulk/bulk-with-index-and-type conn index-name index-type insert-ops {:refresh true}) 72 | docs (->> response :items (map :create) ) 73 | initial-count (:count (doc/count conn index-name index-type)) 74 | delete-response (bulk/bulk-with-index-and-type conn index-name index-type (bulk/bulk-delete docs) {:refresh true})] 75 | (is (= 10 initial-count)) 76 | (are-all-successful (->> response :items (map :create))) 77 | (is (= 0 (:count (doc/count conn index-name index-type)))))) 78 | 79 | (deftest ^{:rest true :indexing true} test-bulk-create 80 | (let [document fx/person-jack 81 | for-index (assoc document :_type index-type :_id "sampleid") 82 | create-operations (bulk/bulk-create (repeat 10 for-index)) 83 | response (bulk/bulk-with-index conn index-name create-operations {:refresh true})] 84 | (is (= 1 (:count (doc/count conn index-name index-type))))))) 85 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/cluster_health_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.cluster-health-test 11 | (:require [clojurewerkz.elastisch.rest.admin :as admin] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojure.test :refer :all])) 15 | 16 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 17 | 18 | (let [conn (rest/connect)] 19 | (deftest ^{:rest true} cluster-health 20 | (let [r (admin/cluster-health conn)] 21 | (is (contains? r :number_of_nodes))) 22 | (let [r (admin/cluster-health conn {:index "tweets"})] 23 | (is (contains? r :number_of_nodes))) 24 | (let [r (admin/cluster-health conn {:index ["tweets" "people"]})] 25 | (is (contains? r :number_of_nodes))) 26 | (let [r (admin/cluster-health conn {:index ["tweets"] :level "shards"})] 27 | (is (contains? r :number_of_nodes))))) 28 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/cluster_state_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.cluster-state-test 11 | (:require [clojurewerkz.elastisch.rest.admin :as admin] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojure.test :refer :all])) 15 | 16 | (let [conn (rest/connect)] 17 | (deftest ^{:rest true} cluster-state 18 | (let [r (admin/cluster-state conn)] 19 | (is (:cluster_name r))) 20 | (let [r (admin/cluster-state conn {:filter_nodes true})] 21 | (is (:cluster_name r))))) 22 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/count_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.count-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer [count-from created?]] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes) 20 | 21 | ;; 22 | ;; count 23 | ;; 24 | 25 | (let [conn (rest/connect)] 26 | (deftest ^{:rest true} test-count-with-the-default-query 27 | (let [index-name "people" 28 | index-type "person"] 29 | (idx/create conn index-name {:mappings fx/people-mapping}) 30 | (doc/create conn index-name index-type fx/person-jack) 31 | (doc/create conn index-name index-type fx/person-joe) 32 | (idx/refresh conn index-name) 33 | (are [c r] (= c (count-from r)) 34 | 2 (doc/count conn index-name index-type)))) 35 | 36 | (deftest ^{:rest true} test-count-with-a-term-query 37 | (let [index-name "people" 38 | index-type "person"] 39 | (idx/create conn index-name {:mappings fx/people-mapping}) 40 | (doc/create conn index-name index-type fx/person-jack) 41 | (doc/create conn index-name index-type fx/person-joe) 42 | (idx/refresh conn index-name) 43 | (are [c r] (= c (count-from r)) 44 | 1 (doc/count conn index-name index-type (q/term :username "esjack")) 45 | 1 (doc/count conn index-name index-type (q/term :username "esjoe")) 46 | 0 (doc/count conn index-name index-type (q/term :username "esmary"))))) 47 | 48 | 49 | (deftest ^{:rest true} test-count-with-mixed-mappings 50 | (let [index-name "people" 51 | index-type "person" 52 | person-properties (:person fx/people-mapping)] 53 | (idx/create conn index-name {:mappings {:person person-properties 54 | :altperson person-properties}}) 55 | (doc/create conn index-name index-type fx/person-jack) 56 | (doc/create conn index-name index-type fx/person-joe) 57 | (doc/create conn index-name "altperson" fx/person-jack) 58 | (idx/refresh conn index-name) 59 | (are [c r] (= c (count-from r)) 60 | 1 (doc/count conn index-name index-type (q/term :username "esjack")) 61 | 1 (doc/count conn index-name "altperson" (q/term :username "esjack")) 62 | 0 (doc/count conn index-name "altperson" (q/term :username "esjoe")))))) 63 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/delete_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.delete-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes) 21 | 22 | (def ^{:const true} index-name "people") 23 | (def ^{:const true} mapping-type "person") 24 | 25 | 26 | (let [conn (rest/connect)] 27 | (deftest ^{:rest true} test-delete-when-a-document-exists 28 | (let [id "1"] 29 | (doc/put conn index-name mapping-type id fx/person-jack) 30 | (is (doc/present? conn index-name mapping-type id)) 31 | (is (found? (doc/delete conn index-name mapping-type id))) 32 | (is (not (doc/present? conn index-name mapping-type id)))))) 33 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/document_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.document-test 11 | (:require [clojurewerkz.elastisch.rest :as es] 12 | [clojurewerkz.elastisch.rest.document :as es.document] 13 | clj-http.core 14 | [clojure.test :refer :all]) 15 | (:import java.io.ByteArrayInputStream 16 | clojure.lang.ExceptionInfo)) 17 | 18 | (deftest ^:rest test-http-options-overridability 19 | (let [default-conn (es/connect) 20 | throwing-conn (es/connect "http://localhost:9200" 21 | {:throw-exceptions true}) 22 | no-throwing-conn (es/connect "http://localhost:9200" 23 | {:throw-exceptions false}) 24 | index "arbitrary_index" 25 | mapping "arbitrary_mapping" 26 | id "arbitrary_id" 27 | doc {"arbitrary_attribute" "arbitrary value"}] 28 | (with-redefs [clj-http.core/request (constantly {:status 400 29 | :body (.getBytes "{\"error\": \"This is an example exception.\", \"status\": 400}" 30 | "UTF-8")})] 31 | (testing "that the functions that default to swallowing exceptions can be configured to actually throw them" 32 | (is (= (es.document/put default-conn index mapping id doc) 33 | {:error "This is an example exception.", :status 400})) 34 | (is (thrown-with-msg? ExceptionInfo 35 | #"clj-http: status 400" 36 | (es.document/put throwing-conn index mapping id doc)))) 37 | (testing "that the functions that default to throwing exceptions can be configured to swallow them" 38 | (is (thrown-with-msg? ExceptionInfo 39 | #"clj-http: status 400" 40 | (es.document/create default-conn index mapping doc {:id id}))) 41 | (is (= (es.document/create no-throwing-conn index mapping doc {:id id}) 42 | {:error "This is an example exception.", :status 400})))))) 43 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/filtering_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.filtering-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-articles-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true} test-term-filtering 23 | (let [index-name "people" 24 | mapping-type "person" 25 | hits (hits-from (doc/search conn index-name mapping-type 26 | {:query (q/match-all) 27 | :filter {:term {:username "esmary"}}}))] 28 | (is (= 1 (count hits))) 29 | (is (= "Lindey" (-> hits first source-from :last-name))))) 30 | 31 | (deftest ^{:rest true} test-range-filtering 32 | (let [index-name "people" 33 | mapping-type "person" 34 | hits (hits-from (doc/search conn index-name mapping-type 35 | {:query (q/match-all) 36 | :filter {:range {:age {:from 26 :to 30}}}}))] 37 | (is (= 2 (count hits))) 38 | (is (#{28 29} (-> hits first source-from :age)))))) 39 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/get_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.get-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes) 20 | 21 | (def ^{:const true} index-name "people") 22 | (def ^{:const true} mapping-type "person") 23 | 24 | (let [conn (rest/connect)] 25 | (deftest ^{:rest true} test-get-with-non-existing-document 26 | (is (nil? (doc/get conn "pages" "page" "as8d8as882jk2jk9#d77$%88s7")))) 27 | 28 | (deftest ^{:rest true} test-get-with-existing-id-that-needs-url-encoding 29 | (let [id "http://www.faz.net/artikel/C31325/piratenabwehr-keine-kriegswaffen-fuer-private-dienste-30683040.html"] 30 | (doc/put conn "pages" "page" id {:url id}) 31 | (is (doc/get conn "pages" "page" id)))) 32 | 33 | (deftest ^{:rest true} test-present-with-non-existing-id 34 | (is (not (doc/present? conn index-name mapping-type "1")))) 35 | 36 | (deftest ^{:rest true} test-present-with-existing-id 37 | (doc/put conn index-name mapping-type "1" fx/person-jack) 38 | (is (doc/present? conn index-name mapping-type "1"))) 39 | 40 | (deftest ^{:rest true} multi-get-test 41 | (doc/put conn index-name mapping-type "1" fx/person-jack) 42 | (doc/put conn index-name mapping-type "2" fx/person-mary) 43 | (doc/put conn index-name mapping-type "3" fx/person-joe) 44 | (let [mget-result (doc/multi-get conn 45 | [{:_index index-name :_type mapping-type :_id "1"} 46 | {:_index index-name :_type mapping-type :_id "2"}])] 47 | (is (= fx/person-jack (source-from (first mget-result)))) 48 | (is (= fx/person-mary (source-from (second mget-result))))) 49 | (let [mget-result (doc/multi-get conn index-name 50 | [{:_type mapping-type :_id "1"} 51 | {:_type mapping-type :_id "2"}])] 52 | (is (= fx/person-jack (source-from (first mget-result)))) 53 | (is (= fx/person-mary (source-from (second mget-result))))) 54 | (let [mget-result (doc/multi-get conn index-name mapping-type 55 | [{:_id "1"} {:_id "2"}])] 56 | (is (= fx/person-jack (source-from (first mget-result)))) 57 | (is (= fx/person-mary (source-from (second mget-result))))))) 58 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/highlighting_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.highlighting-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true} test-highlighting-with-all-defaults 24 | (let [index "articles" 25 | type "article"] 26 | (idx/create conn index {:mappings fx/articles-mapping}) 27 | (doc/put conn index type "1" fx/article-on-elasticsearch) 28 | (doc/put conn index type "2" fx/article-on-lucene) 29 | (doc/put conn index type "3" fx/article-on-nueva-york) 30 | (doc/put conn index type "4" fx/article-on-austin) 31 | (idx/refresh conn index) 32 | (let [resp (doc/search conn index type 33 | {:query (q/query-string {:query "software" :default_field "summary"}) 34 | :highlight {:fields {:summary {}}}}) 35 | hits (hits-from resp)] 36 | (is (re-find #"software" (-> hits first :highlight :summary first))))))) 37 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/mappings_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.mappings-test 11 | (:require [clojurewerkz.elastisch.rest :as rest] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojurewerkz.elastisch.rest.response :refer :all] 15 | [clojure.test :refer :all])) 16 | 17 | (use-fixtures :each fx/reset-indexes) 18 | 19 | (let [conn (rest/connect)] 20 | (deftest ^{:rest true} test-getting-index-mapping 21 | (let [index "people1" 22 | mappings fx/people-mapping 23 | response (idx/create conn index {:mappings mappings})] 24 | (is (created-or-acknowledged? response)) 25 | (is (get-in (idx/get-mapping conn index) [:people1 :mappings :person :properties :username :store])))) 26 | 27 | (deftest ^{:rest true} test-updating-index-mapping 28 | (let [index "people2" 29 | mapping2 {:person {:properties {:first-name {:type "string" :store "yes"}}}} 30 | mapping fx/people-mapping 31 | _ (idx/create conn index {:mappings mapping2}) 32 | response (idx/update-mapping conn index "person" {:mapping mapping})] 33 | (is (created-or-acknowledged? response)))) 34 | 35 | (deftest ^{:rest true} test-updating-blank-index-mapping 36 | (let [index "people4" 37 | mapping fx/people-mapping 38 | _ (idx/create conn index {:mappings {}}) 39 | response (idx/update-mapping conn index "person" {:mapping mapping})] 40 | (is (created-or-acknowledged? response)) 41 | (is (get-in (idx/get-mapping conn index) [:people4 :mappings :person :properties :username :store]))))) 42 | 43 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/more_like_this_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.more-like-this-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true} test-more-like-this 24 | (let [index "articles" 25 | type "article"] 26 | (idx/create conn index {:mappings fx/articles-mapping}) 27 | (doc/put conn index type "1" fx/article-on-elasticsearch) 28 | (doc/put conn index type "2" fx/article-on-lucene) 29 | (doc/put conn index type "3" fx/article-on-nueva-york) 30 | (doc/put conn index type "4" fx/article-on-austin) 31 | (idx/refresh conn index) 32 | (let [response (doc/more-like-this conn index type 33 | {:ids ["2"] 34 | :fields ["tags"] 35 | :min_term_freq 1 36 | :min_doc_freq 1})] 37 | (is (= 1 (total-hits response))) 38 | (is (= fx/article-on-elasticsearch 39 | (-> (hits-from response) first source-from))))))) 40 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/multi_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.multi-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.multi :as multi] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-articles-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true} test-multi-search 23 | (let [res1 (doc/search conn "people" "person" {:query (q/match-all) :size 1}) 24 | res2 (doc/search conn "articles" "article" {:query (q/match-all) :size 1}) 25 | multires (multi/search conn [{:index "people" 26 | :type "person"} {:query (q/match-all) :size 1} 27 | {:index "articles" 28 | :type "article"} {:query (q/match-all) :size 1} 29 | {:index "tweets" 30 | :type "tweet"} {:query (q/match-all) :size 1}])] 31 | (is (= 3 (count multires))) 32 | (is (= (-> res1 hits-from first source-from) 33 | (-> multires first hits-from first source-from))) 34 | (is (= (-> res2 hits-from first source-from) 35 | (-> multires second hits-from first source-from))))) 36 | 37 | (deftest ^{:rest true} test-multi-with-index-and-type 38 | (let [res1 (doc/search conn "people" "person" {:query (q/term :planet "earth")}) 39 | res2 (doc/search conn "people" "person" {:query (q/term :first-name "mary")}) 40 | multires (multi/search-with-index-and-type conn 41 | "people" "person" 42 | [{} {:query (q/term :planet "earth")} 43 | {} {:query (q/term :first-name "mary")} 44 | {} {:query (q/match-all)}])] 45 | (is (= 3 (count multires))) 46 | (is (= (res1 :hits) (-> multires first :hits))) 47 | (is (= (res2 :hits) (-> multires second :hits)))))) 48 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/nodes_info_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.nodes-info-test 11 | (:require [clojurewerkz.elastisch.rest :as rest] 12 | [clojurewerkz.elastisch.rest.admin :as admin] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojure.test :refer :all])) 15 | 16 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 17 | 18 | (let [conn (rest/connect)] 19 | (deftest ^{:rest true} test-nodes-info 20 | (testing "basic info" 21 | (let [info (admin/nodes-info conn)] 22 | (is (:nodes info)) 23 | (is (:cluster_name info)))) 24 | (testing "node selection" 25 | (let [info (admin/nodes-info conn) 26 | node-id (first (keys (:nodes info))) 27 | node-name (get-in info [:nodes node-id :name])] 28 | (is (empty? (:nodes (admin/nodes-info conn {:nodes ["foo"]})))) 29 | (is (= 1 (count (:nodes (admin/nodes-info conn {:nodes (name node-id)}))))) 30 | (is (= 1 (count (:nodes (admin/nodes-info conn {:nodes (vector (name node-id))}))))) 31 | (is (= 1 (count (:nodes (admin/nodes-info conn {:nodes node-name}))))))) 32 | (testing "parameters" 33 | (is (not (= (admin/nodes-info conn {:attributes ["plugins"]}) 34 | (admin/nodes-info conn {:attributes ["os"]}))))))) 35 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/nodes_stats_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.nodes-stats-test 11 | (:require [clojurewerkz.elastisch.rest.admin :as admin] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.fixtures :as fx] 14 | [clojure.test :refer :all])) 15 | 16 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 17 | 18 | (defn- node-stats-group 19 | [stats node-id stats-group] 20 | (-> stats :nodes node-id stats-group)) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true} nodes-stats 24 | (is (= #{:cluster_name :nodes} 25 | (into #{} (keys (admin/nodes-stats conn))))) 26 | (let [stats (admin/nodes-stats conn) 27 | node-id (first (keys (:nodes stats))) 28 | node-name (get-in stats [:nodes node-id :name])] 29 | 30 | (testing "node selection" 31 | (is (empty? (:nodes (admin/nodes-stats conn {:nodes "foo"})))) 32 | (is (= 1 (count (:nodes (admin/nodes-stats conn {:nodes (name node-id)}))))) 33 | (is (= 1 (count (:nodes (admin/nodes-stats conn {:nodes (vector (name node-id))}))))) 34 | (is (= 1 (count (:nodes (admin/nodes-stats conn {:nodes node-name})))))) 35 | 36 | (testing "parameters" 37 | (is (node-stats-group (admin/nodes-stats conn {:attributes ["indices"]}) 38 | node-id 39 | :indices)) 40 | (is (nil? (node-stats-group (admin/nodes-stats conn {:attributes ["fs"]}) 41 | node-id 42 | :indices))))))) 43 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/percolation_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.percolation-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.percolation :as pcl] 17 | [clojurewerkz.elastisch.rest.response :refer :all] 18 | [clojure.test :refer :all])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :percolation true} test-percolation-case-1 24 | (let [index-name "articles" 25 | percolator "article" 26 | result1 (pcl/register-query conn index-name percolator {:query {:term {:title "search"}}}) 27 | result2 (pcl/percolate conn index-name percolator {:doc {:title "You know, for search"}})] 28 | (is (= [{:_index "articles" :_id "article"}] (matches-from result2))) 29 | (pcl/unregister-query conn index-name percolator))) 30 | 31 | (deftest ^{:rest true :percolation true} test-percolation-existing-doc 32 | (let [index-name "articles" 33 | percolator "article" 34 | response (doc/put conn index-name percolator "123" {:title "You know, for search"}) 35 | result1 (pcl/register-query conn index-name percolator {:query {:term {:title "search"}}}) 36 | result2 (pcl/percolate-existing conn index-name percolator "123")] 37 | (is (= [{:_index "articles" :_id "article"}] (matches-from result2))) 38 | (pcl/unregister-query conn index-name percolator)))) 39 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/bool_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.bool-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojure.test :refer :all] 16 | [clojurewerkz.elastisch.rest.response :refer :all])) 17 | 18 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 19 | 20 | (let [conn (rest/connect)] 21 | (deftest ^{:rest true :query true} test-basic-bool-query 22 | (let [index-name "people" 23 | mapping-type "person" 24 | response (doc/search conn index-name mapping-type {:query (q/bool {:must {:term {:planet "earth"}} 25 | :should {:range {:age {:from 20 :to 30}}} 26 | :minimum_number_should_match 1})})] 27 | (is (any-hits? response)) 28 | (is (= (sort (ids-from response)) (sort ["1" "2" "4"]))) 29 | (is (= 3 (total-hits response)))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/filtered_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.filtered-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojure.test :refer :all] 17 | [clojurewerkz.elastisch.rest.response :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-basic-filtered-query 24 | (let [index-name "people" 25 | mapping-type "person" 26 | response (doc/search conn index-name mapping-type {:query (q/filtered {:query (q/term :planet "earth") 27 | :filter {:range {:age {:from 20 :to 30}}}})})] 28 | (is (any-hits? response)) 29 | (is (= 3 (total-hits response)))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/fuzzy_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.fuzzy-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-basic-fuzzy-query-with-string-fields 24 | (let [index-name "articles" 25 | mapping-type "article" 26 | response (doc/search conn index-name mapping-type {:query (q/fuzzy {:title "Nueva"})}) 27 | hits (hits-from response)] 28 | (is (any-hits? response)) 29 | (is (= 1 (total-hits response))) 30 | (is (= #{"3"} (ids-from response))))) 31 | 32 | (deftest ^{:rest true :query true} test-basic-fuzzy-query-with-numeric-fields 33 | (let [index-name "articles" 34 | mapping-type "article" 35 | response (doc/search conn index-name mapping-type {:query (q/fuzzy {:number-of-edits {:value 13000 :fuzziness 3}})}) 36 | hits (hits-from response)] 37 | (is (any-hits? response)) 38 | (is (= 1 (total-hits response))) 39 | (is (= #{"4"} (ids-from response))))) 40 | 41 | (deftest ^{:rest true :query true} test-basic-fuzzy-query-with-date-fields 42 | (let [index-name "articles" 43 | mapping-type "article" 44 | response (doc/search conn index-name mapping-type {:query (q/fuzzy {"latest-edit.date" {:value "2012-03-25T12:00:00" :fuzziness "1d"}})}) 45 | hits (hits-from response)] 46 | (is (any-hits? response)) 47 | (is (= 1 (total-hits response))) 48 | (is (= #{"1"} (ids-from response)))))) 49 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/ids_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.ids-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojure.set :as cs] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :query true} test-basic-ids-query 23 | (let [response (doc/search conn "tweets" "tweet" {:query (q/ids "tweet" ["1" "2" "8ska88"])})] 24 | (is (any-hits? response)) 25 | (is (= 2 (total-hits response))) 26 | (is (= #{"1" "2"} (set (map :_id (hits-from response)))))))) 27 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/match_all_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.match-all-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :query true} test-basic-match-all-query 23 | (let [index-name "articles" 24 | mapping-type "article" 25 | response (doc/search conn index-name mapping-type {:query (q/match-all)}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 4 (total-hits response)))))) 29 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/mlt_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.mlt-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.rest.index :as idx] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:query true :rest true} test-more-like-this-query 24 | (let [index-name "articles" 25 | mapping-type "article" 26 | response (doc/search conn index-name mapping-type {:query (q/mlt {:like_text "technology, opensource, search, full-text search, distributed, software, lucene" 27 | :fields ["tags"] :min_term_freq 1 :min_doc_freq 1})})] 28 | (is (= 2 (total-hits response))) 29 | (is (= #{"1" "2"} (ids-from response)))))) 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/prefix_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.prefix-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :query true} test-basic-prefix-query 23 | (let [index-name "people" 24 | mapping-type "person" 25 | response (doc/search conn index-name mapping-type {:query (q/prefix {:username "esj"})}) 26 | hits (hits-from response)] 27 | (is (any-hits? response)) 28 | (is (= 2 (total-hits response))) 29 | (is (= #{"1" "3"} (set (map :_id hits)))))) 30 | 31 | (deftest ^{:rest true :query true} test-full-word-prefix-query-over-a-text-field-analyzed-with-the-standard-analyzer 32 | (let [index-name "tweets" 33 | mapping-type "tweet" 34 | response (doc/search conn index-name mapping-type {:query (q/prefix {:text "why"})}) 35 | hits (hits-from response)] 36 | (is (= 1 (total-hits response))) 37 | (is (= "4" (-> hits first :_id))))) 38 | 39 | (deftest ^{:rest true :query true} test-partial-prefix-query-over-a-text-field 40 | (let [index-name "tweets" 41 | mapping-type "tweet" 42 | response (doc/search conn index-name mapping-type {:query (q/prefix {:text "congr"})}) 43 | hits (hits-from response)] 44 | (is (= 1 (total-hits response))) 45 | (is (= "3" (-> hits first :_id))))) 46 | 47 | (deftest ^{:rest true :query true} test-partial-prefix-query-over-a-text-field-with-map-options 48 | (let [index-name "tweets" 49 | mapping-type "tweet" 50 | m {:text "congr"} 51 | response (doc/search conn index-name mapping-type {:query (q/prefix m)}) 52 | hits (hits-from response)] 53 | (is (= 1 (total-hits response))) 54 | (is (= "3" (-> hits first :_id)))))) 55 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/query_string_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.query-string-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-query-string-query 24 | (let [index-name "articles" 25 | mapping-type "article" 26 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "Austin" :default_field "title"})})] 27 | (is (= 1 (total-hits response))) 28 | (is (= #{"4"} (ids-from response))))) 29 | 30 | (deftest ^{:rest true :query true} test-query-string-query-across-all-mapping-types 31 | (let [index-name "articles" 32 | response (doc/search-all-types conn index-name {:query (q/query-string {:query "Austin" :default_field "title"})})] 33 | (is (= 1 (total-hits response))) 34 | (is (= #{"4"} (ids-from response))))) 35 | 36 | (deftest ^{:rest true :query true} test-query-string-query-across-all-indexes-and-mapping-types 37 | (let [response (doc/search-all-indexes-and-types conn {:query (q/query-string {:query "Austin" :default_field "title"})})] 38 | (is (= 1 (total-hits response))) 39 | (is (= #{"4"} (ids-from response))))) 40 | 41 | (deftest ^{:rest true :query true} test-query-string-query-over-a-text-field-analyzed-with-the-standard-analyzer-case1 42 | (let [index-name "tweets" 43 | mapping-type "tweet" 44 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "cloud+"})}) 45 | hits (hits-from response)] 46 | (is (= 1 (total-hits response))) 47 | (is (= "5" (-> hits first :_id))))) 48 | 49 | (deftest ^{:rest true :query true} test-query-string-query-over-a-text-field-analyzed-with-the-standard-analyzer-case2 50 | (let [index-name "tweets" 51 | mapping-type "tweet" 52 | response (doc/search conn index-name mapping-type {:query (q/query-string {:query "cloud AND (NOT adoption)"})}) 53 | hits (hits-from response)] 54 | (is (= 0 (total-hits response)))))) 55 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/range_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.range-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-range-query-over-numerical-field 24 | (let [index-name "people" 25 | mapping-type "person" 26 | response (doc/search conn index-name mapping-type {:query (q/range :age {:from 27 :to 29})}) 27 | hits (hits-from response)] 28 | (is (any-hits? response)) 29 | (is (= 2 (total-hits response))) 30 | (is (= #{"2" "4"} (set (map :_id hits)))))) 31 | 32 | 33 | (let [index-name "tweets" 34 | mapping-type "tweet"] 35 | (deftest ^{:rest true :query true} test-range-query-over-string-field 36 | (let [response (doc/search conn index-name mapping-type {:query (q/range :username {:from "c" :to "j"})}) 37 | ids (ids-from response)] 38 | (is (= 2 (total-hits response))) 39 | (is (= #{"1" "2"} ids)))) 40 | 41 | (deftest ^{:rest true :query true} test-range-query-over-date-time-field-with-from 42 | (let [response (doc/search conn index-name mapping-type {:query (q/range :timestamp {:from "20120801T160000+0100"})}) 43 | ids (ids-from response)] 44 | (is (= 2 (total-hits response))) 45 | (is (= #{"1" "2"} ids)))) 46 | 47 | (deftest ^{:rest true :query true} test-range-query-over-date-time-field-with-from-and-to 48 | (let [response (doc/search conn index-name mapping-type {:query (q/range :timestamp {:from "20120801T160000+0100" :to "20120801T180000+0100"})}) 49 | ids (ids-from response)] 50 | (is (= 1 (total-hits response))) 51 | (is (= #{"2"} ids)))))) 52 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/span_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.span-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index fx/prepopulate-people-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-span-first-query 24 | ;; Finding the document that contains the word “eating” (i.e., 25 | ;; [[clojurewerkz.elastisch.fixtures/person-jack]]) requires searching for 26 | ;; the word “eat” —not “eating”— because the `biography` field is indexed 27 | ;; with the `snowball` analyzer. 28 | (let [response (doc/search conn "people" "person" {:query (q/span-first {:match {:span_term {:biography "eat"}} :end 5})}) 29 | hits (hits-from response)] 30 | (is (any-hits? response)) 31 | (is (= 1 (total-hits response))) 32 | (is (= #{"1"} (set (map :_id hits)))))) 33 | 34 | (deftest ^{:rest true :query true} test-span-near-query 35 | ;; Similarly to the previous test, we search for “document” instead of 36 | ;; “documents” (which is what [[clojurewerkz.elastisch.fixtures/article-on-elasticsearch]]) 37 | ;; *actually* contains). 38 | (let [response (doc/search conn "articles" "article" {:query (q/span-near {:clauses [{:span_term {:summary "search"}} 39 | {:span_term {:summary "document"}}] :slop 5 :in_order true})}) 40 | hits (hits-from response)] 41 | (is (any-hits? response)) 42 | (is (= 1 (total-hits response))) 43 | (is (= #{"1"} (set (map :_id hits))))))) 44 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/term_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.term-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all] 18 | [clj-time.core :refer [months ago now from-now]])) 19 | 20 | (use-fixtures :each fx/reset-indexes fx/prepopulate-people-index fx/prepopulate-tweets-index) 21 | 22 | (let [conn (rest/connect)] 23 | (deftest ^{:rest true :query true} test-basic-term-query-with-person-mapping 24 | (let [result (doc/search conn "people" "person" {:query (q/term :biography "avoid")})] 25 | (is (any-hits? result)) 26 | (is (= fx/person-jack (-> result hits-from first source-from))))) 27 | 28 | 29 | (deftest ^{:rest true :query true} test-term-query-with-person-mapping-and-a-limit 30 | (let [result (doc/search conn "people" "person" {:query (q/term :planet "earth") :size 2})] 31 | (is (any-hits? result)) 32 | (is (= 2 (count (hits-from result)))) 33 | ;; but total # of hits is reported w/o respect to the limit. MK. 34 | (is (= 4 (total-hits result))))) 35 | 36 | (deftest ^{:rest true :query true} test-basic-term-query-with-tweets-mapping 37 | (let [result (doc/search conn "tweets" "tweet" {:query (q/term :text "improved")})] 38 | (is (any-hits? result)) 39 | (is (= fx/tweet1 (-> result hits-from first source-from))))) 40 | 41 | (deftest ^{:rest true :query true} test-basic-terms-query-with-tweets-mapping 42 | (let [result (doc/search conn "tweets" "tweet" {:query (q/term :text ["supported" "improved"])})] 43 | (is (any-hits? result)) 44 | (is (= fx/tweet1 (-> result hits-from first source-from))))) 45 | 46 | (deftest ^{:rest true :query true} test-basic-term-query-over-non-analyzed-usernames 47 | (are [username id] (= id (-> (doc/search conn "tweets" "tweet" {:query (q/term :username username) :sort {:timestamp "asc"}}) 48 | hits-from 49 | first 50 | :_id)) 51 | "clojurewerkz" "1" 52 | "ifesdjeen" "2" 53 | "michaelklishin" "4" 54 | "DEVOPS_BORAT" "5")) 55 | 56 | (deftest ^{:rest true :query true} test-basic-term-query-over-non-analyzed-embedded-fields 57 | (are [state id] (= id (-> (doc/search conn "tweets" "tweet" {:query (q/term "location.state" state) :sort {:timestamp "asc"}}) 58 | hits-from 59 | first 60 | :_id)) 61 | "Moscow" "4" 62 | "CA" "5"))) 63 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/type_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.type-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest :as rest] 13 | [clojurewerkz.elastisch.query :as q] 14 | [clojurewerkz.elastisch.fixtures :as fx] 15 | [clojurewerkz.elastisch.rest.response :refer :all] 16 | [clojure.test :refer :all])) 17 | 18 | (use-fixtures :each fx/reset-indexes fx/prepopulate-tweets-index) 19 | 20 | (let [conn (rest/connect)] 21 | (deftest ^{:query true :rest true} test-basic-type-query 22 | (let [response (doc/search conn "tweets" "tweet" 23 | {:query (q/type "tweet")})] 24 | (is (any-hits? response)) 25 | (is (= 5 (total-hits response))) 26 | (is (= #{"tweet"} (set (map :_type (hits-from response)))))))) 27 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/queries/wildcard_query_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.queries.wildcard-query-test 11 | (:require [clojurewerkz.elastisch.rest.document :as doc] 12 | [clojurewerkz.elastisch.rest.index :as idx] 13 | [clojurewerkz.elastisch.rest :as rest] 14 | [clojurewerkz.elastisch.query :as q] 15 | [clojurewerkz.elastisch.fixtures :as fx] 16 | [clojurewerkz.elastisch.rest.response :refer :all] 17 | [clojure.test :refer :all])) 18 | 19 | (use-fixtures :each fx/reset-indexes fx/prepopulate-articles-index fx/prepopulate-tweets-index) 20 | 21 | (let [conn (rest/connect)] 22 | (deftest ^{:rest true :query true} test-trailing-wildcard-query-with-nested-fields 23 | (let [response (doc/search conn "articles" "article" {:query (q/wildcard {"latest-edit.author" "Thorw*"})}) 24 | hits (hits-from response)] 25 | (is (any-hits? response)) 26 | (is (= 1 (total-hits response))) 27 | (is (= "2" (-> hits first :_id))))) 28 | 29 | 30 | (deftest ^{:rest true :query true} test-leading-wildcard-query-with-non-analyzd-field 31 | (let [response (doc/search conn "tweets" "tweet" {:query (q/wildcard {:username "*werkz"})}) 32 | hits (hits-from response)] 33 | (is (any-hits? response)) 34 | (is (= 1 (total-hits response))) 35 | (is (= "1" (-> hits first :_id)))))) 36 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/rest_api/response_test.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.rest-api.response-test 11 | (:refer-clojure :exclude [replace]) 12 | (:require [clojurewerkz.elastisch.rest.response :as resp] 13 | [clojure.test :refer :all])) 14 | 15 | (deftest ^{:rest true} test-created? 16 | (is (resp/created? 17 | {:_id "id" 18 | :_index "idx" 19 | :_type "type" 20 | :_version 1 21 | :status 201})) 22 | (is (false? (resp/created? 23 | {:_id "id" 24 | :_index "idx" 25 | :_type "type" 26 | :_version 1 27 | :status 200})))) 28 | 29 | 30 | -------------------------------------------------------------------------------- /test/clojurewerkz/elastisch/test/helpers.clj: -------------------------------------------------------------------------------- 1 | ;; Copyright (c) 2011-2019 Michael S. Klishin, Alex Petrov, and the ClojureWerkz Team 2 | ;; 3 | ;; The use and distribution terms for this software are covered by the 4 | ;; Eclipse Public License 1.0 (http://opensource.org/licenses/eclipse-1.0.php) 5 | ;; which can be found in the file epl-v10.html at the root of this distribution. 6 | ;; By using this software in any fashion, you are agreeing to be bound by 7 | ;; the terms of this license. 8 | ;; You must not remove this notice, or any other, from this software. 9 | 10 | (ns clojurewerkz.elastisch.test.helpers 11 | (:require [clojurewerkz.elastisch.native :as es])) 12 | 13 | (defn ci? 14 | "Returns true if tests are running in the CI environment 15 | (on travis-ci.org)" 16 | [] 17 | (System/getenv "CI")) 18 | 19 | (defn infer-cluster-name 20 | "Returns current cluster name set via the `ES_CLUSTER_NAME` env variable" 21 | [] 22 | (get (System/getenv) "ES_CLUSTER_NAME" "elasticsearch")) 23 | 24 | (defn infer-cluster-host 25 | "returns cluster host ip from `ES_CLUSTER_HOST` env variable" 26 | [] 27 | (get (System/getenv) "ES_CLUSTER_HOST" "127.0.0.1")) 28 | 29 | (defn connect-native-client 30 | ([] 31 | (connect-native-client (infer-cluster-name) [[(infer-cluster-host) 9300]])) 32 | ([cluster-name] 33 | (connect-native-client cluster-name [[(infer-cluster-host) 9300]])) 34 | ([cluster-name host-pairs] 35 | (es/connect host-pairs {"cluster.name" cluster-name}))) 36 | --------------------------------------------------------------------------------