elasticsearch单节点和分布式的安装以及使用代码进行操作

关于安装是在另一个博客上面写的 不知道怎么同步 有兴趣的可以点击链接去看下

博客地址:elasticsearch单节点和分布式的安装

在文章开始之前、先明确三个概念
1、索引
2、类型
3、文档
对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。

使用postman进行操作

    向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种:
        http://<ip>:<port>/<索引>/<类型>/<文档id>
下面使用post向es发送请求进行索引的创建

请求url:127.0.0.1:9200/people
请求入参:

{
    "settings": { 
        "index.number_of_shards":3,
        "index.number_of_replicas":1
    },
    "mappings": {
        "man": { 
            "properties":{
                "name":{
                    "type":"text"
                    
                },
                "height":{
                    "type":"text"
                    
                },
                "age":{
                    "type":"integer"
                    
                }
            }
        }
        
    }
}
上述的过程就是创建了一个索引为people、类型为man、类型中的properties有name、height、age这个字段

响应参数:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

插入数据有两种方式:
第一种 指定id来插入:
请求url:127.0.0.1:9200/people/man/1 (1就是我们指定的id)
请求入参json串:

{
    "name":"shuaige",
    "age" : 18,
    "height": "188cm"
}

响应:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

自动生成id:
请求url:127.0.0.1:9200/people/man
请求入参:

{
    "name":"laoshiren",
    "age" : 30,
    "height": "166cm"
}

响应参数:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWXxFS1S66ULpPmE4hFv",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

此时就创建了一个id为AWXxFS1S66ULpPmE4hFv的数据,其它的操作同理,都是像es发请求,只不过入参改改就可以

使用java代码进行操作

1、首先、构建一个springboot'工程、引入es的依赖
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

新建一个配置文件的类,初始化一个es的客户端

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public TransportClient client() throws Exception {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp端口是9300

        Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }
}

最后在一个controller里面对es进行操作

首先注入初始化es客户端:

    @Autowired
    private TransportClient client;

插入数据:

public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){

        try {
            XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                    .field("name",name)
                    .field("height",height)
                    .field("age",age)
                    .endObject();
            IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get();
            log.info("插入数据成功={}",response);
            return new ResponseEntity(response.getId(),HttpStatus.OK);

        }catch (Exception e){
            log.error("插入数据异常={}", e);
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

最后为方法配置路由去访问就可以,其它的操作同理的。
代码的地址:代码下载连接

<p>---恢复内容结束---</p>在文章开始之前、先明确三个概念
1、索引
2、类型
3、文档
对比于数据库中,索引就是一个数据库、类型就是数据库中的某张表、文档也就是表中具体的记录。抽象点来看,索引抽象成一个人、人又分为男人和女人(就是类型)、然后男人有姓名、年龄、身高等(就是文档)。

使用postman进行操作

    向es(elasticsearch,下面全部用es简称)发送操作的请求是一个RestFui风格的,类似下面这种:
        http://<ip>:<port>/<索引>/<类型>/<文档id>
下面使用post向es发送请求进行索引的创建

请求url:127.0.0.1:9200/people
请求入参:

{
    "settings": { 
        "index.number_of_shards":3,
        "index.number_of_replicas":1
    },
    "mappings": {
        "man": { 
            "properties":{
                "name":{
                    "type":"text"
                    
                },
                "height":{
                    "type":"text"
                    
                },
                "age":{
                    "type":"integer"
                    
                }
            }
        }
        
    }
}
上述的过程就是创建了一个索引为people、类型为man、类型中的properties有name、height、age这个字段

响应参数:

{
    "acknowledged": true,
    "shards_acknowledged": true
}

插入数据有两种方式:
第一种 指定id来插入:
请求url:127.0.0.1:9200/people/man/1 (1就是我们指定的id)
请求入参json串:

{
    "name":"shuaige",
    "age" : 18,
    "height": "188cm"
}

响应:

{
    "_index": "people",
    "_type": "man",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

自动生成id:
请求url:127.0.0.1:9200/people/man
请求入参:

{
    "name":"laoshiren",
    "age" : 30,
    "height": "166cm"
}

响应参数:

{
    "_index": "people",
    "_type": "man",
    "_id": "AWXxFS1S66ULpPmE4hFv",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 2,
        "failed": 0
    },
    "created": true
}

此时就创建了一个id为AWXxFS1S66ULpPmE4hFv的数据,其它的操作同理,都是像es发请求,只不过入参改改就可以

使用java代码进行操作

1、首先、构建一个springboot'工程、引入es的依赖
        <dependency>
            <groupId>org.elasticsearch.client</groupId>
            <artifactId>transport</artifactId>
            <version>5.5.2</version>
        </dependency>

        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
            <version>2.7</version>
        </dependency>

新建一个配置文件的类,初始化一个es的客户端

@Configuration
public class ElasticSearchClientConfig {

    @Bean
    public TransportClient client() throws Exception {
        InetSocketTransportAddress node = new InetSocketTransportAddress(InetAddress.getByName("localhost"),9300);//tcp端口是9300

        Settings settings = Settings.builder().put("cluster.name","sanxiongdi").build();

        TransportClient client = new PreBuiltTransportClient(settings);
        client.addTransportAddress(node);
        return client;
    }
}

最后在一个controller里面对es进行操作

首先注入初始化es客户端:

    @Autowired
    private TransportClient client;

插入数据:

public ResponseEntity addBook(@RequestParam("name")String name, @RequestParam("height")String height,@RequestParam("age")int height){

        try {
            XContentBuilder content = XContentFactory.jsonBuilder().startObject()
                    .field("name",name)
                    .field("height",height)
                    .field("age",age)
                    .endObject();
            IndexResponse response = this.client.prepareIndex("person","man").setSource(content).get();
            log.info("插入数据成功={}",response);
            return new ResponseEntity(response.getId(),HttpStatus.OK);

        }catch (Exception e){
            log.error("插入数据异常={}", e);
            return new ResponseEntity(HttpStatus.INTERNAL_SERVER_ERROR);
        }


    }

最后为方法配置路由去访问就可以,其它的操作同理的。
代码的地址:代码下载连接

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,003评论 2 89
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,837评论 18 139
  • “靠!” 顾栩烦闷地踢了踢车门,顿时感觉倒霉极了。 有谁在被灌了一晚上酒,车爆胎后还有好心情呢。 “先生,来算一卦...
    莫有小酒阅读 134评论 0 1
  • 周末了,给大家推荐一些歌吧。希望大家度过一个什么都不用去想的周末,好好放松一下。 没什么大不了 这首歌应该大家都有...
    翻身中的阿孙阅读 846评论 0 3
  • 好几年前,好友就邀我一同练瑜伽,而我拒绝的理由居然是节奏太慢,我是快节奏的人,瑜伽不适合我。 一个偶然的机会,让我...
    李小五阅读 300评论 0 5