elasticsearch

创建索引库
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);

    }

}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值