elasticsearch的使用

ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索引擎。设计用于云计算中,能够达到实时搜索,稳定,可靠,快速,安装使用方便。

最近的一个项目中使用了elasticsearch,顺便学习了一下,总结了一个小的demo.

pom.xml

        
            org.elasticsearch
            elasticsearch
            1.7.1
        

demo
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;

import net.sf.json.JSONObject;

/**
 * 
 *
 *
类名:ElasticsearchDemo.java
 *
描述: Elasticsearch使用Demo 
   *
创建时间: 2017年4月20日 下午2:53:08
 *
创建人: leegtang 
 *
 */ public class ElasticsearchDemo { public static void main(String[] args) { ElasticsearchDemo demo = new ElasticsearchDemo(); String res=demo.search("3"); System.out.println(res);   } public void createIndex(){ Settings settings = ImmutableSettings.settingsBuilder() //         .put("cluster.name", "elasticsearch") //         .put("client.transport.sniff", true)         .build(); @SuppressWarnings("resource") Client client =    new TransportClient(settings) .addTransportAddress(new InetSocketTransportAddress("10.10.18.221", 9300));      String jsonValue = JSONObject.fromObject("{"username":"John"}").toString();   IndexResponse response = client.prepareIndex("student_index", "student_info", "3") .setSource(jsonValue).execute().actionGet();   System.out.println(response);   client.close(); // // Index name // String _index = response.getIndex(); // // Type name // String _type = response.getType(); // // Document ID (generated or not) // String _id = response.getId(); // // Version (if it's the first time you index this document, you will get: 1) // long _version = response.getVersion(); // // isCreated() is true if the document is a new one, false if it has been updated // boolean created = response.isCreated(); } // see api https://www.elastic.co/guide/en/elasticsearch/client/java-api/1.7/get.html public String search(String id){ Settings settings = ImmutableSettings.settingsBuilder() //         .put("cluster.name", "elasticsearch") //         .put("client.transport.sniff", true)         .build(); @SuppressWarnings("resource") Client client =    new TransportClient(settings) .addTransportAddress(new InetSocketTransportAddress("10.10.18.221", 9300)); GetResponse gResponse = client.prepareGet("student_index", "student_info", "3")         .execute()         .actionGet(); client.close(); return gResponse.getSourceAsString(); } }

elasticsearch 下载和安装

https://www.elastic.co/downloads/past-releases

选择 elasticsearch 1.7.5 版本下载。(本程序示例使用的版本)

bat


CHCP 65001

e:

cd E:/elasticsearch-1.7.1

elasticsearch

修改配置文件 elasticsearch.yml 在最下面增加一行

network.host: 10.10.18.221

(IP改为自己机器ip)

启动后,访问 http://10.10.18.221:9200 (IP改为自己机器ip)

插件安装

在elasticsearch的安装目录bin下执行

plugin -install mobz/elasticsearch-head

安装后可以访问 http://10.10.18.221:9200/_plugin/head/

0%