elasticsearch 入门

概念:

我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选。他可以快速的存储、搜索和分析海量数据。Spring Boot通过整合Spring Data ElasticSearch为我们提供了非常便捷的检索功能支持;

Elasticsearch是一个分布式搜索服务,提供Restful API,底层基于Lucene,采用多shard(分片)的方式保证数据安全,并且提供自动resharding的功能,github等大型的站点也是采用了ElasticSearch作为其搜索服务。

  • 以 员工文档 的形式存储为例:一个文档代表一个员工数据。存储数据到 ElasticSearch 的行为叫做 索引 ,但在索引一个文档之前,需要确定将文档存储在哪里。

  • 一个 ElasticSearch 集群可以 包含多个 索引 ,相应的每个索引可以包含多个 类型 。 这些不同的类型存储着多个 文档 ,每个文档又有 多个 属性 。

  • 类似关系:

    索引-数据库   /  类型-表  /  文档-表中的记录  /   属性-列

put :放数据
get :获取数据
head:检查是否存在该数据,有则响应200,没有就404


http://192.168.0.113:9200/megacorp/employee/1

{
    "first_name" : "John",
    "last_name" : "Smith",
    "age" : 25,
    "about" : "I love to go rock climbing",
    "interests" : [ "sports","music"]
}


{
    "first_name" : "Jane",
    "last_name" : "Smith",
    "age" : 32,
    "about" : "I like to collect rock albums",
    "interests" : [ "music"]
}


{
    "first_name" : "Douglas",
    "last_name" : "Fir",
    "age" : 35,
    "about" : "I like to build cabinets",
    "interests" : [ "forestry"]
}

http://192.168.0.113:9200/megacorp/employee/_search

http://192.168.0.113:9200/megacorp/employee/_search?q=last_name:Smith




get没有请求体,所以放到post请求里面

轻量搜索:
{
"query" : {
    "match" : {
        "last_name" : "Smith"
    }
}
}

全文检索:
字段包含rock /   climbing  都可能出现
{
"query" : {
    "match" :{
    "about" : "rock climbing"
    }
}
}

 "max_score": 0.53484553   :  相关性得分


 短语搜索:
 内容完全匹配才会展示 
 {
"query" : {
    "match_phrase" :{
    "about" : "rock climbing"
    }
}
}

高亮搜索:
 {
"query" : {
    "match_phrase" :{
    "about" : "rock climbing"
    }
},
"highlight" : {
    "fields" : {
    "about" : {}
    }
}
}
结果:
<em>rock</em> <em>climbing</em>
原文地址:https://www.cnblogs.com/MagicAsa/p/10882078.html