1. 程式人生 > >elasticsearch基礎知識以及建立索引

elasticsearch基礎知識以及建立索引

一、基礎概念:

1、索引:

索引(index)是elasticsearch的一個邏輯儲存,可以理解為關係型資料庫中的資料庫,es可以把索引資料存放到一臺伺服器上,也可以sharding後存到多臺伺服器上,每個索引有一個或多個分片,每個分片可以有多個副本。

2、索引型別(index_type):

在es中,一個索引物件可以儲存多個不同用途的物件,通過索引型別(index_type)可以區分單個索引中的不同物件,可以理解為關係型資料庫中的表。每個索引型別可以有不同的結構,但是不同的索引型別不能為相同的屬性設定不同的型別。

3、文件(document):

儲存在es中的主要實體叫文件(document),可以理解為關係型資料庫中表的一行記錄。每個文件由多個欄位構成,es是一個非結構化的資料庫,每個文件可以有不同的欄位

,並且有一個唯一的識別符號。

4、對映(mapping):

ES預設是動態建立索引和索引型別的mapping的。這就相當於無需定義Solr中的Schema,無需指定各個欄位的索引規則就可以索引檔案,很方便。但有時方便就代表著不靈活。比如,ES預設一個欄位是要做分詞的,但我們有時要搜尋匹配整個欄位卻不行。如有統計工作要記錄每個城市出現的次數。對於NAME欄位,若記錄“new york”文字,ES可能會把它拆分成“new”和“york”這兩個詞,分別計算這個兩個單詞的次數,而不是我們期望的“new york”。

這時,就需要我們在建立索引時定義mapping。此外,es支援多欄位結構,例如:我們希望兩個欄位中有相同的值,一個用於搜尋,一個使用者排序;或者一個用於分詞器分析,一個用於空白字元。例如:編寫mapping檔案如下:

{	
	   "index_type":{  
		  "properties":{  
			 "ID":{  
				"type":"string",  
				"index":"not_analyzed"   
			 },  			 
			 "NAME":{
				"type":"string",
				"fields":{
					"NAME":{
						"type":"string"
					},
					"raw":{
						"type":"string",
						"index":"not_analyzed"	
					}
				}					
			 } 		 	 		
		  }  
	   }   
}
以上檔案是說我們對於index_type這個索引型別,定義了它的mapping。重點是將NAME這個欄位對映為兩個,一個是需要做索引分析的NAME,另一個是不分析的raw,即不會拆分new york這種片語。這樣我們在做搜尋的時候,就可以對NAME.raw這個欄位做term aggregation,獲得所有城市出現的次數了。term aggregation的REST方式的請求編寫如下:
{
  "query": {
    "match_all": {}
  },
  "aggregations": {
    "cityAggs": {
      "terms": {
        "field": "NAME.raw"
      }
    }
  }
}

二、建立索引:

1、使用postman工具:

1)es提供了restful api,可以通過post請求,建立索引的mapping。如下圖:url為es伺服器叢集中的一個節點ip,埠是9200,其中test是索引名稱;post資料中,test_type是索引型別名稱,裡面包含了兩個欄位id和name


2)刪除索引:

同樣,es提供了restful api,可以通過delete請求,刪除索引。


2、使用es-java api:

上面我們使用了http常用的方式建立所以,接下來我們使用api的方式:

public static boolean createIndex(String indexName, String indexType,
			String mappingSource) {

	if (isExistsIndex(indexName)) {
		return false;
	}
	IndicesAdminClient indicesAdminClient = transportClient.admin()
			.indices();
			
	// setting
	Settings settings = Settings.builder().put("index.number_of_shards", 3)
			.put("index.number_of_replicas", 2).build();
			
	// mapping
	CreateIndexResponse response = indicesAdminClient
			.prepareCreate(indexName).setSettings(settings)// setting
			.addMapping(indexType, mappingSource)// type,mapping
			.get();
	return response.isAcknowledged();
}

public static void testCreateIndex4Mapping() {
	String indexName=  "test";
	String indexType = "test_type";

	JSONObject mappingJson = new JSONObject();
	JSONObject mappingTypeJson = new JSONObject();
	JSONObject propertiesJson = new JSONObject();

	JSONObject  idJson = new JSONObject();
	idJson.put("type", "string");
	idJson.put("store", "true");
	propertiesJson.put("id", idJson);

	JSONObject nameJson = new JSONObject();
	nameJson.put("type", "string");
	propertiesJson.put("name", nameJson);
	
	mappingTypeJson.put("properties", propertiesJson);
	mappingJson.put(indexType, mappingTypeJson);
	
	logger.info(mappingJson.toJSONString());

	createIndex(indexName, indexType, mappingJson.toJSONString());
}


其中,mappingSource打印出來的字元:
{
	"test_type": {
		"properties": {
			"id": {
				"store": "true",
				"type": "string"
			},
			"name": {
				"type": "string"
			}
		}
	}
}