|NO.Z.00036|——————————|BigDataEnd|——|Hadoop&ElasticSearch.V36|——|ELK.v36|集群|Java API操作ES.V4|
一、Java API操作ES:文档操作
### --- 编程实现:文档操作;添加doc
~~~ # 步骤五:文档操作;添加doc
@Test
public void addDoc() throws IOException {
//准备一个添加文档的对象
final IndexRequest indexRequest = new IndexRequest("es_test").id("1");
/*
{
"name": "spark添加文档",
"description": "spark技术栈",
"studymodel":"online",
"pic": "http://www.baidu.com"
}
*/
//准备文档内容
String str = " {\n" +
" \"name\": \"spark添加文档\",\n" +
" \"description\": \"spark技术栈\",\n" +
" \"studymodel\":\"online\",\n" +
" \"pic\": \"http://www.baidu.com\"\n" +
" }";
indexRequest.source(str, XContentType.JSON);
//执行添加动作
final IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.status());
}
### --- 编译打印
D:\JAVA\jdk1.8.0_231\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54401:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.80000.Hadoop.project\elasticsearch\target\classes;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-high-level-client\7.9.0\elasticsearch-rest-high-level-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-client\7.9.0\elasticsearch-rest-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpclient\4.5.10\httpclient-4.5.10.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore-nio\4.4.12\httpcore-nio-4.4.12.jar;C:\Users\Administrator\.m2\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\mapper-extras-client\7.9.0\mapper-extras-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\parent-join-client\7.9.0\parent-join-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\aggs-matrix-stats-client\7.9.0\aggs-matrix-stats-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\rank-eval-client\7.9.0\rank-eval-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\lang-mustache-client\7.9.0\lang-mustache-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\com\github\spullara\mustache\java\compiler\0.9.6\compiler-0.9.6.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch\7.9.0\elasticsearch-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-core\7.9.0\elasticsearch-core-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-secure-sm\7.9.0\elasticsearch-secure-sm-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-x-content\7.9.0\elasticsearch-x-content-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.10.4\jackson-core-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-smile\2.10.4\jackson-dataformat-smile-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.10.4\jackson-dataformat-yaml-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-cbor\2.10.4\jackson-dataformat-cbor-2.10.4.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-geo\7.9.0\elasticsearch-geo-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-core\8.6.0\lucene-core-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-analyzers-common\8.6.0\lucene-analyzers-common-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-backward-codecs\8.6.0\lucene-backward-codecs-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-grouping\8.6.0\lucene-grouping-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-highlighter\8.6.0\lucene-highlighter-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-join\8.6.0\lucene-join-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-memory\8.6.0\lucene-memory-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-misc\8.6.0\lucene-misc-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queries\8.6.0\lucene-queries-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queryparser\8.6.0\lucene-queryparser-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-sandbox\8.6.0\lucene-sandbox-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial-extras\8.6.0\lucene-spatial-extras-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial3d\8.6.0\lucene-spatial3d-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-suggest\8.6.0\lucene-suggest-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-cli\7.9.0\elasticsearch-cli-7.9.0.jar;C:\Users\Administrator\.m2\repository\net\sf\jopt-simple\jopt-simple\5.0.2\jopt-simple-5.0.2.jar;C:\Users\Administrator\.m2\repository\com\carrotsearch\hppc\0.8.1\hppc-0.8.1.jar;C:\Users\Administrator\.m2\repository\joda-time\joda-time\2.10.4\joda-time-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\tdunning\t-digest\3.2\t-digest-3.2.jar;C:\Users\Administrator\.m2\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\jna\5.5.0\jna-5.5.0.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\Administrator\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\Administrator\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-core\2.5\log4j-core-2.5.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.5\log4j-api-2.5.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.yanqi.es.ESDemo,addDoc
~~~输出参数
CREATED
Process finished with exit code 0
二、添加doc查询文档
### --- 添加doc查询文档
~~~ # 步骤六:添加doc查询文档
@Test
public void getDoc() throws IOException {
//准备一个查询的reqeust请求
final GetRequest getRequest = new GetRequest("es_test");
//设定id he
getRequest.id("1");
//执行查询
final GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
final Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
}
### --- 编译打印
D:\JAVA\jdk1.8.0_231\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=54444:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.80000.Hadoop.project\elasticsearch\target\classes;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-high-level-client\7.9.0\elasticsearch-rest-high-level-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-client\7.9.0\elasticsearch-rest-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpclient\4.5.10\httpclient-4.5.10.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore-nio\4.4.12\httpcore-nio-4.4.12.jar;C:\Users\Administrator\.m2\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\mapper-extras-client\7.9.0\mapper-extras-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\parent-join-client\7.9.0\parent-join-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\aggs-matrix-stats-client\7.9.0\aggs-matrix-stats-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\rank-eval-client\7.9.0\rank-eval-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\lang-mustache-client\7.9.0\lang-mustache-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\com\github\spullara\mustache\java\compiler\0.9.6\compiler-0.9.6.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch\7.9.0\elasticsearch-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-core\7.9.0\elasticsearch-core-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-secure-sm\7.9.0\elasticsearch-secure-sm-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-x-content\7.9.0\elasticsearch-x-content-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.10.4\jackson-core-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-smile\2.10.4\jackson-dataformat-smile-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.10.4\jackson-dataformat-yaml-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-cbor\2.10.4\jackson-dataformat-cbor-2.10.4.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-geo\7.9.0\elasticsearch-geo-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-core\8.6.0\lucene-core-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-analyzers-common\8.6.0\lucene-analyzers-common-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-backward-codecs\8.6.0\lucene-backward-codecs-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-grouping\8.6.0\lucene-grouping-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-highlighter\8.6.0\lucene-highlighter-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-join\8.6.0\lucene-join-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-memory\8.6.0\lucene-memory-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-misc\8.6.0\lucene-misc-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queries\8.6.0\lucene-queries-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queryparser\8.6.0\lucene-queryparser-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-sandbox\8.6.0\lucene-sandbox-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial-extras\8.6.0\lucene-spatial-extras-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial3d\8.6.0\lucene-spatial3d-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-suggest\8.6.0\lucene-suggest-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-cli\7.9.0\elasticsearch-cli-7.9.0.jar;C:\Users\Administrator\.m2\repository\net\sf\jopt-simple\jopt-simple\5.0.2\jopt-simple-5.0.2.jar;C:\Users\Administrator\.m2\repository\com\carrotsearch\hppc\0.8.1\hppc-0.8.1.jar;C:\Users\Administrator\.m2\repository\joda-time\joda-time\2.10.4\joda-time-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\tdunning\t-digest\3.2\t-digest-3.2.jar;C:\Users\Administrator\.m2\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\jna\5.5.0\jna-5.5.0.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\Administrator\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\Administrator\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-core\2.5\log4j-core-2.5.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.5\log4j-api-2.5.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.yanqi.es.ESDemo,getDoc
~~~输出参数
{studymodel=online, name=spark添加文档, description=spark技术栈, pic=http://www.baidu.com}
Process finished with exit code 0
三、查询所有文档
### --- 查询所有文档
~~~ # 步骤七:查询所有文档
@Test
public void getAllDoc() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("es_test");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query( QueryBuilders.matchAllQuery());
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
final RequestOptions aDefault = RequestOptions.DEFAULT;
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//解析响应结果
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" + entry.getValue());
}
}
}
### --- 编译打印
D:\JAVA\jdk1.8.0_231\bin\java.exe -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar=58747:D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\bin" -Dfile.encoding=UTF-8 -classpath "D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\lib\idea_rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit5-rt.jar;D:\IntelliJIDEA\IntelliJ IDEA 2019.3.3\plugins\junit\lib\junit-rt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\charsets.jar;D:\JAVA\jdk1.8.0_231\jre\lib\deploy.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\access-bridge-64.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\cldrdata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\dnsns.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jaccess.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\jfxrt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\localedata.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\nashorn.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunec.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunjce_provider.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunmscapi.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\sunpkcs11.jar;D:\JAVA\jdk1.8.0_231\jre\lib\ext\zipfs.jar;D:\JAVA\jdk1.8.0_231\jre\lib\javaws.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jce.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfr.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jfxswt.jar;D:\JAVA\jdk1.8.0_231\jre\lib\jsse.jar;D:\JAVA\jdk1.8.0_231\jre\lib\management-agent.jar;D:\JAVA\jdk1.8.0_231\jre\lib\plugin.jar;D:\JAVA\jdk1.8.0_231\jre\lib\resources.jar;D:\JAVA\jdk1.8.0_231\jre\lib\rt.jar;E:\NO.Z.80000.Hadoop.project\elasticsearch\target\classes;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-high-level-client\7.9.0\elasticsearch-rest-high-level-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\client\elasticsearch-rest-client\7.9.0\elasticsearch-rest-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpclient\4.5.10\httpclient-4.5.10.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore\4.4.12\httpcore-4.4.12.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpasyncclient\4.1.4\httpasyncclient-4.1.4.jar;C:\Users\Administrator\.m2\repository\org\apache\httpcomponents\httpcore-nio\4.4.12\httpcore-nio-4.4.12.jar;C:\Users\Administrator\.m2\repository\commons-codec\commons-codec\1.11\commons-codec-1.11.jar;C:\Users\Administrator\.m2\repository\commons-logging\commons-logging\1.1.3\commons-logging-1.1.3.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\mapper-extras-client\7.9.0\mapper-extras-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\parent-join-client\7.9.0\parent-join-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\aggs-matrix-stats-client\7.9.0\aggs-matrix-stats-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\rank-eval-client\7.9.0\rank-eval-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\plugin\lang-mustache-client\7.9.0\lang-mustache-client-7.9.0.jar;C:\Users\Administrator\.m2\repository\com\github\spullara\mustache\java\compiler\0.9.6\compiler-0.9.6.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch\7.9.0\elasticsearch-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-core\7.9.0\elasticsearch-core-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-secure-sm\7.9.0\elasticsearch-secure-sm-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-x-content\7.9.0\elasticsearch-x-content-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\yaml\snakeyaml\1.26\snakeyaml-1.26.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\core\jackson-core\2.10.4\jackson-core-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-smile\2.10.4\jackson-dataformat-smile-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-yaml\2.10.4\jackson-dataformat-yaml-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\fasterxml\jackson\dataformat\jackson-dataformat-cbor\2.10.4\jackson-dataformat-cbor-2.10.4.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-geo\7.9.0\elasticsearch-geo-7.9.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-core\8.6.0\lucene-core-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-analyzers-common\8.6.0\lucene-analyzers-common-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-backward-codecs\8.6.0\lucene-backward-codecs-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-grouping\8.6.0\lucene-grouping-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-highlighter\8.6.0\lucene-highlighter-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-join\8.6.0\lucene-join-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-memory\8.6.0\lucene-memory-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-misc\8.6.0\lucene-misc-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queries\8.6.0\lucene-queries-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-queryparser\8.6.0\lucene-queryparser-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-sandbox\8.6.0\lucene-sandbox-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial-extras\8.6.0\lucene-spatial-extras-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-spatial3d\8.6.0\lucene-spatial3d-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\apache\lucene\lucene-suggest\8.6.0\lucene-suggest-8.6.0.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\elasticsearch-cli\7.9.0\elasticsearch-cli-7.9.0.jar;C:\Users\Administrator\.m2\repository\net\sf\jopt-simple\jopt-simple\5.0.2\jopt-simple-5.0.2.jar;C:\Users\Administrator\.m2\repository\com\carrotsearch\hppc\0.8.1\hppc-0.8.1.jar;C:\Users\Administrator\.m2\repository\joda-time\joda-time\2.10.4\joda-time-2.10.4.jar;C:\Users\Administrator\.m2\repository\com\tdunning\t-digest\3.2\t-digest-3.2.jar;C:\Users\Administrator\.m2\repository\org\hdrhistogram\HdrHistogram\2.1.9\HdrHistogram-2.1.9.jar;C:\Users\Administrator\.m2\repository\org\elasticsearch\jna\5.5.0\jna-5.5.0.jar;C:\Users\Administrator\.m2\repository\junit\junit\4.12\junit-4.12.jar;C:\Users\Administrator\.m2\repository\org\hamcrest\hamcrest-core\1.3\hamcrest-core-1.3.jar;C:\Users\Administrator\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\Administrator\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\Administrator\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-core\2.5\log4j-core-2.5.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.5\log4j-api-2.5.jar" com.intellij.rt.junit.JUnitStarter -ideVersion5 -junit4 com.yanqi.es.ESDemo,getAllDoc
~~~输出参数:
查询结果状态:OK
----------------------------
查询的字段:price; 字段value:66.6
查询的字段:studymodel; 字段value:offline
查询的字段:name; 字段value:es添加文档
查询的字段:description; 字段value:es技术栈
查询的字段:pic; 字段value:wwww.google.com
----------------------------
查询的字段:price; 字段value:56.6
查询的字段:studymodel; 字段value:online
查询的字段:name; 字段value:hadoop添加文档
查询的字段:description; 字段value:hadoop技术栈
查询的字段:pic; 字段value:http://www.baidu.com
Process finished with exit code 0
四、准备数据
### --- 准备数据一:
~~~ # 添加数据一:
POST /es_test/_doc/1
{
"name": "hadoop添加文档",
"description": "hadoop技术栈",
"studymodel":"online",
"pic": "http://www.baidu.com", "price":56.6
}

