elasticsearch部署
1 ?拉取elasticsearch鏡像
? ? ?docker pull elasticsearch:7.7.0
2 ? 創(chuàng)建文件映射路徑
? ? ?mkdir /mydata/elasticsearch/data
? ? ?mkdir /mydata/elasticsearch/plugins
? ? ?mkdir /mydata/elasticsearch/config
3 ?文件夾授權(quán) ? ?
? ? chmod 777 /mydata/elasticsearch/data
4 ?修改配置文件
? ? cd /mydata/elasticsearch/config
? ? vi elasticsearch.yml
? ? 填入如下內(nèi)容:
? ? #集群名稱
? ? cluster.name: "elasticsearch"
? ? network.host: 0.0.0.0
? ? #跨域設(shè)置
? ? http.cors.enabled: true
? ? http.cors.allow-origin: "*"
? ? #http端口
? ? http.port: 9200
? ?#java端口
? ?transport.tcp.port: 9300
5 ?運行鏡像:
? ?docker run --name elasticsearch -p 9200:9200 -p 9300:9300 -d ?-e "discovery.type=single-node" -e ES_JAVA_OPTS="-Xms512m -Xmx512m" ?\
-v /mydata/elasticsearch/data:/usr/share/elasticsearch/data -v /mydata/elasticsearch/plugins:/usr/share/elasticsearch/plugins ?\
-v /data/elasticsearch/config/elasticsearch.yml:/usr/share/elasticsearch/config/elasticsearch.yml elasticsearch:7.7.0
6 ?訪問頁面:
? ?http://192.168.56.102:9200 查看是否部署成功
如出現(xiàn)如上內(nèi)容,表示elasticsearch部署成功,就可以進行搜索操作了
elasticsearch-head部署
? ? 為了更好的使用elasticsearch,需要部署elasticsearch-head插件
1 ?#拉取鏡像
? ? ? docker pull mobz/elasticsearch-head:5
? 2 ?啟動容器
? ? docker run -d --name elasticsearch-head -p 9100:9100 mobz/elasticsearch-head:5
? ?3 訪問elasticsearch-head頁面內(nèi)容
????????
elasticsearch-head的頁面操作
? ? 1? 查詢所有數(shù)據(jù)
????????????????
? ?出現(xiàn)如上所示報錯。解決方案如下:
? 1? docker exec -it {elasticearch-head}容器名稱 /bin/bash
? ?2? 修改elasticsearch-head的js文件
? ? ? ?vi? /usr/src/app/_site/vendor.js
? ? ? a? 第6886行 :/contentType: "application/x-www-form-urlencoded
? ? ? ? ? 改為 :contentType: "application/json;charset=UTF-8"
? ? ? ?b??第7574行 var inspectData = s.contentType === "application/x-www-form-urlencoded" &&
? ? ? ? ? ?改為 var inspectData = s.contentType === "application/json;charset=UTF-8" &&
? ?3? 重啟elasticsearch-head容器
其他頁面操作參見博客:
ElasticSearch-Head操作Elasticsearch進行查詢數(shù)據(jù)(查詢所有數(shù)據(jù),查詢單個索引所有數(shù)據(jù),查詢單個索引指定類型所有數(shù)據(jù),根據(jù)指定條件查詢數(shù)據(jù))_elasticsearch-head 查詢-CSDN博客文章來源:http://www.zghlxwxcb.cn/news/detail-725507.html
使用java操作elasticsearch
?1 引入依賴:
??文章來源地址http://www.zghlxwxcb.cn/news/detail-725507.html
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>7.7.0</version>
<exclusions>
<exclusion>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.elasticsearch</groupId>
<artifactId>elasticsearch</artifactId>
<version>7.7.0</version>
</dependency>
2? 相關(guān)crud操作
public class EsHandler {
private static final String ES_SERVER_ADDRESS = "192.168.56.102";
private static final String IDX_NAME = "employee";
private static RestHighLevelClient CLIENT = null;
public static void main(String[] args) throws IOException {
// 初始化
init();
// 創(chuàng)建索引數(shù)據(jù)
createIndex();
// 修改數(shù)據(jù)
// updateDoc();
// 刪除數(shù)據(jù)
// deleteDoc();
// 查詢數(shù)據(jù)
// searchDoc();
// 關(guān)閉連接
close();
}
private static void init() {
CLIENT = new RestHighLevelClient(RestClient.builder(
new HttpHost(ES_SERVER_ADDRESS,9200,"http")));
}
private static void close() throws IOException {
CLIENT.close();
}
private static void createIndex() throws IOException {
IndexRequest indexRequest = new IndexRequest(IDX_NAME);
Map<String, String> insertInfo = new HashMap<>();
insertInfo.put("name","wangwu");
indexRequest.source(insertInfo);
IndexResponse response = CLIENT.index(indexRequest, RequestOptions.DEFAULT);
System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
}
private static void updateDoc() throws IOException {
UpdateRequest updateRequest = new UpdateRequest(IDX_NAME,"8");
// 注意此處的泛型類型:<String,Object>,如果是其他的泛型類型,es的api會認為是另一套api調(diào)用
Map<String, Object> sourceInfo = new HashMap<>();
sourceInfo.put("name","騾子攤");
// updateRequest.doc("name","隆昌羊肉湯");
updateRequest.doc(sourceInfo);
updateRequest.timeout("1s");
updateRequest.retryOnConflict(3);
UpdateResponse response = CLIENT.update(updateRequest, RequestOptions.DEFAULT);
System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
}
private static void deleteDoc() throws IOException {
DeleteRequest deleteRequest = new DeleteRequest(IDX_NAME,"9");
DeleteResponse response = CLIENT.delete(deleteRequest, RequestOptions.DEFAULT);
System.out.println("ID:" + response.getId() + "/t" + "RESULT:" + response.getResult());
}
/**
*
* @throws IOException
*/
private static void searchDoc() throws IOException {
SearchSourceBuilder builder = new SearchSourceBuilder()
.query(QueryBuilders.matchQuery("message", "execute"));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("rizhi-log-*");
searchRequest.source(builder);
// 執(zhí)行請求
SearchResponse response = CLIENT.search(searchRequest, RequestOptions.DEFAULT);
// 解析查詢結(jié)果
System.out.println(response.toString());
}
到了這里,關(guān)于docker創(chuàng)建elasticsearch、elasticsearch-head部署及簡單操作的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!