`

SpringBoot-第十二章 搜索框架ElasticSearch整合SpringBooot

 
阅读更多

mysql:database,table,record

ES:           index,type,document

ES在6.x版本以后重大变更:一个index只能存在一个type

 

需要建一个新用户来启动ES,root用户没法启动

 

安装后没法访问http://安装ES的IP:9200

$ES_HOME/config/下elasticsearch-.yml配置文件 配置下面参数
network.host: xx.xx.xx.xx

http.port: 9200

 

ERROR: [3] bootstrap checks failed

修改config/elasticsearch.yml
discovery.type: single-node

再启动,启动成功!此时不进行启动检查!## File descriptor check 跳过

 

要在防火墙中放开9200 9300端口

-A INPUT -p tcp -m state --state NEW -m tcp --dport 9200 -j ACCEPT

-A INPUT -p tcp -m state --state NEW -m tcp --dport 9300 -j ACCEPT

 

参考链接:

https://www.cnblogs.com/yehui/p/9087845.html

https://blog.csdn.net/cool__7/article/details/81136987

 

 

ES的启动确认

http://10.138.60.123:9200

ES的健康状态检查

http://10.138.60.123:9200/_cat/health?v

查看ES的索引

http://10.138.60.123:9200/_cat/indices?v

创建ES索引

PUT:http://10.138.60.123:9200/customer?pretty

插入一条数据

http://10.138.60.123:9200/customer/external/1?pretty

{ "name":"kevin" }

返回

{

    "_index": "customer",数据库

    "_type": "external",表

    "_id": "1",主键

    "_version": 1,

    "result": "created",

    "_shards": {

        "total": 2,

        "successful": 1,

        "failed": 0

    },

    "created": true

}

 

查询一条记录

get:http://10.138.60.123:9200/customer/external/1?pretty

返回

{ "_index": "customer", "_type": "external", "_id": "1", "_version": 1, "found": true,是否已经找到 "_source": { "name": "kevin" } }

 

 

 在使用SpringBoot整合Elasticsearch 之前,我们应该了解下它们之间对应版本关系。

Spring Boot Version (x) Spring Data Elasticsearch Version (y) Elasticsearch Version (z)
x <= 1.3.5 y <= 1.3.4 z <= 1.7.2*
x >= 1.4.x 2.0.0 <=y < 5.0.0** 2.0.0 <= z < 5.0.0**

 

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-elasticsearch</artifactId>
</dependency>

 

spring.data.elasticsearch.cluster-name=elasticsearch
spring.data.elasticsearch.cluster-nodes=xxx.xxx.xxx.xxx:9300
spring.data.elasticsearch.repositories.enabled=true

 

 

 

@Document(
        indexName = "blog",//要小写 代表数据库
type = "atricle"//要小写 代表表
)
public class Article implements Serializable{
    private static final long serialVersionUID=1L;
    private long id;
    private String titile;
    private String summary;
    private String content;
    private int pv;

    public long getId() {
        return id;
    }

    public void setId(long id) {
        this.id = id;
    }

    public String getTitile() {
        return titile;
    }

    public void setTitile(String titile) {
        this.titile = titile;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public int getPv() {
        return pv;
    }

    public void setPv(int pv) {
        this.pv = pv;
    }
}

 

@Component
@Document(indexName = "blog",type="atricle",shards = 1,replicas = 0)
public interface ArticleRepository extends ElasticsearchRepository<Article,Long>{

}

 

@RequestMapping("/es")
@RestController
public class AtricleController {

    @Autowired
private ArticleRepository repository;

    @RequestMapping("/save")
    public String save()
    {
        Article article = new Article();
        article.setId(1);
        article.setTitile("nihao");
        article.setContent("lalalala");
        article.setSummary("66666summary");
        article.setPv(9998);
        repository.save(article);
        return "success";
    }
}

 

添加好面的代码就可以通过url地址访问了

 

http://localhost:8081/es/save 访问完就在es里添加了一条记录

 

通过查询到了一个新的数据库也就是一个新的index

http://10.138.60.123:9200/_cat/indices?v

 

health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open blog 974t88jwRpOXuRFE4SOreQ 5 1 1 0 5.6kb 5.6kb yellow open customer 2u-wByWGQaSugyhKzgZmUQ 5 1 1 0 4.1kb 4.1kb

 

http://10.138.60.123:9200/blog 查询表结构

{
    "blog": {
        "aliases": {},
        "mappings": {
            "atricle": {
                "properties": {
                    "content": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "id": {
                        "type": "long"
                    },
                    "pv": {
                        "type": "long"
                    },
                    "summary": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    },
                    "titile": {
                        "type": "text",
                        "fields": {
                            "keyword": {
                                "type": "keyword",
                                "ignore_above": 256
                            }
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "refresh_interval": "1s",
                "number_of_shards": "5",
                "provided_name": "blog",
                "creation_date": "1552542258282",
                "store": {
                    "type": "fs"
                },
                "number_of_replicas": "1",
                "uuid": "974t88jwRpOXuRFE4SOreQ",
                "version": {
                    "created": "5061599"
                }
            }
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics