关于laravel:使用-Laradock-安装-ElasticSearch

32次阅读

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

应用 Laradock 装置 ElasticSearch

  • ElasticSearch 可视化工具 ElasticHQ / 官网地址

装置和应用

  1. 应用 docker-compose up 命令运行 ElasticSearch 容器

docker-compose up -d elasticsearch
  1. 关上浏览器并通过端口 9200 拜访本地主机 http://localhost:9200

默认用户是 user,默认明码是 changeme

如果是在 laradock 中应用时


curl http://elasticsearch:9200

装置 ElasticSearch 插件


# 装置一个 ElasticSearch 插件
docker-compose exec elasticsearch /usr/share/elasticsearch/bin/elasticsearch-plugin install {plugin-name}

# 重启容器
docker-compose restart elasticsearch

装置 elasticsearch-analysis-ik 中文分词插件

比方,此时须要装置 elasticsearch-analysis-ik 中文分词插件,须要下载 ik 的 releases 源码 zip 包


# 形式 1,你能够间接在 elasticsearch 容器外,执行以下命令
docker-compose exec elasticsearch /usr/share/elasticsearch/bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip

# 形式 2,你能够间接进入到 elasticsearch 容器内,而后执行以下命令
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip

须要留神的是:如果你的 elasticsearch 的版本是 7.9.1 那么,你装置的 ik 插件也必须是 7.9.1 的版本 ,elasticsearch 的版本号能够通过拜访 http://localhost:9200/ 查看 version.number 字段查看,而后 docker-compose restart elasticsearch 重启 elasticsearch 容器即可

装置 elasticsearch-analysis-ik 过程如下所示


[root@f1831cb3b4dd elasticsearch]# ./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip
-> Installing https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip
-> Downloading https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v7.9.1/elasticsearch-analysis-ik-7.9.1.zip
[=================================================] 100%??
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@     WARNING: plugin requires additional permissions     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
* java.net.SocketPermission * connect,resolve
See http://docs.oracle.com/javase/8/docs/technotes/guides/security/permissions.html
for descriptions of what these permissions allow and the associated risks.

Continue with installation? [y/N]y
-> Installed analysis-ik
[root@f1831cb3b4dd elasticsearch]# ./bin/elasticsearch-plugin list
analysis-ik

查看插件列表


./bin/elasticsearch-plugin list

ElasticSearch 和 mysql 数据库的概念比照

MySQL Elasticsearch
表(Table) 索引(Index)
记录(Row) 文档(Document)
字段(Column) 字段(Fields)

ElasticSearch 的简略应用

新建索引 index(创立表)


curl -XPUT http://localhost:9200/test_index  

# 在 Elasticsearch 的返回中如果蕴含了 "acknowledged" : true, 则代表申请胜利。{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}

查看


curl http://localhost:9200/test_index

{"test_index":{"aliases":{},"mappings":{},"settings":{"index":{"creation_date":"1617069458624","number_of_shards":"1","number_of_replicas":"1","uuid":"XKjqatZTSOu9I_PiwzaNOQ","version":{"created":"7090199"},"provided_name":"test_index"}}}}

# 能够加上 pretty 参数,返回比拟人性化的构造
curl http://localhost:9200/test_index\?pretty                                                                           
{
  "test_index" : {"aliases" : {},
    "mappings" : { },
    "settings" : {
      "index" : {
        "creation_date" : "1617069458624",
        "number_of_shards" : "1",
        "number_of_replicas" : "1",
        "uuid" : "XKjqatZTSOu9I_PiwzaNOQ",
        "version" : {"created" : "7090199"},
        "provided_name" : "test_index"
      }
    }
  }
}

创立类型

对应的接口地址是 /{index_name}/_mapping


curl -H'Content-Type: application/json' -XPUT http://localhost:9200/test_index/_mapping?pretty -d'{"properties": {"title": {"type":"text","analyzer":"ik_smart"},"description": {"type":"text","analyzer":"ik_smart"},"price": {"type":"scaled_float","scaling_factor": 100}
  }
}'

# 会返回

{"acknowledged" : true}


