创建索引库
put ::http://127.0.0.1:9200/people
{
“settings”: {
“number_of_shards”: 3,
“number_of_replicas”: 1
},
“mappings”: {
“properties”: {
“name”: {
“type”: “text”
},
“age”: {
“type”: “integer”
}
}
}
}
创建文档
post::http://127.0.0.1:9200/people/_doc/8
{
“name” : “陈培根”,
“age” : 21
}
搜索文档
get ::http://127.0.0.1:9200/people/_doc/8
post::http://127.0.0.1:9200/people/_doc/_search
{
“query” : {
“term” : {
“name” : “陈”
}
}
}
post:: http://127.0.0.1:9200/people/_doc/_search
{
“query” : {
“query_string” : {
“default_field” : “name”,
“query” : “培根”
}
}
}
post http://127.0.0.1:9200/_analyze
{
“analyzer”: “ik_smart”,
“text”: “没有安装分词器时可以正常启动我非常不是喜欢你”
}
添加映射
post http://127.0.0.1:9200/people/_mapping
{
“properties”: {
“name1”: {
“type”: “text”
},
“age”: {
“type”: “integer”
}
}
}
查询映射
http://127.0.0.1:9200/people/_mapping
自动使用ik分词器
index:true 可以搜索
store:true 再保存一份
package config;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.alias.Alias;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.query.QueryBuilders;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
/**
* @author Administrator
* @version 1.0
**/
class App {
public static RestHighLevelClient getRestHighLevelClient() {
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(new HttpHost("localhost", 9200, "http")));
return client;
}
//创建索引
@Test
public void createIndex() throws IOException {
CreateIndexRequest request = new CreateIndexRequest("twitter");
request.settings(Settings.builder()
.put("index.number_of_shards", 3)
.put("index.number_of_replicas", 2)
);
Map<String, Object> message = new HashMap<>();
message.put("type", "text");
Map<String, Object> properties = new HashMap<>();
properties.put("message", message);
Map<String, Object> mapping = new HashMap<>();
mapping.put("properties", properties);
request.mapping(mapping);
request.alias(new Alias("twitter_alias").filter(QueryBuilders.termQuery("user", "kimchy")));
CreateIndexResponse createIndexResponse = getRestHighLevelClient().indices().create(request, RequestOptions.DEFAULT);
boolean acknowledged = createIndexResponse.isAcknowledged();
boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged();
System.out.println(acknowledged);
System.out.println(shardsAcknowledged);
}
@Test
public void testAddDoc() throws IOException {
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("message", "trying out Elasticsearch");
IndexRequest indexRequest = new IndexRequest("twitter")
.id("1").source(jsonMap); //以Map形式提供的文档源,可自动转换为JSON格式
IndexResponse indexResponse = getRestHighLevelClient().index(indexRequest, RequestOptions.DEFAULT);
String index = indexResponse.getIndex();
String id = indexResponse.getId();
System.out.println(index);
System.out.println(id);
}
}