_ElasticSearch+Kibana测试环境安装 _ docker pull elasticsearch:7.9.0 docker pull kibana:7.9.0 docker run -d -p 9200:9200 -p 9300:9300 -e "discovery.type=single-node" -v /Users/val/Documents/my/elasticsearch/data:/usr/share/elasticsearch/data --name elasticsearch elasticsearch:7.9.0 docker exec -it elasticsearch /bin/bash docker run -d -p 5601:5601 --link=elasticsearch --name kibana kibana:7.9.0 _Kibana DevTools 基本使用 _ # 打开控制台 http://localhost:5601/app/dev\_tools#/console # 创建索引 PUT es_biz_trade_product { "settings": { "number_of_shards": 1, "number_of_replicas": 1 } } # 删除索引 DELETE es_biz_trade_product # 查询索引 GET es_biz_trade_product # 查询索引是否存在 HEAD es_biz_trade_product # 创建映射关系(数据库列) type: 类型,text/long/short/integer/object index: 是否索引,默认为true store: 是否存储,默认为false analyzer: 分词器 PUT es_biz_trade_product/_mapping { "properties": { "id": { "type": "long" }, "name": { "type": "text" }, "original_price": { "type": "double" }, "current_price": { "type": "double" } } } # 查询映射关系 GET es_biz_trade_product/_mapping
添加 / 更新数据 # 新增 POST es_biz_trade_product/_doc { "id": "1", "name": "AI商品", "original_price": 32.5, "current_price": 30.5 } # 修改 POST es_biz_trade_product/_doc/qu7mCnQB9QusIJpJXdEe { "id": "1", "name": "商品", "original_price": 32.5, "current_price": 30.5 } # 新增 POST es_biz_trade_product/_doc { "id": "2", "name": "商品", "original_price": 40.5, "current_price": 38.5 } # 查询所有 GET es_biz_trade_product/_search # 根据ID查询单个 GET es_biz_trade_product/_doc/7e7oCnQB9QusIJpJ6NEv # 删除数据 DELETE es_biz_trade_product/_doc/7e7oCnQB9QusIJpJ6NEv
_#####查询使用 _ # 查询所有 GET es_biz_trade_product/_search # 根据ID查询单个 GET es_biz_trade_product/_doc/7e7oCnQB9QusIJpJ6NEv GET /索引库名/_search { "query":{ "查询类型":{ "查询条件":"查询条件值" } } } 返回 time_out:是否超时 _shards:分片信息 hits:搜索结果总览对象 total:搜索到的总条数 max_score:所有结果中文档得分的最高分 hits:搜索结果的文档对象数组,每个元素是一条搜索到的文档信息 _index:索引库 _type:文档类型 _id:文档id _score:文档得分 _source:文档的源数据 # 查询所有 GET es_biz_trade_product/_search { "query": { "match_all": { } } } # 匹配查询(模糊分词匹配,词之间是OR关系) GET es_biz_trade_product/_search { "query": { "match": { "name": "里斯本" } } } # 匹配查询(全匹配,词之间是and关系),里斯本,过滤了 里斯 GET es_biz_trade_product/_search { "query": { "match": { "name": { "query": "里斯本", "operator": "and" } } } } # 最小匹配度查询(50%类似OR,100%类似AND) GET es_biz_trade_product/_search { "query": { "match": { "name": {"query":"里斯本", "minimum_should_match": "100%"} } } } # 多字段查询 GET es_biz_trade_product/_search { "query": { "multi_match": { "query": "10", "fields": ["id","name"] } } } # 词条匹配(精确查找),terms可以匹配多个 terms:{ "original_price": ["100","50"] } GET es_biz_trade_product/_search { "query": { "term": { "original_price": "100" } } } # 结果过滤,只要 name 字段 GET es_biz_trade_product/_search { "query": { "terms": { "original_price": ["100"] } }, "_source": "name" } # 结果过滤,不带 current_price 字段 GET es_biz_trade_product/_search { "query": { "terms": { "original_price": ["100"] } }, "_source": { "excludes": "current_price" } } # 结果过滤,只要 id, name 字段 GET es_biz_trade_product/_search { "query": { "terms": { "original_price": [ "100" ] } }, "_source": { "includes": ["id", "name"] } }