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())); } }