需求
需要使用精确搜索和全文搜索,找到相关数据的ID或者唯一的名称。使用Elastic Search建立搜索引擎。
Restful Api 需求
暴露给用户的是 Restful Api。
有以下场景:
- 用户插入数据。
 - 用户更新价格。
 - 用户根据Tag查询并分页展示。
 - 用户根据ID查询数据详细信息。
 
Restful接口规范
1.插入数据到ElasticSearch
url:/api/v1/dataset
method:POST
请求:
{
    "Name":"上海第九人民医院青少年骷髅腿数据",
    "TAG1":"青少年疾病",
        "TAG2":"骨科",
        "TAG3":"上海第九人民医院青少年骷髅腿数据1949年到2004年",
        "StartTimeCollected":"20110404",
        "EndTimeCollected":"20120909",
        "Price":"0.1",
        "CoinType":"ong"
}
| Field Name | Type | Description | 
|---|---|---|
| Name | String | 标识一条数据,用户插入数据的时候指定 | 
| ID | String | 标识一条数据,查询系统生成 | 
| Tag1 | String | 大类别 | 
| Tag2 | String | 小类别 | 
| Tag3 | Text | 描述,属性 | 
| Price | String | 价格 | 
| CoinType | String | 货币种类 | 
| StartTimeCollected | Date | UTC时间,数据开始采集的时间 | 
| EndTimeCollected | Date | UTC时间,数据结束采集的时间 | 
响应:
{
  "error":0,
  "desc":"SUCCESS",
  "result": "ID"
}
| Field Name | Type | Description | 
|---|---|---|
| error | int | 错误码 | 
| desc | String | 成功为SUCCESS,失败为错误描述 | 
| result | String | 成功返回数据ID,失败返回"" | 
2.更新数据到ElasticSearch
url:/api/v1/dataset
method:POST
请求:
{
        "ID":"0000000001",
    "Name":"上海第九人民医院青少年骷髅腿数据",
    "TAG1":"青少年疾病",
        "TAG2":"骨科",
        "TAG3":"上海第九人民医院青少年骷髅腿数据1949年到2004年",
        "StartTimeCollected":"20110404",
        "EndTimeCollected":"20120909",
        "Price":"0.1",
        "CoinType":"ong"
}
数据格式和插入的一样。
响应:
{
  "error":0,
  "desc":"SUCCESS",
  "result": "ID"
}
| Field Name | Type | Description | 
|---|---|---|
| error | int | 错误码 | 
| desc | String | 成功为SUCCESS,失败为错误描述 | 
| result | String | 成功返回数据ID,失败返回"" | 
3.查询数据,分页返回
url:/api/v1/dataset?{name=}&{tag1=}&{tag2=}&{tag3=}&&{page_index=}&{page_offset=}
method:Get
返回分页数据。tag1,tag2,name,id都是精确匹配,tag3是全文检索。
响应:
{
  "error":0,
  "desc":"SUCCESS",
  "result": {
            "total": "",
            "records": []
    }
}
| Field Name | Type | Description | 
|---|---|---|
| error | int | 错误码 | 
| desc | String | 成功为SUCCESS,失败为错误描述 | 
| result | Object | 返回分页数据 | 
| total | String | 总页数 | 
| records | Array | Array里面每个数据和插入的数据一个格式 | 
4. 根据ID返回数据
url:/api/v1/dataset/id
method:Get
响应:
{
  "error":0,
  "desc":"SUCCESS",
  "result": {
        //和插入的数据一样
    }
}
5. 权限
目前Restful API没有设计权限系统,由使用代码的第三方自己实现
Elastic Search Java API
1. 创建Index
索引,类似数据库
    public static boolean createIndex(String index) {
        if (!isIndexExist(index)) {
            LOGGER.info("Index is not exits!");
        }
        CreateIndexResponse indexresponse = client.admin().indices().prepareCreate(index).execute().actionGet();
        LOGGER.info("执行建立成功?" + indexresponse.isAcknowledged());
        return indexresponse.isAcknowledged();
    }
    /**
     * 判断索引是否存在
     *
     * @param index
     * @return
     */
    public static boolean isIndexExist(String index) {
        IndicesExistsResponse inExistsResponse = client.admin().indices().exists(new IndicesExistsRequest(index)).actionGet();
        if (inExistsResponse.isExists()) {
            LOGGER.info("Index [" + index + "] is exist!");
        } else {
            LOGGER.info("Index [" + index + "] is not exist!");
        }
        return inExistsResponse.isExists();
    }
2. 插入的数据
数据格式:
| 列 | 类型 | 作用 | 
|---|---|---|
| Name | String | 标识一条数据,用户插入数据的时候指定 | 
| ID | String | 标识一条数据,查询系统生成 | 
| Tag1 | String | 大类别 | 
| Tag2 | String | 小类别 | 
| Tag3 | Text | 描述,属性 | 
| Price | String | 价格 | 
| CoinType | String | 货币种类 | 
| StartTimeCollected | Date | UTC时间,数据开始采集的时间 | 
| EndTimeCollected | Date | UTC时间,数据结束采集的时间 | 
三级标签搜索方式:
| 标签名称 | 标签优先级 | 查询方式 | 
|---|---|---|
| Tag1 | 高 | 精确匹配 | 
| Tag2 | 中 | 精确匹配 | 
| Tag3 | 低 | 全文检索 | 
还有其他的属性,存贮在Mysql,Elastic Search主要负责对上面Tag进行搜索。
/**
     * 数据添加
     *
     * @param jsonObject 要增加的数据
     * @param index      索引,类似数据库
     * @param type       类型,类似表
     * @param id         数据ID
     * @return
     */
    public static String addData(JSONObject jsonObject, String index, String type, String id) {
        IndexResponse response = client.prepareIndex(index, type, id).setSource(jsonObject).get();
        LOGGER.info("addData response status:{},id:{}", response.status().getStatus(), response.getId());
        return response.getId();
    }
3. 更新或者删除数据
- 对插入
Elastic Search的index,document,type进行更新。 - 通过ID删除数据。
 
    /**
     * 通过ID删除数据
     *
     * @param index 索引,类似数据库
     * @param type  类型,类似表
     * @param id    数据ID
     */
    public static void deleteDataById(String index, String type, String id) {
        DeleteResponse response = client.prepareDelete(index, type, id).execute().actionGet();
        LOGGER.info("deleteDataById response status:{},id:{}", response.status().getStatus(), response.getId());
    }
/**
     * 通过ID 更新数据
     *
     * @param jsonObject 要增加的数据
     * @param index      索引,类似数据库
     * @param type       类型,类似表
     * @param id         数据ID
     * @return
     */
    public static void updateDataById(JSONObject jsonObject, String index, String type, String id) {
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(index).type(type).id(id).doc(jsonObject);
        client.update(updateRequest);
    }
4. 查询数据并分页
  /**
     * 使用分词查询,并分页
     *
     * @param index          索引名称
     * @param type           类型名称,可传入多个type逗号分隔
     * @param startPage      当前页
     * @param pageSize       每页显示条数
     * @param query          查询条件
     * @param fields         需要显示的字段,逗号分隔(缺省为全部字段)
     * @param sortField      排序字段
     * @param highlightField 高亮字段
     * @return
     */
    public static EsPage searchDataPage(String index, String type, int startPage, int pageSize, QueryBuilder query, String fields, String sortField, String highlightField) {