Elasticsearch JAVA API ---索引api(二)

代碼示例

設置分片API:

<code>private void buildSetting(CreateIndexRequest request) {
request.settings(Settings.builder().put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2));
}/<code>

我們以短信平臺發送短信為例來創建索引結構,索引字段如下所示:

Elasticsearch JAVA API ---索引api(二)

請根據上面的表設計出索引結構。

生成索引mapping(以短信下發表為例):

<code>private void  buildIndexMapping(CreateIndexRequest request, String type) throws IOException {
XContentBuilder mappingBuilder = JsonXContent.contentBuilder()
.startObject()
.startObject("properties")
.startObject("mobile")
.field("type", "keyword")
.field("index", "true")
.endObject()

.startObject("createDate")
.field("type", "date")
.field("index", "true")
.endObject()

.startObject("sendDate")
.field("type", "date")
.field("index", "true")
.endObject()

.startObject("longCode")
.field("type", "keyword")
.field("index", "true")
.endObject()

.startObject("corpName")
.field("type", "keyword")
.field("index", "true")
.endObject()

.startObject("smsContent")
.field("type", "text")
.field("index", "true")
.field("analyzer", "ik_max_word")
.endObject()

.startObject("state")
.field("type", "integer")
.field("index", "true")
.endObject()

.startObject("province")
.field("type", "keyword")
.field("index", "true")
.endObject()

.startObject("operatorId")
.field("type", "integer")
.field("index", "true")
.endObject()

.startObject("ipAddr")
.field("type", "ip")
.field("index", "true")
.endObject()

.startObject("replyTotal")
.field("type", "integer")
.field("index", "true")
.endObject()

.startObject("fee")
.field("type", "integer")
.field("index", "true")
.endObject()
.endObject()
.endObject();
request.mapping(type, mappingBuilder);
}/<code>

創建索引要關操作:

<code>@Service("indexService")
public class IndexServiceImpl implements IndexService {
private final static Logger log = LoggerFactory.getLogger(IndexServiceImpl.class);
@Autowired
private RestHighLevelClient restHighLevelClient;

/**
* 創建索引
* @param index 索引名稱
* @param type 索引類型
* @param request 創建索引的REQUEST
* @throws IOException
*/
@Override
public void createIndex(String index,String type,CreateIndexRequest request) throws IOException {
log.info("source:" + request.toString());
if (!existsIndex(index)) {
CreateIndexResponse response = restHighLevelClient.indices().create(request, RequestOptions.DEFAULT);
System.out.println(response.toString());
log.info("索引創建結查:" + response.isAcknowledged());
} else {
log.warn("索引:{},已經存在,不能再創建。", index);
}
}

/**
* 刪除索引
* @param index 索引名稱
* @throws IOException
*/
@Override
public void deleteIndex(String index) throws IOException {
GetIndexRequest getIndexRequest = new GetIndexRequest();
getIndexRequest.indices(index);
if (restHighLevelClient.indices().exists(getIndexRequest)) {
DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest(index);
log.info("source:" + deleteIndexRequest.toString());
restHighLevelClient.indices().delete(deleteIndexRequest);
}
}

/**
* 判斷索引是否存在
* @param index
* @return
* @throws IOException
*/
public boolean existsIndex(String index) throws IOException {
GetIndexRequest request = new GetIndexRequest();
request.indices(index);
log.info("source:" + request.toString());
boolean exists = restHighLevelClient.indices().exists(request, RequestOptions.DEFAULT);
log.debug("existsIndex: " + exists);
return exists;
}

}/<code>

完整代碼地址:

https://github.com/qfjiaoyan/elasticsearch-examples


分享到:


相關文章: