1. docker-compose.yml文件


version: '3'
services:
  elasticsearch:
    container_name: elasticsearch
    user: "1000:1000"  #防止没有操作文件权限问题
    image: docker.elastic.co/elasticsearch/elasticsearch:8.7.0
    environment:
      - network.host=0.0.0.0
      - discovery.seed_hosts=elasticsearch
      - http.port=9200 
      - ES_JAVA_OPTS=-Xms1g -Xmx1g
     # - xpack.security.http.ssl.enabled=false
     # - xpack.security.transport.ssl.enabled=true   # 启用 SSL
     # - xpack.security.enabled=true               
     # - ES_JAVA_OPTS=-Xmx1g -Xms1g
      - discovery.type=single-node
    ports:
      - 9200:9200  # Elasticsearch RESTful API 端口
      - 9300:9300  # Elasticsearch 集群通信端口
    volumes:
      - ./data:/usr/share/elasticsearch/data
      - ./config:/usr/share/elasticsearch/config
      - ./plugins:/usr/share/elasticsearch/plugins
    restart: unless-stopped
    networks:
      - elastic

  kibana:
    container_name: kibana
    image: docker.elastic.co/kibana/kibana:8.7.0
    restart: unless-stopped
    ports:
      - "5601:5601"  # Kibana 的 Web 界面端口
    depends_on:
      - elasticsearch
    volumes:
      - ./kibana_data:/usr/share/kibana/data
      - ./kibana_config:/usr/share/kibana/config
      - ./kibana_plugins:/usr/share/kibana/plugins
    networks:
      - elastic

networks:
  elastic:
    driver: bridge

2.挂载目录配置文件修改

elasticsearch配置文件修改 config/elasticsearch.yml

cluster.name: "docker-cluster"
network.host: 0.0.0.0 
http.port: 9200

#----------------------- BEGIN SECURITY AUTO CONFIGURATION -----------------------
#
# The following settings, TLS certificates, and keys have been automatically      
# generated to configure Elasticsearch security features on 14-11-2024 04:41:50
#
# --------------------------------------------------------------------------------

# Enable security features
xpack.security.enabled: true

xpack.security.enrollment.enabled: true

# Enable encryption for HTTP API client connections, such as Kibana, Logstash, and Agents
xpack.security.http.ssl:
  enabled: false
  keystore.path: certs/http.p12

# Enable encryption and mutual authentication between cluster nodes
xpack.security.transport.ssl:
  enabled: false
  verification_mode: certificate
  keystore.path: certs/transport.p12
  truststore.path: certs/transport.p12
#----------------------- END SECURITY AUTO CONFIGURATION -------------------------

kibana配置文件修改 config/kibana.yml

#
# ** THIS IS AN AUTO-GENERATED FILE **
#

# Default Kibana configuration for docker target
server.host: "0.0.0.0"
server.shutdownTimeout: "5s"
elasticsearch.hosts: [ "http://elasticsearch:9200" ]
monitoring.ui.container.elasticsearch.enabled: true
elasticsearch.username: "xiao"         # 设置 Elasticsearch 用户名
elasticsearch.password: "xiao"   # 设置 Elasticsearch 密码

3. 配置kibana enroll-token与添加用户


docker exec -it elasticsearch /bin/bash

./bin/elasticsearch-service-tokens create elastic/kibana xiao

#enroll token
./bin/elasticsearch-create-enrollment-token --scope kibana





elasticsearch-users useradd xiao

#给账户授权
elasticsearch-users roles -a superuser xiao
elasticsearch-users roles -a kibana_system xiao

4. 容器内安装分词器

#手动安装

/usr/share/elasticsearch/bin/elasticsearch-plugin install file:///usr/share/elasticsearch/config/analysis-ik/elasticsearch-analysis-ik-8.7.0.zip



#下载安装
bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v8.7.0/elasticsearch-analysis-ik-8.7.0.zip

http://42.192.251.34:5601/app/dev_tools#/console

#elasticsearch 基本crud
https://www.cnblogs.com/tanghaorong/p/16295478.html

https://www.cnblogs.com/xfeiyun/p/15890024.html

注意事项

  1. 性能:模糊查询(wildcard)在 Elasticsearch 中可能会对性能产生影响,特别是当查询的字段包含大量数据时。确保对频繁查询的字段进行适当的优化,例如使用 text 类型字段并启用分词。

  2. 匹配字段类型:确保查询的字段是 text 类型而不是 keyword 类型,因为 keyword 是精确匹配,text 字段会被分词,因此适合做模糊匹配。

  3. 分页:查询时最好使用分页(PageRequest),以避免返回大量数据导致性能问题。

#创建索引
PUT /index_event
{
  "mappings": {
    "properties": {
      "id": {
        "type": "text"
      },
      "sgyPath": {
        "type": "text",
        "fields": {
          "keyword": {
            "type": "keyword"
          }
        }
      },
      "zone": {
        "type": "text"
      },
      "type": {
        "type": "integer"
      },
      "createTime": {
        "type": "date"
      }
      
    }
  }
}





#查询所有文档
GET /index_event/_search



public interface EventDao extends ElasticsearchDao<Event, String> {


    List<Event> findBySgyPathContaining(String sgyPath);

    // 使用Elasticsearch的Wildcard查询进行模糊查询  text会进行分词,keyword精确匹跟sql like查询一致
    @Query("{\"bool\": {\"must\": [{\"wildcard\": {\"sgyPath\": \"*?0*\"}}]}}")
    List<Event> searchBySgyPath(String path);

}

文章作者:
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自
喜欢就支持一下吧