分布式搜索Elasticsearch——集成paoding-maping

先做个记录,研究出来了,不记下来下次就不知道怎么处理了。

为es安装paoding插件

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

 

[java]   view plain copy
  1. 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 plain copy
  1. index:    
  2.   analysis:                       
  3.     analyzer:          
  4.       paoding:    
  5.           alias: [paoding_analyzer]    
  6.           type: org.elasticsearch.index.analysis.PaodingAnalyzerProvider   

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

 

 

[java]   view plain copy
  1. import java.util.Iterator;  
  2. import java.util.Map;  
  3.   
  4. import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;  
  5. import org.elasticsearch.action.index.IndexResponse;  
  6. import org.elasticsearch.action.search.SearchResponse;  
  7. import org.elasticsearch.action.search.SearchType;  
  8. import org.elasticsearch.client.Client;  
  9. import org.elasticsearch.client.Requests;  
  10. import org.elasticsearch.common.unit.TimeValue;  
  11. import org.elasticsearch.common.xcontent.XContentBuilder;  
  12. import org.elasticsearch.common.xcontent.XContentFactory;  
  13. import org.elasticsearch.index.query.QueryBuilder;  
  14. import org.elasticsearch.index.query.QueryBuilders;  
  15. import org.elasticsearch.indices.IndexAlreadyExistsException;  
  16. import org.elasticsearch.node.Node;  
  17. import org.elasticsearch.node.NodeBuilder;  
  18. import org.elasticsearch.search.SearchHit;  
  19. import org.junit.BeforeClass;  
  20. import org.junit.Test;  
  21.   
  22. /** 
  23.  * <p> 
  24.  * MyTest.java 
  25.  * </p> 
  26.  * <p> 
  27.  * </p> 
  28.  *  
  29.  * @author $Author: Geloin $ 
  30.  * @version $Revision: V5.1 $ 
  31.  */  
  32. public class MyTest {  
  33.   
  34.     private static Node node;  
  35.   
  36.     @BeforeClass  
  37.     public static void beforeClass() {  
  38.         node = NodeBuilder.nodeBuilder().node();  
  39.     }  
  40.   
  41.     /** 
  42.      *  
  43.      * <p> 
  44.      * 创建索引 
  45.      * </p> 
  46.      * @author Geloin 
  47.      * Created [2012-12-29 下午5:38:22] 
  48.      * @throws Exception 
  49.      */  
  50.     @Test  
  51.     public void createIndex() throws Exception {  
  52.   
  53.         Client client = node.client();  
  54.         try {  
  55.   
  56.             try {  
  57.                 // 预定义一个索引  
  58.                 client.admin().indices().prepareCreate("app").execute().actionGet();  
  59.                   
  60.                 // 定义索引字段属性  
  61.                 XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  62.                 mapping = mapping.startObject("title")  
  63.                                 // 创建索引时使用paoding解析  
  64.                                 .field("indexAnalyzer""paoding")  
  65.                                 // 搜索时使用paoding解析  
  66.                                 .field("searchAnalyzer""paoding")  
  67.                                 .field("store""yes")  
  68.                           .endObject();  
  69.                 mapping = mapping.endObject();  
  70.   
  71.                 PutMappingRequest mappingRequest = Requests.putMappingRequest("app").type("article").source(mapping);  
  72.                 client.admin().indices().putMapping(mappingRequest).actionGet();  
  73.             }  
  74.             catch (IndexAlreadyExistsException e) {  
  75.                 System.out.println("索引库已存在");  
  76.             }  
  77.   
  78.             // 生成文档  
  79.             XContentBuilder doc = XContentFactory.jsonBuilder().startObject();  
  80.             doc = doc.field("title""java附魔大师");  
  81.             doc = doc.endObject();  
  82.   
  83.             // 创建索引  
  84.             IndexResponse response = client.prepareIndex("app""article""1").setSource(doc).execute().actionGet();  
  85.   
  86.             System.out.println(response.getId() + "====" + response.getIndex() + "====" + response.getType());  
  87.         }  
  88.         catch (Exception e) {  
  89.             e.printStackTrace();  
  90.         }  
  91.         finally {  
  92.             client.close();  
  93.         }  
  94.     }  
  95.   
  96.     /** 
  97.      *  
  98.      * <p> 
  99.      * 查询 
  100.      * </p> 
  101.      * @author Geloin 
  102.      * Created [2012-12-29 下午5:40:55] 
  103.      * @throws Exception 
  104.      */  
  105.     @Test  
  106.     public void search() throws Exception {  
  107.         Client client = node.client();  
  108.         try {  
  109.             QueryBuilder qb = QueryBuilders.termQuery("title""大师");  
  110.             SearchResponse scrollResp = client.prepareSearch("app").setSearchType(SearchType.SCAN).setScroll(  
  111.                     new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();  
  112.   
  113.             while (true) {  
  114.                 scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();  
  115.                 for (SearchHit hit : scrollResp.getHits()) {  
  116.                     Map<String, Object> source = hit.getSource();  
  117.                     if (!source.isEmpty()) {  
  118.                         for (Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); it.hasNext();) {  
  119.                             Map.Entry<String, Object> entry = it.next();  
  120.                             System.out.println(entry.getKey() + "=======" + entry.getValue());  
  121.   
  122.                         }  
  123.                     }  
  124.   
  125.                 }  
  126.                 if (scrollResp.hits().hits().length == 0) {  
  127.                     break;  
  128.                 }  
  129.   
  130.             }  
  131.         }  
  132.         catch (Exception e) {  
  133.             e.printStackTrace();  
  134.         }  
  135.         finally {  
  136.             client.close();  
  137.         }  
  138.   
  139.     }  
  140. }  


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

 

 

