当前位置: 首页 > elasticsearch, 搜索 > 正文

elasticsearch建立索引,查询实例

1 星2 星3 星4 星5 星 (1 次投票, 评分: 5.00, 总分: 5)
Loading ... Loading ...
baidu_share

关于如何获取java client ,请参看上一篇文章elasticsearch获取java client实例

在建立索引之前,首先要建立mapping,相当于数据库里面的表,代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
	public static XContentBuilder createMapping(String indexName){
		XContentBuilder mapping = null;
		try {
			mapping = jsonBuilder()
					.startObject()
						// 索引库名(类似数据库中的表)
						.startObject(indexName)
 
							// 索引里面的字段
							.startObject("properties")
 
								.startObject("id")
									.field("type", "long")
									.field("store", "yes")
									.field("index", "not_analyzed")
									.field("include_in_all", "false")
								.endObject()
 
 
								.startObject("thumb")
									.field("type", "string")
									.field("store", "yes")
									.field("include_in_all", "false")
								.endObject()
 
							.endObject()
						.endObject()
					.endObject();
		} catch (IOException e) {
			e.printStackTrace();
		}
 
		return mapping;
	}

然后进行批量添加索引:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
	public static Integer addIndex(String indexName, String indexType){
 
 
	    Client esClient = ESServiceSingleton2.getTransportClient();
 
		BulkRequestBuilder bulkRequest = esClient.prepareBulk();   
 
		for(int i=1;i<5;i++){
	        try {
				bulkRequest.add(esClient.prepareIndex(indexName, indexType, ""+i)   
  			.setSource(jsonBuilder()
					.startObject()
						.field("id", ""+i)
 
						.field("thumb", "http://www.chepoo.com/imges/"+i+".jpg")
 
					.endObject())
		        );
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try{
		bulkRequest.add(esClient.prepareIndex(indexName, indexType, ""+5)   
	  			.setSource(jsonBuilder()
						.startObject()
							.field("id", ""+5)
 
							.field("thumb", "http://www.chepoo.com/imges/"+5+".png")
 
						.endObject())
			        );
		}catch(Exception e){
			e.printStackTrace();
		}
		bulkRequest.execute().actionGet();   
 
 
		return bulkRequest.numberOfActions();
	}

索引,添加完后,我们进行一个简单的查询实例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
	public static void main(String[] args) throws Exception{
		Client client = ESServiceSingleton2.getTransportClient();
		client.admin().indices().prepareDelete().execute().actionGet();
 
		createIndex("test", "xq");
		addIndex("test", "xq");
 
		client.admin().indices().flush(new FlushRequest("test").refresh(true)).actionGet();
 
		SearchResponse response = client.prepareSearch("test")
		        .setTypes("xq")
                        //查询图片为jpg或者png
		        .setQuery(new QueryStringQueryBuilder("png|jpg").field("thumb"))            		       
		        .setFrom(0).setSize(60).setExplain(true)
		        .execute()
		        .actionGet();		
               System.out.println(response);
 
 
		client.close();
	}

本文固定链接: http://www.chepoo.com/elasticsearch-create-index-query-demo.html | IT技术精华网

elasticsearch建立索引,查询实例:等您坐沙发呢!

发表评论