ElasticSearch 用ik分词器创建索引(java API)

  ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,做为当前流行的企业级搜索引擎,用于云计算中,可以达到实时搜索,而且具备稳定,可靠,快速安装,使用方便等多种优势,获得大多数企业的青睐。web

  ElasicSearch能够经过终端创建索引,可是本人在作项目的时候用终端创建的索引,而后使用Java API进行数据的插入,始终得不到分词,最终的解决办法就是经过Java API进行创建索引,才避免了尴尬,很少说了。json

  假设基本数据是poi:服务器

  属性以下:app

  poi_index:相似于id分布式

  poi_title:地名  (相似于:武汉大学,华中科技大学等)ui

  poi_lng:经度搜索引擎

  poi_lat:纬度云计算

  poi_phone:电话(通常为undefined)spa

  poi_address:地址   (相似于  :XX省XX市XX区XX路XX号)code

  poi_tags:标签,类别 (相似于:学校,宾馆,公司等)

 

  

  以下,某些对象或者变量已经在全局设定好:

//客户端设置
            settings = Settings
                    .builder()
                    .put("cluster.name", "cxy")               //节点名称, 在es配置的时候设置
                    .put("client.transport.sniff", "true")
                    .build();
            //建立客户端
            
            client = new PreBuiltTransportClient(settings)
                .addTransportAddress(new InetSocketTransportAddress
                        (InetAddress.getByName("127.0.0.1"), 9300));  //以本机做为节点 //建立映射
            mapping = XContentFactory.jsonBuilder()
                .startObject()
                    .startObject("properties")
                //      .startObject("m_id").field("type","keyword").endObject()
                        .startObject("poi_index").field("type","integer").endObject()
                        .startObject("poi_title").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("poi_address").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("poi_tags").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("poi_phone").field("type","text").field("analyzer","ik_max_word").endObject()
                        .startObject("poi_lng").field("type","text").endObject()
                        .startObject("poi_lat").field("type","text").endObject()
                    .endObject()
                .endObject();
            //pois:索引名 cxyword:类型名(能够本身定义)
            PutMappingRequest putmap = Requests.putMappingRequest("pois").type("cxyword").source(mapping);
            //建立索引
            client.admin().indices().prepareCreate("pois").execute().actionGet();
            //为索引添加映射
            client.admin().indices().putMapping(putmap).actionGet();

  这个时候索引就建立好了,mapping不能掉,这至关于一扇门户,对数据按照设定规则处理,ik_max_word 是分词类型,最细粒度切割,网上搜索ik分词器的配置,这里就不说了,下一篇是往索引里面插入数据。

相关文章
相关标签/搜索