elasticsearch入门

简介

Elasticsearch是一个基于Lucene的分布式搜索和存储引擎,并基于RESTful web接口提供交互。Elasticsearch是Apache开源项目,由Java语言开发的。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。并且Lucene也是由Java语言开发的。

一:核心概念

​ Elasticsearch首先作为一个存储引擎,类比关系性数据库,它存储数据的地方叫index(索引)=table(表),数据的结构有mapping(表的属性)定义。其次是基于Lucene做的分布式服务,故有cluster(集群)和node(节点)。最后它是基于倒排索引做的搜索。

1.1: index,documents,mapping,field

​ 可将index看作是优化的document(mapping的实例)集合,而每个document都是field的集合,field是包含数据的键值对,mapping是定义document和field的语法。默认情况下,Elasticsearch索引每个field的所有数据,每个index的field都有一个专门的、优化的数据结构。例如,文本字段存储在倒排索引引中,数字或其它关键字(邮箱,URL,姓名.....不可分割的)存储在BKD(二叉树)树中。

1.2:shards

​ 即分片,它是对index的一种水平分割。每次设置index都会设置shards(默认或者手动指定),一旦设置后就不能修改。用途:向es存储document时,es会计算document的id,通过id的hash值和分片数计算该doc放入那个shards

1.3:replicas

​ 即index的备份,在集群模式中备份的数据可能和主体数据不在同一台机器上。

1.4:倒排索引(inverted index)

​ 即对文档类型的数据搜索的一种最佳实践方法,比如我要搜索一种做红烧肉的文档,我会通过搜索关键字(红烧肉的做法),而不是直接搜着文档的ID。

​ 正排索引:文档ID和文档内容的映射

文档ID 文档内容
文档1 Word1,word2,word3
文档2 Word1,word5,word4

​ 倒排索引:关键字和文档内容的映射

关键字 文档内容
Word1 文档1(坐标),文档2(坐标)
Word2 文档1(坐标)
Word3 文档1(坐标)
Word4 文档2(坐标)
Word5 文档2(坐标)
1.6:analyzer

​ 一个把字符串(文档内容)转换成词和字集合的程序,并把转换后的词和字集合存入倒排索引。通常一个analyzer的分析过程分为三步。第一步过滤无用字符如“,<....”,第二步通过分词器分成词和字集合,第三步分析词义例如dogs->dog。除了Elasticsearch自带的一些分词器外,还又很多第三方的分词器,或自定义分词器。

​ 第三方的分词器:ik分词器(中文分词器),其有两种分词模式即ik_max_word和ik_smart

二:安装和启动

参考官方文档:https://www.elastic.co/guide/en/elasticsearch/reference/7.0/getting-started-install.html

三:常用API
3.1:索引管理(新增+删除+mapping)

新增索引:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/indices-create-index.html

put http://localhost:9200/first_index 
结果{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "first_index"
}

删除索引

delete http://localhost:9200/first_index
结果:{
    "acknowledged": true
}

设置索引的分片和备份

http://localhost:9200/first_index
参数{
    "settings":{
        "number_of_shards":2,
        "number_of_replicas":1
    }
}

为索引设置mapping和filed:https://www.elastic.co/guide/en/elasticsearch/reference/6.0/mapping.html

http://localhost:9200/first_index3
参数:{
  "mappings": {
      "properties": { 
        "title":    { "type": "text"  }, 
        "name":     { "type": "text"  }, 
        "age":      { "type": "long" },  
        "created":  {
          "type":   "date", 
          "format": "strict_date_optional_time||epoch_millis"
        }
      }
    }
}
3.2:新增文档
http://localhost:9200/first_index4/_doc
参数:{
    "title": "java",
    "name": "入门Java",
    "age": 21.9,
    "created": "2021-11-01"
}
3.3:查询

​ 简介:Elasticsearch的查询不像关系型数据库(mysql...)中的查询,相比关系型数据库的查询结果yes|no。Elasticsearch的结果是多相似“How well does this document match this query clause?”。相似度的值在每个命中的结果属性“_score”中,分值越大相似度就越高(越匹配你想要的结果)。分值的计算取决你的查询条件,有的条件(match)可能会贡献得分,有的(filter)不会。

full-text queries--会通过分析器,多用于text类型的属性

​ match查询:最常用的全文搜索查询

参数:{
    "query": {
        "match": {
            "name":  "入门Java"            
        }
    }
}

term-level queries --不走分析器,故不适用于text类型数据,多用于

​ term查询:相当于mysql中的等于=

{
    "query": {
        "term": {
            "name":  "入门Java"            
        }
    }
}

​ fuzzy查询:一种模糊查询或相似查询。

  • Changing a character (box → fox)
  • Removing a character (black → lack)
  • Inserting a character (sic → sick)
  • Transposing two adjacent characters (act → cat)
{
    "query": {
        "fuzzy": {
            "name":  "bava"            
        }
    }
}

Compound queries:组合查询

​ boolean-queries:通过组合full-text或term-level查询,并利用must,filter,must_not和should计算返回值

Occur Description
must The clause (query) must appear in matching documents and will contribute to the score.
filter The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.
should The clause (query) should appear in the matching document.
must_not The clause (query) must not appear in the matching documents. Clauses are executed in filter context meaning that scoring is ignored and clauses are considered for caching. Because scoring is ignored, a score of 0 for all documents is returned.
{
  "query": {
    "bool": {
      "must": [{"match": {"title": "java"}}
      ],
      "filter": [{"range": {"created": {
              "gte": "2015-01-01"}}}
      ],
      "must_not": {"range": {"age": {
            "gte": 30,
            "lte": 40}}},
      "should": [{"term": {"age": "5"}}]
    }
  }
}
posted @ 2021-10-22 17:50  爱我-中华  阅读(55)  评论(0编辑  收藏  举报