安装和使用elasticsearch
一、环境配置
操作系统:Cent OS
ElasticSearch版本:5.4.0
JDK版本:1.8
Xshell5
elasticsearch-analysis-ik
二、操作过程
解压下载好的ElasticSearch
目录结构
bin:可以执行文件 config:配置文件 data:es存放数据的文件 lib:运行所需类库 logs:日志文件 modules:加载模块列表(其实是必要插件) plugins:插件文件(需要自定义安装) |
进入根目录下的bin目录
./elasticsearch 前台启动
./elasticsearch -d 后台启动
ES启动成功,可以进行简单的操作,增加索引,增加类型和数据,查询、删除等等操作
想通过域名访问、或者想修改节点名称等。可以前往\elasticsearch-5.4.0\config\elasticsearch.yml修改
//配置分词
ES有默认的分词,在此我配置的IK分词
下载elasticsearch-analysis-ik
下载之后进入到下载的文件夹下,如图所示:
因为其源码使用的maven开发,故而使用maven编译项目
Mvn clear package
编译成功在下面会提示Succes
打开编译后的target\releases,解压压缩包,然后进入解压的压缩包里面可以看到几个jar包和配置文件
在Elasticsearch的安装的plugin下创建文件夹ik
跳转到ik文件夹下,将上面所述文件拷贝到ik文件夹
重新启动ElasticSearch
http://localhost:9200/_analyze?analyzer=ik&pretty=true&text=helloworld,%E6%AC%A2%E8%BF%8E%E6%82%A8
访问浏览器,如果分词,则说明配置成功。
三、代码集成
创建连接
private static TransportClient newClient() { Settings settings = Settings.builder() .put("cluster.name", "elasticsearch") .build(); try { client = new PreBuiltTransportClient(settings) .addTransportAddress(new InetSocketTransportAddress( InetAddress.getByName("192.168.1.1"), 9300)); } catch (UnknownHostExceptione) { e.printStackTrace(); } return client; } |
创建索引
public static void createCluterName(Stringindices) { client.admin().indices().prepareCreate(indices).execute().actionGet(); } |
删除索引
public static void deleteIndex(String indexName) { DeleteIndexResponse dResponse = client.admin().indices() .prepareDelete(indexName).execute().actionGet(); } |
添加数据
public static void index(){ //client.prepareIndex(index, type, id) for (int i = 1; i < 1000; i++) { Map<String, Object> map = new HashMap<String, Object>(); //map.put("nickname", "测试" + ran.nextInt(100)); Random ran = new Random(); map.put("nickname", "张三"+ran); map.put("sex", 1); map.put("age", 26); map.put("mobile", "18501944699"); IndexResponse response = client.prepareIndex("huoxf", "user",""+i).setSource(map).execute() .actionGet();//.get(); System.out.println(response); } } |
查询数据
public static void search() throws UnsupportedEncodingException{ System.out.println(new Date().getTime()); SearchResponse response = client.prepareSearch("huoxf").setTypes("user") .setSearchType(SearchType.DEFAULT) .setQuery(QueryBuilders.rangeQuery("id").from(0).to(60)) .setQuery(QueryBuilders.commonTermsQuery("name", "lishici")) //.setQuery(QueryBuilders.matchAllQuery()) //.setQuery(QueryBuilders.queryStringQuery("").field("name").field("kw") //. analyzer("ik_smart")) .setFrom(0).setSize(20).setExplain(true) .execute() .actionGet(); SearchHits hits2 = response.getHits(); System.out.println(hits2.totalHits); System.out.println(new Date().getTime()); SearchHit[] hits = hits2.getHits();
List<Map<String, Object>> results = new ArrayList<Map<String, Object>>(); /*for (SearchHit hit : hits) { results.add(hit.getSource()); System.out.println(hit.getSourceAsString()); System.out.println(hit.getId()); } */ } |
|
创建mapper
public static void createMapping(String indices,String mappingType)throws Exception{ new XContentFactory(); XContentBuilder builder=XContentFactory.jsonBuilder() .startObject() .startObject(mappingType) .startObject("properties") .startObject("sex").field("type", "integer").field("store", "yes").endObject() .startObject("name").field("type", "text").field("store", "yes").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").endObject() .startObject("age").field("type", "integer").field("store", "yes").endObject() .startObject("details").field("type","text").field("store", "yes").field("analyzer","ik_max_word").field("search_analyzer","ik_max_word").endObject() .endObject() .endObject() .endObject(); PutMappingRequest mapping = Requests.putMappingRequest(indices).type(mappingType).source(builder); client.admin().indices().putMapping(mapping).actionGet(); client.close(); } |