[java]   view plain copy
  1. // 定义索引字段属性  
  2. XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  3. mapping = mapping.startObject("title")  
  4.                 // 创建索引时使用paoding解析  
  5.                 .field("indexAnalyzer""paoding")  
  6.                 // 搜索时使用paoding解析  
  7.                 .field("searchAnalyzer""paoding")  
  8.                 .field("store""yes")  
  9.           .endObject();  
  10. mapping = mapping.endObject();  

 

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

 

[java]   view plain copy
  1. import java.util.Date;  
  2. import java.util.Iterator;  
  3. import java.util.Map;  
  4. import java.util.UUID;  
  5.   
  6. import org.elasticsearch.action.admin.indices.mapping.put.PutMappingRequest;  
  7. import org.elasticsearch.action.index.IndexResponse;  
  8. import org.elasticsearch.action.search.SearchResponse;  
  9. import org.elasticsearch.action.search.SearchType;  
  10. import org.elasticsearch.client.Client;  
  11. import org.elasticsearch.client.Requests;  
  12. import org.elasticsearch.common.unit.TimeValue;  
  13. import org.elasticsearch.common.xcontent.XContentBuilder;  
  14. import org.elasticsearch.common.xcontent.XContentFactory;  
  15. import org.elasticsearch.index.query.QueryBuilder;  
  16. import org.elasticsearch.index.query.QueryBuilders;  
  17. import org.elasticsearch.indices.IndexAlreadyExistsException;  
  18. import org.elasticsearch.node.Node;  
  19. import org.elasticsearch.node.NodeBuilder;  
  20. import org.elasticsearch.search.SearchHit;  
  21. import org.junit.BeforeClass;  
  22. import org.junit.Test;  
  23.   
  24. /** 
  25.  * <p> 
  26.  * MyTest.java 
  27.  * </p> 
  28.  * <p> 
  29.  * </p> 
  30.  *  
  31.  * @author $Author: Geloin $ 
  32.  * @version $Revision: V5.1 $ 
  33.  */  
  34. public class MyTest {  
  35.   
  36.     private static Node node;  
  37.   
  38.     @BeforeClass  
  39.     public static void beforeClass() {  
  40.         node = NodeBuilder.nodeBuilder().node();  
  41.     }  
  42.   
  43.     /** 
  44.      *  
  45.      * <p> 
  46.      * 创建索引 
  47.      * </p> 
  48.      * @author Geloin 
  49.      * Created [2012-12-29 下午5:38:22] 
  50.      * @throws Exception 
  51.      */  
  52.     @Test  
  53.     public void createIndex() throws Exception {  
  54.   
  55.         Client client = node.client();  
  56.         try {  
  57.   
  58.             try {  
  59.                 // 预定义一个索引  
  60.                 client.admin().indices().prepareCreate("app").execute().actionGet();  
  61.                   
  62.                 // 定义索引字段属性  
  63.                 XContentBuilder mapping = XContentFactory.jsonBuilder().startObject();  
  64.                 mapping = mapping.startObject("article");  
  65.                 mapping = mapping.startObject("id")  
  66.                                 // 创建索引时使用paoding解析  
  67.                                 .field("indexAnalyzer""paoding")  
  68.                                 // 搜索时使用paoding解析  
  69.                                 .field("searchAnalyzer""paoding")  
  70.                                 .field("store""yes")  
  71.                           .endObject();  
  72.                 mapping = mapping.startObject("title")  
  73.                         // 创建索引时使用paoding解析  
  74.                         .field("indexAnalyzer""paoding")  
  75.                         // 搜索时使用paoding解析  
  76.                         .field("searchAnalyzer""paoding")  
  77.                         .field("store""yes")  
  78.                         .endObject();  
  79.                 mapping = mapping.startObject("createTime")  
  80.                         // 创建索引时使用paoding解析  
  81.                         .field("indexAnalyzer""paoding")  
  82.                         // 搜索时使用paoding解析  
  83.                         .field("searchAnalyzer""paoding")  
  84.                         .field("store""yes")  
  85.                         .endObject();  
  86.                 mapping = mapping.endObject();  
  87.                 mapping = mapping.endObject();  
  88.                 // type的值必须与第一个startObject("...")内的值相同,且必须有一个根目录,如article为根目录,id、title和createTime为子目录  
  89.                 PutMappingRequest mappingRequest = Requests.putMappingRequest("app").type("article").source(mapping);  
  90.                 client.admin().indices().putMapping(mappingRequest).actionGet();  
  91.             }  
  92.             catch (IndexAlreadyExistsException e) {  
  93.                 System.out.println("索引库已存在");  
  94.             }  
  95.   
  96.             String id = UUID.randomUUID().toString();  
  97.             // 生成文档  
  98.             XContentBuilder doc = XContentFactory.jsonBuilder().startObject();  
  99.             doc = doc.startObject("article");  
  100.             doc = doc.field("id", id);  
  101.             doc = doc.field("title""java附魔大师");  
  102.             doc = doc.field("createTime"new Date());  
  103.             doc = doc.endObject();  
  104.             doc = doc.endObject();  
  105.   
  106.             // 创建索引  
  107.             IndexResponse response = client.prepareIndex("app""article", id).setSource(doc).execute().actionGet();  
  108.   
  109.             System.out.println(response.getId() + "====" + response.getIndex() + "====" + response.getType());  
  110.         }  
  111.         catch (Exception e) {  
  112.             e.printStackTrace();  
  113.         }  
  114.         finally {  
  115.             client.close();  
  116.         }  
  117.     }  
  118.   
  119.     /** 
  120.      *  
  121.      * <p> 
  122.      * 查询 
  123.      * </p> 
  124.      * @author Geloin 
  125.      * Created [2012-12-29 下午5:40:55] 
  126.      */  
  127.     @Test  
  128.     public void search() throws Exception {  
  129.         Client client = node.client();  
  130.         try {  
  131. //            QueryBuilder qb = QueryBuilders.fieldQuery("title", "大师");  
  132.             QueryBuilder qb = QueryBuilders.fieldQuery("article.title""大师");  
  133.             SearchResponse scrollResp = client.prepareSearch("app").setSearchType(SearchType.SCAN).setScroll(  
  134.                     new TimeValue(60000)).setQuery(qb).setSize(100).execute().actionGet();  
  135.   
  136.             while (true) {  
  137.                 scrollResp = client.prepareSearchScroll(scrollResp.getScrollId()).setScroll(new TimeValue(600000)).execute().actionGet();  
  138.                 for (SearchHit hit : scrollResp.getHits()) {  
  139.                     Map<String, Object> source = hit.getSource();  
  140.                     if (!source.isEmpty()) {  
  141.                         for (Iterator<Map.Entry<String, Object>> it = source.entrySet().iterator(); it.hasNext();) {  
  142.                             Map.Entry<String, Object> entry = it.next();  
  143.                             System.out.println(entry.getKey() + "=======" + entry.getValue());  
  144.   
  145.                         }  
  146.                     }  
  147.   
  148.                 }  
  149.                 if (scrollResp.hits().hits().length == 0) {  
  150.                     break;  
  151.                 }  
  152.   
  153.             }  
  154.         }  
  155.         catch (Exception e) {  
  156.             e.printStackTrace();  
  157.         }  
  158.         finally {  
  159.             client.close();  
  160.         }  
  161.   
  162.     }  
  163. }  

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

你可能感兴趣的:(elasticsearch)