关于芯片:Elasticsearch-操作

41次阅读

共计 3288 个字符,预计需要花费 9 分钟才能阅读完成。

1. 创立索引

创立一个名为 products 的索引,用来存储商品数据。

分片和正本参数阐明:

  • number_of_shards:分片数量,默认值是 5
  • number_of_replicas:正本数量,默认值是 1

咱们有三个节点,在每个节点上都创立一个分片。每个分片在另两个节点上各创立一个正本。

# 创立索引,命名为 products
PUT /products
{
  "settings": {
    "number_of_shards": 3, 
    "number_of_replicas": 2
  }
}

2. 创立映射

在 products 索引中创立映射。

# 定义 mapping,数据结构
PUT /products/_mapping
{
  "properties": {
    "id": {"type": "long"},
    "title": {"type": "text"},
    "category": {"type": "text"},
    "price": {"type": "float"},
    "city": {"type": "text"},
    "barcode": {"type": "keyword"}
  }
}

查看映射

GET /products/_mapping

映射(数据结构)

相似于数据库表构造,索引数据也被分为多个数据字段,并且须要设置数据类型和其余属性。

映射,是对索引中字段构造的定义和形容。

罕用类型:

  • 数字类型:

    • byte、short、integer、long
    • float、double
    • unsigned_long
  • 字符串类型:

    • text:会进行分词
    • keyword:不会进行分词,实用于 email、主机地址、邮编等
  • 日期和工夫类型:

    • date

3. 增加文档

增加的文档会有一个名为 _id 的文档 id,这个文档 id 能够主动生成,也能够手动指定,通常能够应用数据的 id 作为文档 id。

# 增加文档
PUT /products/_doc/10033
{
  "id":"10033",
  "title":"新一代 PLAY:5 无线智能音响系统",
  "category":"潮酷数码会场",
  "price":"3980.01",
  "city":"上海",
  "barcode":"527848718459"
}

PUT /products/_doc/10034
{
  "id":"10034",
  "title":"高清电视盒子 wifi 64 位硬盘播放器",
  "category":"潮酷数码会场",
  "price":"398.00",
  "city":"浙江杭州",
  "barcode":"522994634119"
}

PUT /products/_doc/10035
{
  "id":"10035",
  "title":"重低音入耳式防脱降噪音乐耳机",
  "category":"潮酷数码会场",
  "price":"860.00",
  "city":"浙江杭州",
  "barcode":"526558749068"
}

PUT /products/_doc/10036
{
  "id":"10036",
  "title":"2.0 无线蓝牙录音师头戴式耳机",
  "category":"潮酷数码会场",
  "price":"2889.00",
  "city":"上海",
  "barcode":"37147009748"
}

PUT /products/_doc/10037
{
  "id":"10037",
  "title":"美国原创 WiFi 连贯 家庭桌面音箱",
  "category":"潮酷数码会场",
  "price":"1580.01",
  "city":"上海",
  "barcode":"527783392239"
}


也能够主动生成 _id 值:

POST /products/_doc
{
  "id":"10027",
  "title":"vivo X9 前置双摄全网通 4Gvivox9",
  "category":"手机会场",
  "price":"2798.00",
  "city":"广东东莞",
  "barcode":"541396973568"
}

查看文档:

GET /products/_doc/10037` 

查看指定文档 title 字段的分词后果:

GET /products/_doc/10037/_termvectors?fields=title

4. 批改文档

底层索引数据无奈批改,批改数据实际上是先删除再从新增加。

两种批改形式:

  • PUT:对文档进行残缺的替换
  • POST:能够批改一部分字段

批改价格字段的值:

# 批改文档 - 替换
PUT /products/_doc/10037
{
  "id":"10037",
  "title":"美国原创 WiFi 连贯 家庭桌面音箱",
  "category":"潮酷数码会场",
  "price":"9999.99",
  "city":"上海",
  "barcode":"527783392239"
}

查看文档:

GET /products/_doc/10037

批改价格和城市字段的值:

# 批改文档 - 更新局部字段
POST /products/_update/10037
{
  "doc": {
    "price":"8888.88",
    "city":"深圳"
  }
}

查看文档:

GET /products/_doc/10037

5. 删除文档

`DELETE /products/_doc/10037` 

清空

POST /products/_delete_by_query
{
  "query": {"match_all": {}
  }
}

6. 删除索引

# 删除 products 索引
DELETE /products

7. 复合查问

创立索引

PUT /pditems
{
  "settings": {
    "number_of_shards": 3, 
    "number_of_replicas": 2
  },
  "mappings": {
    "properties": {
      "id": {"type": "long"},
      "brand": {"type": "text"},
      "title": {"type": "text"},
      "sell_point": {
        "type": "text",
        "analyzer": "ik_max_word",
        "search_analyzer": "ik_smart"
      },
      "price": {"type": "float"},
      "image": {"type": "keyword"},
      "cid": {"type": "long"},
      "status": {"type": "byte"},
      "created": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      },
      "updated": {
        "type": "date",
        "format": "yyyy-MM-dd HH:mm:ss"
      }
    } 
  }
}

查看数据

搜寻 pditems 索引中全副 3160 条数据:

GET /pditems/_search
{
  "query": {"match_all": {}
  },
  "size": 3160
}

8. 搜寻文档

搜寻所有数据

# 搜寻 pditems 索引中全副数据
POST /pditems/_search
{
  "query": {"match_all": {}
  }
}

关键词搜寻

# 查问 pditems 索引中 title 中蕴含 "电脑" 的商品
POST /pditems/_search
{
  "query": {
    "match": {"title": "电脑"}
  }
}

搜寻后果过滤器

# 价格大于 2000,并且 title 中蕴含 "电脑" 的商品
POST /pditems/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {"title": "电脑"}
        }
      ],

      "filter": [
        {
          "range": {
            "price": {"gte": "2000"}
          }
        }
      ]
    }
  }
}

搜寻后果高亮显示

POST /pditems/_search
{
    "query": {
        "multi_match":{
            "query": "手机",
            "fields": ["title", "sell_point"]
        }
    },
    "highlight" : {"pre_tags" : ["<i class="highlight">"],
        "post_tags" : ["</i>"],
        "fields" : {"title" : {},
            "sell_point" : {
              "pre_tags": "<em>",
              "post_tags": "</em>"
            }
        }
    }
}

正文完
 0