ElasticSearch Install

1.下载安装文件

假如我们下载到/opt/elk目录

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

2.解压安装文件

假如我们解压到/opt/elk目录

tar zxvf elasticsearch- 2.3 . 3 .tar.gz

3.修改elasticsearch.yml

打开/opt/elk/elasticsearch-2.3.3/config/elasticsearch.yml,修改配置内容如下:

cluster.name: goeasy-dev
node.name: goeasy-dev-node1
network.host:  192.168 . 1.10
http.port:  9200

4.新建Linux用户

由于elasticsearch不能用root用户启动,因此我们需要新建一个用户来启动它

 a. 使用useradd命令创建一个用户,并设置登录密码

useradd phy
passwd phy
输入新密码

b.给新建的用户分配目录权限

假如/opt/elk/elasticsearch-2.3.3是我们安装的目录,那需要把这个目录的权限分配给用户

chown -R phy:phy /opt/elk/elasticsearch- 2.3 . 3

5.启动ElasticSearch

 a. 使用su命令从root用户切换到phy用户

su phy
输入密码

b. 打开/opt/elk/elasticsearch-2.3.3目录,然后运行:

bin/elasticsearch
 
#如果想在后台运行,可以增加-d
bin/elasticsearch -d

6.测试

在浏览器中输入http://192.168.1.10:9200/  ,如果能看到下面返回信息,说明elasticsearch安装成功了

{
   "name"  "goeasy-dev-node1" ,
   "cluster_name"  "goeasy-dev" ,
   "version"  : {
     "number"  "2.3.3" ,
     "build_hash"  "218bdf10790eef486ff2c41a3df5cfa32dadcfde" ,
     "build_timestamp"  "2016-05-17T15:40:04Z" ,
     "build_snapshot"  false ,
     "lucene_version"  "5.5.0"
   },
   "tagline"  "You Know, for Search"
}

 

参考文档:

https://www.elastic.co/guide/en/elasticsearch/reference/current/setup.html

 

附:Java操作ElastchSearch代码:

package  com.yhiker.search;
import  java.net.InetAddress;
import  java.net.UnknownHostException;
import  java.util.HashMap;
import  java.util.Map;
import  org.elasticsearch.action.index.IndexResponse;
import  org.elasticsearch.action.search.SearchResponse;
import  org.elasticsearch.client.Client;
import  org.elasticsearch.client.transport.TransportClient;
import  org.elasticsearch.common.settings.Settings;
import  org.elasticsearch.common.transport.InetSocketTransportAddress;
import  org.elasticsearch.index.query.MatchQueryBuilder.Operator;
import  org.elasticsearch.index.query.QueryBuilders;
import  org.elasticsearch.search.SearchHit;
import  com.lm.core.util.JsonUtils;
import  com.lm.core.util.ValueUtils;
public  class  Test {
     private  static  Client client =  null ;
     static  {
         init();
     }
     private  static  void  init() {
         if  (client !=  null ) {
             return ;
         }
         try  {
             
             Map<String,String> settingsMap =  new  HashMap<String,String>();
             settingsMap.put( "cluster.name" "goeasy-dev" );
             settingsMap.put( "client.transport.sniff" "true" );
             
             
             Settings settings = Settings.settingsBuilder().put(settingsMap).build();
             client = TransportClient.builder().settings(settings).build()
                     .addTransportAddress( new  InetSocketTransportAddress(InetAddress.getByName( "192.168.1.10" ),  9300 ));
             
         catch  (UnknownHostException e) {
             e.printStackTrace();
         }
     }
     
     public  static  void  createIndex(Goods goods) {
         System.out.println( "==============createIndex=============" );
         
         String json = JsonUtils.toJson(goods);
         IndexResponse response = client.prepareIndex( "goeasy" "goods" , ValueUtils.toString(goods.getId())).setSource(json).get();
         // Index name
         String _index = response.getIndex();
         System.out.println( "_index="  + _index);
         // Type name
         String _type = response.getType();
         System.out.println( "_type="  + _type);
         // Document ID (generated or not)
         String _id = response.getId();
         System.out.println( "_id="  + _id);
         // Version (if it's the first time you index this document, you will
         // get: 1)
         long  _version = response.getVersion();
         System.out.println( "_version="  + _version);
         // isCreated() is true if the document is a new one, false if it has
         // been updated
         boolean  created = response.isCreated();
         System.out.println( "created="  + created);
     }
     
     public  static  void  createGoodsIndex( int  id,String title,String description,String category,String brand){
         Goods goods =  new  Goods();
         goods.setId(id);
         goods.setTitle(title);
         goods.setDescription(description);
         goods.setCategory(category);
         goods.setBrand(brand);
         createIndex(goods);
     }
     
     public  static  void  crateGoodsIndex(){
         createGoodsIndex( 1 , "台南府城盐咖啡" , "【台湾第一盐咖啡】台南府城盐咖啡 时尚独特 香浓细腻" , "美食佳品" , "雀巢" );
         createGoodsIndex( 2 , "我的心机 玻尿酸保湿锁水黑面膜 8片" , "【强效锁水】我的心机 玻尿酸保湿锁水黑面膜 8片 深度补水,海客" , "美妆个护" , "我的心机" );
         createGoodsIndex( 3 , "我的心机 仙人掌精粹补水黑面膜 8片" , "【补水神器】我的心机 仙人掌精粹补水黑面膜 8片 补水急救" , "美妆个护" , "我的心机" );
         createGoodsIndex( 4 , "糖村法式牛轧糖 500g" , "【高圆圆的喜糖】糖村法式牛轧糖 500g 新鲜美味" , "美食佳品" , "糖村" );
         createGoodsIndex( 5 , "糖村法式牛轧糖 250g" , "【最好吃的牛扎糖】糖村法式牛轧糖 250g 新鲜美味" , "美食佳品" , "糖村" );   
     }
     
     public  static  void  search(){
         SearchResponse response = client.prepareSearch( "goeasy" )
                 .setTypes( "goods" )
                 .setQuery(QueryBuilders.multiMatchQuery( "美食" "title" , "description" , "category" , "brand" ).operator(Operator.AND))
                 .execute()
                 .actionGet();
     
         System.out.println(response.toString());
         
         SearchHit[] hits = response.getHits().getHits();
         for (SearchHit hit:hits){
              System.out.println(hit.sourceAsString());
         }
     }
     
     public  static  void  main(String[] args) {
         //crateGoodsIndex();
         //search();
     }
}

 

猜你喜欢

转载自penghuaiyi.iteye.com/blog/2310405
今日推荐