ue2平台增加搜索引擎es开发步骤

发布者: xiaozhimn

第一步:下载es

wget https://download.elastic.co/elasticsearch/release/org/elasticsearch/distribution/tar/elasticsearch/2.3.4/elasticsearch-2.3.4.tar.gz

第二步:解压

cd /usr/local/es
tar -zxvf elasticsearch-2.3.4.tar.gz

第三步: 配置elasticsearch

进入es解压目录的config目录中编辑 elasticsearch.yml 文件
http.cors.enabled: true
http.cors.allow-origin: "*"
#http.cors.allow-headers: Authorization
#http.port: 9200
network.host: 0.0.0.0
index:
  analysis:
    analyzer:
      ik_syno:
          type: custom
          tokenizer: ik_max_word
      ik_syno_smart:
          type: custom
          tokenizer: ik_smart

其中index这里配置索引和分词器

第四步:安装elasticsearch-analysis-ik

wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v1.9.4/elasticsearch-analysis-ik-1.9.4.zip
将elasticsearch-analysis-ik-1.9.4.zip解压到$ES_HOME/plugins/ik文件夹中。

第五步:创建索引和映射

curl -XPUT http://xxx:9200/source_code
curl -XPOST http://xxx:9200/source_code/source_code_info/_mapping
{
  "source_code_info": {
    "_all": {
      "enabled": true,  
      "analyzer": "ik_max_word",  
      "search_analyzer": "ik_max_word",  
      "term_vector": "no",  
      "store": "false"
    },
    "properties": {
      "name": {
        "type": "string",
        "store": "no",
        "term_vector": "with_positions_offsets",
        "analyzer": "ik_syno",
        "search_analyzer": "ik_syno",
        "include_in_all": "true",
        "boost": 8
      }
    }
  }
}

第六步:启动es

进入解压好的es bin目录直接运行即可

第七步: 安装logstash进行mysql到es的数据同步

cd /home
wget https://artifacts.elastic.co/downloads/logstash/logstash-5.6.16.tar.gz
tar -xzf logstash-5.6.16.tar.gz 
cd logstash-5.6.16/
./bin/logstash-plugin install logstash-input-jdbc

第八步:下载工具包,设置mysql相关配置 配置文件 mysql.conf

cd /home/logstash-5.6.16/bin/
mkdir config-mysql
cd config-mysql/
wget https://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-8.0.13.tar.gz
tar -zxf mysql-connector-java-8.0.13.tar.gz 
vim mysql.conf

第九步:配置mysql.conf

input{
    stdin{
    }
    jdbc{
#数据库连接地址
jdbc_connection_string => "jdbc:mysql://localhost:3306/ue2?useUnicode=true&characterEncoding=UTF-8"
jdbc_user => "xxx" #用户名
jdbc_password => "xxxx"
#mysql连接的jar包
jdbc_driver_library => "/home/logstash/logstash-5.6.16/bin/mysql/mysql-connector-java-8.0.11.jar"
jdbc_driver_class => "com.mysql.jdbc.Driver"
jdbc_paging_enabled => "true"
jdbc_page_size => "50000"
codec => plain { charset => "UTF-8"}
statement => "select * from 表名 where update_time >= :sql_last_value"
tracking_column_type => "timestamp"
tracking_column => "update_time"
schedule => "* */10 * * *"
#es的type名
type => "sourceCode"
last_run_metadata_path => "./sourceCode_last_id"

}
}

output{
    elasticsearch{
        #es集群地址
    hosts => "xxx.xxx.xxx.xxx:9200"
        #es中数据存放的索引名
    index => "source_code"
        #定义es中主键的生成规则,沿用mysql表中的主键
    document_id => "%{id}"
    document_type => "source_code_info"
    }
        stdout {
        #JSON格式输出
        codec => json_lines
     }
}

第十步:启动logstash

 ./logstash -f mysql/mysql.conf

第十一步: 查询测试

打开浏览器:
http://xx.xx.xx.xx:9200/索引/_search?q=name:php%E6%94%AF%E4%BB%98%E7%B3%BB%E7%BB%9F

或者进行post请求的复合查询:

{
  "size": 1,
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "name": "java"
          }
        },
        {
          "match": {
            "name": "java"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "id": {
        "order": "desc"
      }
    }
  ]
}
0赞