### --- 准备数据二:
~~~ # 添加数据二:
POST /es_test/_doc/2
{
"name": "es添加文档",
"description": "es技术栈",
"studymodel":"offline",
"pic": "wwww.google.com",
"price":66.6
}

五、编程实现:定版编程:创建索引
package com.yanqi.es;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.rest.RestStatus;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.Map;
public class ESDemo {
// 步骤一:准备一个highlevel client
RestHighLevelClient client;
@Before
public void init() {
final RestHighLevelClient highLevelClient = new RestHighLevelClient( RestClient.builder(
new HttpHost("hadoop01", 9200, "http"),
new HttpHost("hadoop02", 9200, "http"),
new HttpHost("hadoop03", 9200, "http")
));
// System.out.println(highLevelClient.cluster().toString());
client = highLevelClient;
}
// 最后步骤:关闭客户端
@After
public void destory() {
if (client != null) {
try {
client.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 步骤二:方法一:创建索引
@Test
public void createIndex() { final CreateIndexRequest indexRequest = new CreateIndexRequest("es_test");
//准备mapping映射信息
String str = "{\n" +
" \"settings\": {},\n" +
" \"mappings\": {\n" +
" \"properties\": {\n" +
" \"description\": {\n" +
" \"type\": \"text\",\n" +
" \"analyzer\": \"ik_max_word\"\n" +
" },\n" +
" \"name\": {\n" +
" \"type\": \"text\"\n" +
" },\n" +
" \"pic\": {\n" +
" \"type\": \"text\",\n" +
" \"index\": false\n" +
" },\n" +
" \"studymodel\": {\n" +
" \"type\": \"text\"\n" +
" }\n" +
" }\n" +
" }\n" +
" }";
//把映射信息添加到request对象中
indexRequest.source(str, XContentType.JSON);
//创建索引
try { final CreateIndexResponse createIndexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT);
final boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建结果:" + acknowledged);
} catch (IOException e) { e.printStackTrace();
}
}
// 步骤三:方法二:创建索引
@Test
public void createIndex2() throws IOException { final CreateIndexRequest indexRequest = new CreateIndexRequest("es_test");
//准备mapping映射信息
// String str = "{\n" +
// "
// " \"mappings\": {\n" +
// " \"properties\": {\n" +
// " \"description\": {\n" +
// " \"type\": \"text\",\n" +
// " \"analyzer\": \"ik_max_word\"\n" +
// " },\n" +
// " \"name\": {\n" +
// " \"type\": \"text\"\n" +
// " },\n" +
// " \"pic\": {\n" +
// " \"type\": \"text\",\n" +
// " \"index\": false\n" +
// " },\n" +
// " \"studymodel\": {\n" +
// " \"type\": \"text\"\n" +
// " }\n" +
// " }\n" +
// " }\n" +
// " }";
// 把映射信息添加到request对象中
// 指定setting信息
indexRequest.settings( Settings.builder()
.put("index.number_of_shards", 5)
.put("index.number_of_replicas", 1)
);
//指定mapping信息
final XContentBuilder xContentBuilder = XContentFactory.jsonBuilder();
xContentBuilder.startObject();
xContentBuilder.startObject("properties");
xContentBuilder.startObject("description")
.field("type", "text")
.field("analyzer", "ik_max_word")
.endObject(); xContentBuilder.startObject("name")
.field("type", "text")
.endObject(); xContentBuilder.startObject("pic")
.field("type", "text")
.field("index", "false")
.endObject(); xContentBuilder.startObject("studymodel")
.field("type", "text")
.endObject(); xContentBuilder.endObject(); xContentBuilder.endObject(); indexRequest.mapping(xContentBuilder);
//创建索引
try {
final CreateIndexResponse createIndexResponse = client.indices().create(indexRequest, RequestOptions.DEFAULT);
final boolean acknowledged = createIndexResponse.isAcknowledged();
System.out.println("创建结果:" + acknowledged);
} catch (IOException e) { e.printStackTrace();
}
}
// 步骤四:删除索引
@Test
public void deleteIndex() throws IOException {
//创建一个删除索引的请求
final DeleteIndexRequest indexRequest = new DeleteIndexRequest("es_test");
final AcknowledgedResponse delete = client.indices().delete(indexRequest, RequestOptions.DEFAULT);
System.out.println(delete.isAcknowledged());
}
// 步骤五:文档操作;添加doc
@Test
public void addDoc() throws IOException {
//准备一个添加文档的对象
final IndexRequest indexRequest = new IndexRequest("es_test").id("1");
/*
{
"name": "spark添加文档",
"description": "spark技术栈",
"studymodel":"online",
"pic": "http://www.baidu.com"
}
*/
//准备文档内容
String str = " {\n" +
" \"name\": \"spark添加文档\",\n" +
" \"description\": \"spark技术栈\",\n" +
" \"studymodel\":\"online\",\n" +
" \"pic\": \"http://www.baidu.com\"\n" +
" }";
indexRequest.source(str, XContentType.JSON);
//执行添加动作
final IndexResponse index = client.index(indexRequest, RequestOptions.DEFAULT);
System.out.println(index.status());
}
// 步骤六:添加doc查询文档
@Test
public void getDoc() throws IOException {
//准备一个查询的reqeust请求
final GetRequest getRequest = new GetRequest("es_test");
//设定id he
getRequest.id("1");
//执行查询
final GetResponse getResponse = client.get(getRequest, RequestOptions.DEFAULT);
final Map<String, Object> sourceAsMap = getResponse.getSourceAsMap();
System.out.println(sourceAsMap);
}
// 步骤七:查询所有文档
@Test
public void getAllDoc() throws IOException {
// 构建一个searchRequest对象
final SearchRequest searchRequest = new SearchRequest();
//指定索引
searchRequest.indices("es_test");
//指定查询条件 matchall
final SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
sourceBuilder.query( QueryBuilders.matchAllQuery());
searchRequest.source(sourceBuilder);
//执行检索 RequestOptions options参数: 请求选项(例如请求头),如果不需要自定义任何内容,使用
final RequestOptions aDefault = RequestOptions.DEFAULT;
final SearchResponse response = client.search(searchRequest, RequestOptions.DEFAULT);
//解析响应结果
final RestStatus status = response.status();
System.out.println("查询结果状态:" + status);
final SearchHits hits = response.getHits();
final SearchHit[] hits1 = hits.getHits();
for (SearchHit searchHit : hits1) {
System.out.println("----------------------------");
final Map<String, Object> map = searchHit.getSourceAsMap();
for (Map.Entry<String, Object> entry : map.entrySet()) {
System.out.println("查询的字段:" + entry.getKey() + "; 字段value:" + entry.getValue());
}
}
}
}
Walter Savage Landor:strove with none,for none was worth my strife.Nature I loved and, next to Nature, Art:I warm'd both hands before the fire of life.It sinks, and I am ready to depart
——W.S.Landor
分类:
bdv025-elk
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通