设为首页 收藏本站
云服务器等爆品抢先购,低至4.2元/月
查看: 1049|回复: 0

[经验分享] 分布式搜索Elasticsearch——集成paoding-maping

[复制链接]
累计签到:29 天
连续签到:1 天
发表于 2017-5-20 13:00:18 | 显示全部楼层 |阅读模式
先做个记录,研究出来了,不记下来下次就不知道怎么处理了。
为es安装paoding插件

首先你得安装paoding插件,进入%ES_HOME%/bin,执行下列代码:
 
[java] view plaincopy



  • plugin -install medcl/elasticsearch-analysis-paoding/1.0.0  

  接下来,在https://github.com/medcl/elasticsearch-analysis-paoding下载paoding解析插件,解压后,把elasticsearch-analysis-paoding-master\config\下的paoding文件夹放置到%ES_HOME%/config下。
 
 
在java代码中使用es和paoding插件
1. 在项目中导入es及es-paoding的包;
2. 将paoding文件夹复制到根目录下的config下;
3. 在src目录下建立一个elasticsearch.yml文件,使其内容如下所示:
 
[java] view plaincopy



  • index:    
  •   analysis:                       
  •     analyzer:          
  •       paoding:    
  •           alias: [paoding_analyzer]    
  •           type: org.elasticsearch.index.analysis.PaodingAnalyzerProvider   

  4. java代码,当你只需要为一个字段建立索引,并通过这个字段查询时代码如下所示:
 
 
[java] view plaincopy



  • import java.util.Iterator;  
  • import java.util.Map;  
  •   
  • import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;  
  • import org.elasticsearch.action.index.IndexResponse;  
  • import org.elasticsearch.action.search.SearchResponse;  
  • import org.elasticsearch.action.search.SearchType;  
  • import org.elasticsearch.client.Client;  
  • import org.elasticsearch.client.Requests;  
  • import org.elasticsearch.common.unit.TimeValue;  
  • import org.elasticsearch.common.xcontent.XContentBuilder;  
  • import org.elasticsearch.common.xcontent.XContentFactory;  
  • import org.elasticsearch.index.query.QueryBuilder;  
  • import org.elasticsearch.index.query.QueryBuilders;  
  • import org.elasticsearch.indices.IndexAlreadyExistsException;  
  • import org.elasticsearch.node.Node;  
  • import org.elasticsearch.node.NodeBuilder;  
  • import org.elasticsearch.search.SearchHit;  
  • import org.junit.BeforeClass;  
  • import org.junit.Test;  
  •   
  • /** 
  •  * <p> 
  •  * MyTest.java 
  •  * </p> 
  •  * <p> 
  •  * </p> 
  •  *  
  •  * @author $Author: Geloin $ 
  •  * @version $Revision: V5.1 $ 
  •  */  
  • public class MyTest {  
  •   
  •     private static Node node;  
  •   
  •     @BeforeClass  
  •     public static void beforeClass() {  
  •         node = NodeBuilder.nodeBuilder().node();  
  •     }  
  •   
  •     /** 
  •      *  
  •      * <p> 
  •      * 创建索引 
  •      * </p> 
  •      * @author Geloin 
  •      * Created [2012-12-29 下午5:38:22] 
  •      * @throws Exception 
  •      */  
  •     @Test  
  •     public void createIndex() throws Exception {  
  •   
  •         Client client = node.client();  
  •         try {  
  •   
  •             try {  
  •                 // 预定义一个索引  
  •                 client.admin().indices().prepareCreate("app").execute().actionGet();  
  •                   
  •                 // 定义索引字段属性  
  •                 XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  •                 mapping = mapping.startObject("title")  
  •                                 // 创建索引时使用paoding解析  
  •                                 .field("indexAnalyzer""paoding")  
  •                                 // 搜索时使用paoding解析  
  •                                 .field("searchAnalyzer""paoding")  
  •                                 .field("store""yes")  
  •                           .endObject();  
  •                 mapping = mapping.endObject();  
  •   
  •                 PutMappingRequest mappingRequest = Requests.putMappingRequest("app").type("article").source(mapping);  
  •                 client.admin().indices().putMapping(mappingRequest).actionGet();  
  •             }  
  •             catch (IndexAlreadyExistsException e) {  
  •                 System.out.println("索引库已存在");  
  •             }  
  •   
  •             // 生成文档  
  •             XContentBuilder doc = XContentFactory.jsonBuilder().startObject();  
  •             doc = doc.field("title""java附魔大师");  
  •             doc = doc.endObject();  
  •   
  •             // 创建索引  
  •             IndexResponse response = client.prepareIndex("app""article""1").setSource(doc).execute().actionGet();  
  •   
  •             System.out.println(response.getId() + "====" + response.getIndex() + "====" + response.getType());  
  •         }  
  •         catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •         finally {  
  •             client.close();  
  •         }  
  •     }  
  •   
  •     /** 
  •      *  
  •      * <p> 
  •      * 查询 
  •      * </p> 
  •      * @author Geloin 
  •      * Created [2012-12-29 下午5:40:55] 
  •      * @throws Exception 
  •      */  
  •     @Test  
  •     public void search() throws Exception {  
  •         Client client = node.client();  
  •         try {  
  •             QueryBuilder qb = QueryBuilders.termQuery("title""大师");  
  •             SearchResponse scrollResp = client.prepareSearch("app").setSearchType(SearchType.SCAN).setScroll(  
  •                     new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();  
  •   
  •             while (true) {  
  •                 scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();  
  •                 for (SearchHit hit : scrollResp.getHits()) {  
  •                     Map<String, Object> source = hit.getSource();  
  •                     if (!source.isEmpty()) {  
  •                         for (Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); it.hasNext();) {  
  •                             Map.Entry<String, Object> entry = it.next();  
  •                             System.out.println(entry.getKey() + "=======" + entry.getValue());  
  •   
  •                         }  
  •                     }  
  •   
  •                 }  
  •                 if (scrollResp.hits().hits().length == 0) {  
  •                     break;  
  •                 }  
  •   
  •             }  
  •         }  
  •         catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •         finally {  
  •             client.close();  
  •         }  
  •   
  •     }  
  • }  

  
