esAPI使用及中文分词

1.建立映射,使用中文分词器

PUT /zw 

{
  "mappings": {
    "person" : {
      "properties" : {
        "addr" : {
          "type" :    "text",
          "analyzer": "ik_max_word"
        },
        
        "name" : {
          "type" :   "text"
        },
        "id" : {
          "type" :   "text"
        }
      }
    }
  }

}

插入数据后搜索文档

GET /zw/person/_search
{
  "query":{
    "match":{
      "addr":"天桥区北坦街道济安街7号"
    }
  }
}

结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
  },
  "hits": {
    "total": 1,
    "max_score": 0.5716521,
    "hits": [
      {
        "_index": "zw",
        "_type": "person",
        "_id": "2",
        "_score": 0.5716521,
        "_source": {
          "name": "bb",
          "id": "sf18888888",
          "addr": "山东省济南市天桥区北坦街道济安街7号旭杰经贸有限公司"
        }
      }
    ]
  }
}



API使用:

maven:

<dependency>
    <groupId>org.elasticsearch.client</groupId>
    <artifactId>rest</artifactId>
    <version>5.4.1</version>
</dependency>

<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.11</version>
    <scope>test</scope>
</dependency>

<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.41</version>
</dependency>
程序:

import com.alibaba.fastjson.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.elasticsearch.client.RestClient;
import org.junit.Before;
import org.junit.Test;

import java.util.Collections;

/**
 * Created by mike on 2018/1/30.
 */
public class Test1 {
    private static RestClient restClient;

    @Before
    public void getRest(){
        restClient = RestClient.builder(new HttpHost("cdh2", 9200, "http")).build();
    }


    @Test
    public void CreateIndex() throws Exception{
        String method = "PUT";
        String endpoint = "/test-index";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }


    /**
     * 创建文档
     * @throws Exception
     */
    @Test
    public void CreateDocument()throws Exception{

        String method = "PUT";
        String endpoint = "/zw/person/2";

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("addr","山东省济南市天桥区北坦街道济安街7号旭杰经贸有限公司");
        jsonObject.put("name","bb");
        jsonObject.put("id","sf18888888");

        HttpEntity entity = new NStringEntity(
                jsonObject.toJSONString(), ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }


    /**
     * 根据addr获取
     * @throws Exception
     */
    @Test
    public void QueryByField() throws Exception {
        String method = "GET";
        String endpoint = "/zw/person/_search";
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("addr","北坦");
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("match",jsonObject);

        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("query",jsonObject1);
        String queryStr = jsonObject2.toJSONString();
        HttpEntity entity = new NStringEntity(queryStr, ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值