curl -H'Content-Type: application/json' -XPUT http://localhost:9200/products/_mapping/?pretty -d'{"properties": {"brand_id": {"type":"integer"},"type": {"type":"integer"},"title": {"type":"text","analyzer":"ik_smart"},"unit": {"type":"keyword"},"sketch": {"type":"text","analyzer":"ik_smart"},"keywords": {"type":"text","analyzer":"ik_smart"},"tags": {"type":"keyword"},"barcode": {"type":"keyword"},"price": {"type":"scaled_float","scaling_factor": 100},"market_price": {"type":"scaled_float","scaling_factor": 100},"rating": {"type":"float"},"sold_count": {"type":"integer"},"review_count": {"type":"integer"},"virtual_retail_num": {"type":"integer"},"description": {"type":"text","analyzer":"ik_smart"},"stock": {"type":"integer"},"warning_stock": {"type":"integer"},"main_image": {"type":"keyword"},"slider_image": {"type":"keyword"},"status": {"type":"integer"},"is_hot": {"type":"integer"},"sort": {"type":"integer"},"categories": {"type":"nested","properties": {"id": {"type":"integer","copy_to":"categories_id"},"pid": {"type":"integer"},"name": {"type":"text","analyzer":"ik_smart","copy_to":"categories_name"},"description": {"type":"text","analyzer":"ik_smart","copy_to":"categories_description"},"status": {"type":"integer"},"level": {"type":"integer"},"img": {"type":"keyword"}
      }
    },    
    "brand": {
      "type": "nested",
      "properties": {"id": { "type": "integer"},
        "name": {"type": "text", "analyzer": "ik_smart", "copy_to": "brand_name"}, 
        "description": {"type": "text", "analyzer": "ik_smart", "copy_to": "brand_description"},
        "log_url": {"type": "keyword"},
        "img": {"type": "keyword"}
      }
    },      
    "attrs": {
      "type": "nested",
      "properties": {"id": { "type": "integer"},
        "name": {"type": "keyword", "copy_to": "attrs_name"}
      }
    },  
    "skus": {
      "type": "nested",
      "properties": {"id": { "type": "integer"},
        "name": {"type": "text", "analyzer": "ik_smart"}, 
        "main_url": {"type": "keyword"},
        "price": {"type": "scaled_float", "scaling_factor": 100},
        "sold_count": {"type": "integer"}
      }
    }
  }
}'
  • 提交数据中的 properties 代表这个索引中各个字段的定义,其中 key 为字段名称,value 是字段的类型定义
  • type 定义了字段的数据类型,罕用的有 text / integer / date / boolean,还有更多类型

    • keyword,这是字符串类型的一种,这种类型是通知 Elasticsearch 不须要对这个字段做分词,通常用于邮箱、标签、属性等字段。
    • scaled_float 代表一个小数位固定的浮点型字段,与 Mysql 的 decimal 类型相似。
    • scaling_factor 用来指定小数位精度,100 就代表准确到小数点后两位。
    • nested 代表这个字段是一个简单对象,由下一级的 properties 字段定义这个对象的字段。
  • analyzer 是一个新的概念,这是通知 Elasticsearch 应该用什么形式去给这个字段做分词,这里咱们用了 ik_smart,是一个中文分词器。
  • copy_to,Elasticsearch 的多字段匹配查问是不反对查问 Nested 对象的字段,然而咱们又必须查问 categories.name 字段,因而咱们能够应用 copy_to 参数,能够将 categories.name 字段复制到下层,咱们就能够通过 categories_name 字段做多字段匹配查问

创立文档

对应的接口地址是 /{index_name}/_doc/{id} 这里的 id 和 mysql 中的 id 不一样,不是自增的,须要咱们手动指定。


# 创立 id 为 1 的文档
curl -H'Content-Type: application/json' -XPUT http://localhost:9200/test_index/_doc/1?pretty -d'{"title":"iPhone 7P","description":"iphone 第一批双摄像头 ","price": 6799}'

# 会返回如下内容
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 0,
  "_primary_term" : 2
}


# 创立 id 为 2 的文档
curl -H'Content-Type: application/json' -XPUT http://localhost:9200/test_index/_doc/2?pretty -d'{"title":"OPPO find x","description":" 高清像素 ","price": 3499}'

# 会返回如下内容
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "2",
  "_version" : 1,
  "result" : "created",
  "_shards" : {
    "total" : 2,
    "successful" : 1,
    "failed" : 0
  },
  "_seq_no" : 1,
  "_primary_term" : 2
}

读取文档数据


curl http://localhost:9200/test_index/_doc/1\?pretty

# 会返回如下内容
{
  "_index" : "test_index",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "_seq_no" : 0,
  "_primary_term" : 2,
  "found" : true,
  "_source" : {
    "title" : "iPhone 7P",
    "description" : "iphone 第一批双摄像头",
    "price" : 6799
  }
}

查看 Elasticsearch 索引中有多少条数据

对应的接口地址为 /{index_name}/_doc/_count


curl http://localhost:9200/test_index/_doc/_count\?pretty

# 会返回如下内容
{
  "count" : 3,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  }
}

简略搜寻


curl -XPOST -H'Content-Type:application/json' http://localhost:9200/test_index/_doc/_search\?pretty -d'{"query": {"match": {"description":"iphone"}}
}'

# 会返回如下内容
{
  "took" : 16,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 1,
      "relation" : "eq"
    },
    "max_score" : 0.60996956,
    "hits" : [
      {
        "_index" : "test_index",
        "_type" : "_doc",
        "_id" : "1",
        "_score" : 0.60996956,
        "_source" : {
          "title" : "iPhone 7P",
          "description" : "iphone 第一批双摄像头",
          "price" : 6799
        }
      }
    ]
  }
}

原文链接地址

正文完
 0