将paoding加入es中的代码主要在定义索引字段属性时,代码如下所示:
 
 
[java] view plaincopy



  • // 定义索引字段属性  
  • XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  • mapping = mapping.startObject("title")  
  •                 // 创建索引时使用paoding解析  
  •                 .field("indexAnalyzer""paoding")  
  •                 // 搜索时使用paoding解析  
  •                 .field("searchAnalyzer""paoding")  
  •                 .field("store""yes")  
  •           .endObject();  
  • mapping = mapping.endObject();  

5. 当你想为多个字段(例如一个实体的多个属性)建立索引并使用字段搜索时,代码如下所示:
 
[java] view plaincopy



  • import java.util.Date;  
  • import java.util.Iterator;  
  • import java.util.Map;  
  • import java.util.UUID;  
  •   
  • import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;  
  • import org.elasticsearch.action.index.IndexResponse;  
  • import org.elasticsearch.action.search.SearchResponse;  
  • import org.elasticsearch.action.search.SearchType;  
  • import org.elasticsearch.client.Client;  
  • import org.elasticsearch.client.Requests;  
  • import org.elasticsearch.common.unit.TimeValue;  
  • import org.elasticsearch.common.xcontent.XContentBuilder;  
  • import org.elasticsearch.common.xcontent.XContentFactory;  
  • import org.elasticsearch.index.query.QueryBuilder;  
  • import org.elasticsearch.index.query.QueryBuilders;  
  • import org.elasticsearch.indices.IndexAlreadyExistsException;  
  • import org.elasticsearch.node.Node;  
  • import org.elasticsearch.node.NodeBuilder;  
  • import org.elasticsearch.search.SearchHit;  
  • import org.junit.BeforeClass;  
  • import org.junit.Test;  
  •   
  • /** 
  •  * <p> 
  •  * MyTest.java 
  •  * </p> 
  •  * <p> 
  •  * </p> 
  •  *  
  •  * @author $Author: Geloin $ 
  •  * @version $Revision: V5.1 $ 
  •  */  
  • public class MyTest {  
  •   
  •     private static Node node;  
  •   
  •     @BeforeClass  
  •     public static void beforeClass() {  
  •         node = NodeBuilder.nodeBuilder().node();  
  •     }  
  •   
  •     /** 
  •      *  
  •      * <p> 
  •      * 创建索引 
  •      * </p> 
  •      * @author Geloin 
  •      * Created [2012-12-29 下午5:38:22] 
  •      * @throws Exception 
  •      */  
  •     @Test  
  •     public void createIndex() throws Exception {  
  •   
  •         Client client = node.client();  
  •         try {  
  •   
  •             try {  
  •                 // 预定义一个索引  
  •                 client.admin().indices().prepareCreate("app").execute().actionGet();  
  •                   
  •                 // 定义索引字段属性  
  •                 XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  •                 mapping = mapping.startObject("article");  
  •                 mapping = mapping.startObject("id")  
  •                                 // 创建索引时使用paoding解析  
  •                                 .field("indexAnalyzer""paoding")  
  •                                 // 搜索时使用paoding解析  
  •                                 .field("searchAnalyzer""paoding")  
  •                                 .field("store""yes")  
  •                           .endObject();  
  •                 mapping = mapping.startObject("title")  
  •                         // 创建索引时使用paoding解析  
  •                         .field("indexAnalyzer""paoding")  
  •                         // 搜索时使用paoding解析  
  •                         .field("searchAnalyzer""paoding")  
  •                         .field("store""yes")  
  •                         .endObject();  
  •                 mapping = mapping.startObject("createTime")  
  •                         // 创建索引时使用paoding解析  
  •                         .field("indexAnalyzer""paoding")  
  •                         // 搜索时使用paoding解析  
  •                         .field("searchAnalyzer""paoding")  
  •                         .field("store""yes")  
  •                         .endObject();  
  •                 mapping = mapping.endObject();  
  •                 mapping = mapping.endObject();  
  •                 // type的值必须与第一个startObject("...")内的值相同,且必须有一个根目录,如article为根目录,id、title和createTime为子目录  
  •                 PutMappingRequest mappingRequest = Requests.putMappingRequest("app").type("article").source(mapping);  
  •                 client.admin().indices().putMapping(mappingRequest).actionGet();  
  •             }  
  •             catch (IndexAlreadyExistsException e) {  
  •                 System.out.println("索引库已存在");  
  •             }  
  •   
  •             String id = UUID.randomUUID().toString();  
  •             // 生成文档  
  •             XContentBuilder doc = XContentFactory.jsonBuilder().startObject();  
  •             doc = doc.startObject("article");  
  •             doc = doc.field("id", id);  
  •             doc = doc.field("title""java附魔大师");  
  •             doc = doc.field("createTime"new Date());  
  •             doc = doc.endObject();  
  •             doc = doc.endObject();  
  •   
  •             // 创建索引  
  •             IndexResponse response = client.prepareIndex("app""article", id).setSource(doc).execute().actionGet();  
  •   
  •             System.out.println(response.getId() + "====" + response.getIndex() + "====" + response.getType());  
  •         }  
  •         catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •         finally {  
  •             client.close();  
  •         }  
  •     }  
  •   
  •     /** 
  •      *  
  •      * <p> 
  •      * 查询 
  •      * </p> 
  •      * @author Geloin 
  •      * Created [2012-12-29 下午5:40:55] 
  •      */  
  •     @Test  
  •     public void search() throws Exception {  
  •         Client client = node.client();  
  •         try {  
  • //            QueryBuilder qb = QueryBuilders.fieldQuery("title", "大师");  
  •             QueryBuilder qb = QueryBuilders.fieldQuery("article.title""大师");  
  •             SearchResponse scrollResp = client.prepareSearch("app").setSearchType(SearchType.SCAN).setScroll(  
  •                     new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();  
  •   
  •             while (true) {  
  •                 scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();  
  •                 for (SearchHit hit : scrollResp.getHits()) {  
  •                     Map<String, Object> source = hit.getSource();  
  •                     if (!source.isEmpty()) {  
  •                         for (Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); it.hasNext();) {  
  •                             Map.Entry<String, Object> entry = it.next();  
  •                             System.out.println(entry.getKey() + "=======" + entry.getValue());  
  •   
  •                         }  
  •                     }  
  •   
  •                 }  
  •                 if (scrollResp.hits().hits().length == 0) {  
  •                     break;  
  •                 }  
  •   
  •             }  
  •         }  
  •         catch (Exception e) {  
  •             e.printStackTrace();  
  •         }  
  •         finally {  
  •             client.close();  
  •         }  
  •   
  •     }  
  • }  

  http://blog.csdn.net/geloin/article/details/8451067

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-379358-1-1.html 上篇帖子: ElasticSearch配置文档(v1.7.2) 下篇帖子: Elasticsearch TermFacet 耗内存问题
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表