{"id":20899626,"url":"https://github.com/tlrx/elasticsearch-test","last_synced_at":"2025-07-22T09:10:35.570Z","repository":{"id":3476609,"uuid":"4531751","full_name":"tlrx/elasticsearch-test","owner":"tlrx","description":"elasticsearch-test, a framework that makes elasticsearch unit testing a breeze.","archived":false,"fork":false,"pushed_at":"2016-09-17T16:03:56.000Z","size":930,"stargazers_count":154,"open_issues_count":10,"forks_count":53,"subscribers_count":19,"default_branch":"master","last_synced_at":"2025-05-20T09:05:35.997Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Java","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/tlrx.png","metadata":{"files":{"readme":"README.textile","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2012-06-02T19:22:14.000Z","updated_at":"2024-10-16T02:35:27.000Z","dependencies_parsed_at":"2022-09-04T03:11:56.954Z","dependency_job_id":null,"html_url":"https://github.com/tlrx/elasticsearch-test","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"purl":"pkg:github/tlrx/elasticsearch-test","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlrx%2Felasticsearch-test","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlrx%2Felasticsearch-test/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlrx%2Felasticsearch-test/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlrx%2Felasticsearch-test/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/tlrx","download_url":"https://codeload.github.com/tlrx/elasticsearch-test/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/tlrx%2Felasticsearch-test/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":266463559,"owners_count":23932904,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","status":"online","status_checked_at":"2025-07-22T02:00:09.085Z","response_time":66,"last_error":null,"robots_txt_status":null,"robots_txt_updated_at":null,"robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-18T11:14:55.764Z","updated_at":"2025-07-22T09:10:35.525Z","avatar_url":"https://github.com/tlrx.png","language":"Java","funding_links":[],"categories":["测试"],"sub_categories":[],"readme":"elasticsearch-test, a framework that makes \"ElasticSearch\":http://www.elasticsearch.org/ unit testing a breeze.\n\n*Warning*: Since 0.90.7, the Elasticsearch team provides a JAR file containing all the test classes. This JAR can be included in third party projects. See the \"pull request\":https://github.com/elasticsearch/elasticsearch/pull/4126 for more information.\n\n\nHere you will find:\n* a \u003cb\u003esimple Java API\u003c/b\u003e that can be used to easily set up ElasticSearch in your unit tests\n* some \u003cb\u003eJava annotations\u003c/b\u003e that simplifies JUnit testing with ElasticSearch\n\n\nh3. Versions\n\n|_. elasticsearch-test\t|_. ElasticSearch|\n|   master (1.2.2-SNAPSHOT)     |     1.2.2     |\n|   1.2.1               |     1.2.1      |\n|   1.1.0               |     1.1.0      |\n|   0.90.6\t\t        |     0.90.6\t |\n|   0.90.5\t\t        |     0.90.5\t |\n|   0.0.10              |     0.90.3     |\n|   0.0.9\t\t        |     0.90.1     |\n|   0.0.8\t\t        |     0.90.0.Beta1   |\n|   0.0.7               |     0.20.1     |\n|   0.0.6     \t\t    |     0.20.0     |\n|   0.0.5\t\t        |     0.19.11    |\n|   0.0.4               |     0.19.8     |\n|   0.0.3               |     0.19.7     |\n|   0.0.2               |     0.19.4     |\n|   0.0.1\t\t        |     0.19.4     |\n\nh3. Maven dependency\n\nAdd a new dependency in your Maven POM file:\n\nbc. \u003cdependency\u003e\n  \u003cgroupId\u003ecom.github.tlrx\u003c/groupId\u003e\n  \u003cartifactId\u003eelasticsearch-test\u003c/artifactId\u003e\n  \u003cversion\u003e1.2.1\u003c/version\u003e\n\u003c/dependency\u003e\n\n\nh2. Using the Java API\n\nThe @EsSetup@ class helps to start and stop a local ElasticSearch node. Then, the @EsSetup.execute()@ method \ncan be used to create indices (with mappings, settings and templates support) and import test data.\n\nThe usage of @EsSetup@ is pretty self explanatory:\n\u003cpre\u003e\u003ccode\u003e\nimport static com.github.tlrx.elasticsearch.test.EsSetup.*;\n\npublic class MyTest {\n        EsSetup esSetup;\n\t@Before public void setUp() throws Exception {\n                // Instantiates a local node \u0026 client\n                esSetup = new EsSetup();\n                // Clean all, and creates some indices\n                esSetup.execute(\n                        deleteAll(),\n                        createIndex(\"my_index_1\"),\n                        createIndex(\"my_index_2\")\n                                .withSettings(fromClassPath(\"path/to/settings.json\"))\n                                .withMapping(\"type1\", fromClassPath(\"path/to/mapping/of/type1.json\"))\n                                .withData(fromClassPath(\"path/to/bulk.json\")),\n                        createTemplate(\"template-1\")\n                                .withSource(fromClassPath(\"path/to/template1.json\")),\n                );\n        }\n        @Test public void testMethod() {\n                // check if the index exists\n                assertTrue(esSetup.exists(\"my_index_2\"));\n\t\t// Index a new document\n\t\tesSetup.execute(index(\"my_index_2\", \"type1\", \"1\").withSource(\"{ \\\"field1\\\" : \\\"value1\\\" }\"));\n                // Count the number of documents\n                Long nb = esSetup.countAll();\n\t\t// Delete a document\n\t\tesSetup.execute(delete(\"my_index_2\", \"type1\", \"1\"));\n                // Clean all indices\n                esSetup.execute(deleteAll());\n        }\n        @After public void tearDown() throws Exception {\n                // This will stop and clean the local node\n                esSetup.terminate();\n        }\t\n}\u003c/code\u003e\u003c/pre\u003e\nMore usages can be found in \"BasicTest.java\":https://github.com/tlrx/elasticsearch-test/blob/master/src/test/java/com/github/tlrx/elasticsearch/test/BasicTest.java\n\n\n\nh2. Using with JUnit\n\nDefine the JUnit @Runner@ to use in your test classes with the annotation @@RunWith(ElasticsearchRunner.class)@ :\n\n\u003cpre\u003e\npackage com.github.tlrx.elasticsearch.samples.core;\n\n@RunWith(ElasticsearchRunner.class)\npublic class VersionTest {\n\t...\n}\n\u003c/pre\u003e\n\n\nh3. Annotations\n\nThe library provides the following annotations:\n\n|_. Annotation\t\t\t|_. \t\t \t\t\t\t\t\t|\n|   @ElasticsearchNode      \t|     Instantiate an elasticsearch Node     \t\t\t|\n|   @ElasticsearchClient      \t|     Instantiate an elasticsearch Client     \t\t\t|\n|   @ElasticsearchAdminClient  \t|     Instantiate an elasticsearch AdminClient \t\t\t|\n|   @ElasticsearchTransportClient   |     Instantiate an elasticsearch TransportClient                  |\n|   @ElasticsearchIndexes  \t|     Used to create multiple index\t \t\t\t|\n|   @ElasticsearchIndex      \t|     Creates an index\t\t\t     \t\t\t|\n|   @ElasticsearchMapping      \t|     Defines a mapping for an index and a document type     \t|\n|   @ElasticsearchMappingField  |     Defines field properties in a mapping     \t\t|\n|   @ElasticsearchSetting  \t|     Defines settings (at node or index level) \t\t|\n|   @ElasticsearchBulkRequest  \t|     Used to import data\t\t\t  \t\t|\n|   @ElasticsearchAnalysis  \t|     Used to define analysis settings of an ElasticsearchIndex\t\t\t  \t\t|\n|   @ElasticsearchAnalyzer  \t|     Used to define an analyzer\t \t  \t\t|\n|   @ElasticsearchFilter  \t|     Used to define an analysis filter \t  \t\t|\n\nh3. @ElasticsearchNode\n\nUsed to instantiate an elasticsearch @Node@ in a unit test class.\n\nSimple node has default name \"elasticsearch-test-node\" and is part of default cluster name \"elasticsearch-test-cluster\". Node is local and can hold data.\n\n\u003cpre\u003epackage com.github.tlrx.elasticsearch.samples.core;\n\n@RunWith(ElasticsearchRunner.class)\npublic class VersionTest {\n\n        @ElasticsearchNode\n\tNode node;\n\t\n        @Test\n\tpublic void test(){\n                // Use of node\n\t}\n}\u003c/pre\u003e\n\nExample with custom cluster and node names, and local set to true and data set to false:\n\nbc. @ElasticsearchNode(name = \"node3\", clusterName = \"fourth-cluster-name\", local = true, data = false)\nNode node3;\n\nExample with custom settings:\n\n\u003cpre\u003e@ElasticsearchNode(name = \"node0\", settings = {\n\t@ElasticsearchSetting(name = \"http.enabled\", value = \"false\"),\n\t@ElasticsearchSetting(name = \"node.zone\", value = \"zone_one\") })\nNode node;\n\u003c/pre\u003e\n\nh3. @ElasticsearchClient\n\nUsed to instantiate an elasticsearch @Client@ from a @Node@. The @nodeName@ parameter of the annotation is used to retrieve the node from which the client will be instantiated. If no nodeName is defined, a default node will be instantiated.\n\nExample with default node:\n\nbc. @ElasticsearchClient\nClient client;\n\nExample with a predefined node:\n\n\u003cpre\u003e@ElasticsearchNode(name = \"node1\")\nNode node1;\n\n@ElasticsearchClient(nodeName = \"node1\")\nClient client1;\u003c/pre\u003e\n\nh3. @ElasticsearchAdminClient\n\nSame as @ElasticsearchClient@ but instantiates an @AdminClient@.\n\n\u003cpre\u003e@ElasticsearchAdminClient\nAdminClient adminClient0;\n\n@ElasticsearchNode(name = \"node1\")\nNode node1;\n\n@ElasticsearchAdminClient(nodeName = \"node1\")\nAdminClient adminClient1;\u003c/pre\u003e\n\nh3. @ElasticsearchTransportClient\n\nUsed to instantiate an elasticsearch @TransportClient@. By default the TransportClient will try to join localhost:9200.\n\nConnect to a remote node with a TransportClient:\n\n\u003cpre\u003e@ElasticsearchTransportClient(local = false, clusterName = \"external\",\n\t\t\t\t\thostnames = {\"host1\", \"host2\"},\n\t\t\t\t\tports= {9300, 9300})\n\tClient client;\u003c/pre\u003e\n\n\nh3. @ElasticsearchIndex, @ElasticsearchIndexes\n\nUsed to creates one or many index before a test method. A node must be instantiated before using this annotation.\n\nThis example will create an index with default name @test@:\n\n\u003cpre\u003e@ElasticsearchAdminClient\nAdminClient adminClient;\n\n@Test\n@ElasticsearchIndex\npublic void test(){...}\u003c/pre\u003e\n\nThis example will create an index \"people\":\n\n\u003cpre\u003e@Test\n@ElasticsearchIndex(indexName = \"people\")\npublic void test(){\n        ...\n}\u003c/pre\u003e\n\nThis example will create 2 indexs with settings:\n\n\u003cpre\u003e@Test\n@ElasticsearchIndexes(indexes = {\n\t\t\t@ElasticsearchIndex(indexName = \"library\", \n\t\t\t\t\tforceCreate = true,\n\t\t\t\t\tsettings = {\n\t\t\t\t\t\t@ElasticsearchSetting(name = \"number_of_shards\", value = \"2\"),\n\t\t\t\t\t\t@ElasticsearchSetting(name = \"number_of_replicas\", value = \"1\") }),\n\t\t\t\t@ElasticsearchIndex(indexName = \"people\") })\npublic void test(){\n        ...\n}\u003c/pre\u003e\n\nThis example will create an index with settings:\n\n\u003cpre\u003e@Test\n@Test\n@ElasticsearchIndex(indexName = \"documents\", settingsFile = \"path/to/settings.json\")\npublic void test() {\n        ...\n}\u003c/pre\u003e\n\n\nh3. Load data from JSON file with @ElasticsearchBulkRequest\n\n\u003cpre\u003e@Test\n@ElasticsearchIndex(indexName = \"documents\", forceCreate = true)\n@ElasticsearchBulkRequest(dataFile = \"com/tlrx/elasticsearch/test/annotations/documents/bulk1.json\")\npublic void test() {\n   // Data from JSON file are indexed\n}\u003c/pre\u003e\n\nh3. @ElasticsearchMapping, @ElasticsearchMappingField and @ElasticsearchSetting\n\nUsed to define the mappings and settings of an index\n\nThis example will create 2 indexs, \"people\" and \"library\", with a mapping for document type \"book\":\n\u003cpre\u003e@ElasticsearchNode\nNode node;\n\n@Test\n@ElasticsearchIndexes(indexes = {\n\t@ElasticsearchIndex(indexName = \"people\", settings = {\n\t\t@ElasticsearchSetting(name = \"number_of_shards\", value = \"2\"),\n\t\t@ElasticsearchSetting(name = \"number_of_replicas\", value = \"1\")\n\t}),\n\t@ElasticsearchIndex(indexName = \"library\",\n\t\tmappings = {\n\t\t\t@ElasticsearchMapping(typeName = \"book\",\n\t\t\t\tproperties = {\n\t\t\t\t\t@ElasticsearchMappingField(name = \"title\", store = Store.Yes, type = Types.String),\n\t\t\t\t\t@ElasticsearchMappingField(name = \"author\", store = Store.Yes, type = Types.String)\n\t\t\t})\n\t\t})\n})\npublic void test(){\n\t...\n}\u003c/pre\u003e\n\nYou can also look at \"the unit tests for some inspiration\":https://github.com/tlrx/elasticsearch-test/tree/master/src/test/java/com/github/tlrx/elasticsearch\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlrx%2Felasticsearch-test","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ftlrx%2Felasticsearch-test","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ftlrx%2Felasticsearch-test/lists"}