下载Elasticsearch:

https://www.elastic.co/cn/downloads/elasticsearch

下载kibana可视化插件:

https://www.elastic.co/cn/downloads/kibana

Elasticsearch配置文件:

安装目录—config目录下—elasticsearch.yml:

  1. cluster.name: Ealdan #配置elasticsearch的集群名称,默认是elasticsearch。建议修改成一个有意义的名称。
  2. node.name: Ealdan_node_1 #节点名,通常一台物理服务器就是一个节点,es会默认随机指定一个名字,建议指定一个有意义的名称,方便管理
  3. network.host: 127.0.0.1 #绑定ip地址
  4. http.port: 9200 #暴露的http端口
  5. transport.tcp.port: 9300 #内部端口
  6. transport.host: localhost
  7. node.master: true #主节点
  8. node.data: true #数据节点
  9. node.ingest: true
  10. #discovery.zen.ping.unicast.hosts: ["0.0.0.0:9300", "0.0.0.0:9301", "0.0.0.0:9302"] #设置集群中master节点的初始列表
  11. #discovery.zen.minimum_master_nodes: 1 #主结点数量的最少值 ,此值的公式为:(master_eligible_nodes / 2) + 1 ,比如:有3个符合要求的主结点,那么这里要设置为2。
  12. bootstrap.memory_lock: false #内存的锁定只给es用
  13. node.max_local_storage_nodes: 1 #单机允许的最大存储结点数,通常单机启动一个结点建议设置为1,开发环境如果单机启动多个节点可设置大于1
  14. path.data: D:\编程软件\elasticsearch-7.8.0\data #索引目录
  15. path.logs: D:\编程软件\elasticsearch-7.8.0\logs #日志
  16. #http.cors.enabled: true # 跨域设置
  17. #http.cors.allow‐origin: /.*/

kibana配置文件:

安装目录—config目录下—elasticsearch.yml:

  1. server.port: 5601
  2. server.host: "0.0.0.0"
  3. elasticsearch.hosts: ["http://localhost:9200"]
  4. elasticsearch.requestTimeout: 90000

启动Elasticsearch:

在安装目录的bin目录下,启动elasticsearch.bat文件
浏览器输入:http://localhost:9200/
查看集群:http://localhost:9200/_cat/nodes?pretty

启动kibana:

在安装目录的bin目录下,启动kibana.bat文件
浏览器输入:http://localhost:5601/app/kibana#
7.0版本之后put型式添加文档,必须要指定id

  1. GET _search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }
  7. #查看所有节点信息
  8. GET _cat/nodes?v&s
  9. #查看所有索引状态
  10. GET _cat/indices
  11. GET _cat/indices?v&s
  12. #查看所以年相关信息
  13. GET users
  14. #查看索引的前十条文档
  15. GET movies/_search
  16. #查看状态为绿的索引
  17. GET _cat/indices?v&health=green
  18. #按照文档个数排序
  19. GET _cat/indices?v&s=docs.count:desc
  20. #查看具体的字段
  21. GET _cat/indices?pri&v&h=health,index,pri,rep,docs,count,mt
  22. GET _cat/nodes
  23. #添加索引库
  24. PUT blog
  25. {
  26. "mappings": {
  27. "properties": {
  28. "title": {
  29. "type": "text",
  30. "index": true,
  31. "store": true,
  32. "analyzer": "standard"
  33. },
  34. "comment":{
  35. "type": "keyword",
  36. "store": true,
  37. "index": false
  38. }
  39. }
  40. }
  41. }
  42. GET blog
  43. POST blog/_doc
  44. {
  45. "name": "text"
  46. }
  47. GET blog/_search
  48. DELETE blog
  49. PUT /blog
  50. {
  51. "settings": {
  52. "number_of_shards": 3,
  53. "number_of_replicas": 1
  54. },
  55. "mappings": {
  56. "properties": {
  57. "title":{
  58. "type": "text",
  59. "index": true,
  60. "store": true,
  61. "analyzer": "standard"
  62. },
  63. "comment":{
  64. "type":"keyword",
  65. "store": true,
  66. "index": true
  67. }
  68. }
  69. }
  70. }
  71. #查询索引库
  72. GET blog/_search
  73. #添加一条内容,put必须指定id
  74. PUT blog/_doc/1
  75. {
  76. "title":"Ealdan",
  77. "comment":"elastic kibana"
  78. }
  79. #添加一条内容
  80. POST blog/_doc/2
  81. {
  82. "title":"测试实例",
  83. "comment":"搜索引擎",
  84. "name":"Ealdan"
  85. }
  86. #删除一条内容
  87. DELETE blog/_doc/2
  88. #查询索引库中包含该字段的数据
  89. GET blog/_search?q=name:"Ealdan"
  90. #分词器测试
  91. POST _analyze
  92. {
  93. "analyzer": "standard",
  94. "text":"Ealdan Test"
  95. }
  96. POST movies/_analyze
  97. {
  98. "field": "title",
  99. }
  100. #查询包含的数据
  101. GET /_search
  102. {
  103. "query": {
  104. "match": {
  105. "name": "Ealdan"
  106. }
  107. }
  108. }
  109. #分词器
  110. POST _analyze
  111. {
  112. "analyzer":"ik_smart",
  113. "text":"大吉大利"
  114. }
  115. POST _analyze
  116. {
  117. "analyzer":"ik_max_word",
  118. "text":"大吉大利"
  119. }
  1. # 创建索引
  2. PUT test
  3. {
  4. "settings": {
  5. "index":{
  6. "number_of_shards":5,
  7. "number_of_replicas":1
  8. }
  9. }
  10. }
  11. # 使用默认配置创建索引pretty只是为了如果json返回打印更 加的漂亮一点
  12. PUT test2?pretty
  13. # 更改索引值
  14. PUT test/_settings
  15. {
  16. "number_of_replicas":0
  17. }
  18. # 查看指定的索引配置信息
  19. GET test/_settings
  20. # 查看所有的索引配置信息
  21. GET _all/_settings
  22. # 查看所有的索引的健康信息
  23. GET /_cat/indices?v
  24. # 向索引库中添加文档
  25. PUT /test/zjj/1
  26. {
  27. "name":"xiaou",
  28. "age":18,
  29. "desc":"我是xiaou"
  30. }
  31. # 通过id获得信息
  32. GET /test/zjj/1
  33. # GET /test/zjj出现问题
  34. # 对查询数据筛选
  35. GET /test/zjj/1?_source=age,desc
  36. # 修改文档
  37. # 1覆盖id
  38. PUT /test/zjj/1
  39. {
  40. "age":"20"
  41. }
  42. # 2修改
  43. POST /test/zjj/1
  44. {
  45. "age":18
  46. }
  47. # 更新
  48. POST /test/zjj/1/_update?pretty
  49. {
  50. "script" : "ctx._source.age += 5"
  51. }
  52. # 删除
  53. DELETE test/zjj/2
  54. # 删除索引库
  55. DELETE test
  56. PUT xiaou
  57. {
  58. "settings":{
  59. "index":{
  60. "number_of_shards": 3,
  61. "number_of_replicas": 0
  62. }
  63. }
  64. }
  65. POST /xiaou/test/_bulk
  66. {"index":{"_id":1}}
  67. {"title":"Java","price":55}
  68. {"index":{"_id":2}}
  69. {"title":"php","price":60}
  70. {"index":{"_id":3}}
  71. {"title":"ww","price":75}
  72. GET xiaou/test/_mget
  73. {
  74. "ids":["1","2","3","4"]
  75. }
  76. POST /xiaou/test/_bulk
  77. {"delete":{"_id":1}}
  78. {"create":{"_id":4}}
  79. {"title":"javascript","price":75}
  80. {"index":{"_index":"test3","_type":"t","_id":1}}
  81. {"name":"xiaou"}
  82. {"update":{"_index":"xiaou","_type":"test","_id":3}}
  83. {"doc":{"price":58}}
  84. POST /xiaou/test/_bulk
  85. {"index":{"_id":1}}
  86. {"title":"i love java"}
  87. GET /xiaou/_mapping
  88. GET xiaou/test/_search?q=java
  89. PUT test4
  90. {
  91. "settings": {
  92. "index":{
  93. "number_of_shards": 3,
  94. "number_of_replicas": 0
  95. }
  96. },"mappings": {
  97. "books":{
  98. "properties": {
  99. "title":{"type":"text"},
  100. "name":{"type":"text","index":false},
  101. "price":{"type":"double"},
  102. "number":{"type":"object",
  103. "dynamic":true}
  104. }
  105. }
  106. }
  107. }
  108. GET test4/_mapping
  109. PUT test4/books/1
  110. {
  111. "name":"xiaou",
  112. "number":{"1":1,"2":3},
  113. "price":56.55,
  114. "title":"this java"
  115. }
  116. GET /test4/books/1
  117. # 数据准备
  118. PUT /lib3
  119. {
  120. "settings":{
  121. "number_of_shards" : 3,
  122. "number_of_replicas" : 0
  123. },
  124. "mappings":{
  125. "user":{
  126. "properties":{
  127. "name": {"type":"text",
  128. "analyzer": "ik_max_word"},
  129. "address": {"type":"text",
  130. "analyzer": "ik_max_word"},
  131. "age": {"type":"integer"},
  132. "desc": {"type":"text",
  133. "analyzer": "ik_max_word"},
  134. "birthday": {"type":"date"}
  135. }
  136. }
  137. }
  138. }
  139. PUT /lib3/user/1
  140. {"name":"小美",
  141. "address":"浙江桐乡",
  142. "age":18,
  143. "desc":"喜欢唱歌,吃饭,睡觉",
  144. "birthday":"1999-03-05"}
  145. PUT /lib3/user/2
  146. {"name":"小u",
  147. "address":"浙江杭州",
  148. "age": 25,
  149. "desc":"喜欢唱歌,睡觉",
  150. "birthday":"1999-03-05"}
  151. PUT /lib3/user/3
  152. {"name":"小啦",
  153. "address":"浙江下沙",
  154. "age":16,
  155. "desc":"喜欢玩游戏,睡觉",
  156. "birthday":"1999-03-05"}
  157. PUT /lib3/user/4
  158. {"name":"小打",
  159. "address":"浙江呜呜",
  160. "age":12,
  161. "desc":"喜欢玩游戏",
  162. "birthday":"2000-03-05"}
  163. # term查询
  164. GET /lib3/user/_search
  165. {
  166. "query": {
  167. "term": {"name":"小美"}
  168. }
  169. }
  170. # terms查询
  171. GET /lib3/user/_search
  172. {
  173. "query": {
  174. "terms": {"desc":["吃饭","游戏"]}
  175. }
  176. }
  177. # 分页
  178. GET /lib3/user/_search
  179. {
  180. "from": 0,
  181. "size": 2,
  182. "query": {
  183. "terms": {"desc":["吃饭","游戏"]}
  184. }
  185. }
  186. # 返回版本号
  187. GET /lib3/user/_search
  188. {
  189. "from": 0,
  190. "size": 2,
  191. "version": true,
  192. "query": {
  193. "terms": {"desc":["吃饭","游戏"]}
  194. }
  195. }
  196. # match查询
  197. GET /lib3/user/_search
  198. {
  199. "query": {
  200. "match": {
  201. "address": "拉你桐乡"
  202. }
  203. }
  204. }
  205. # 查询所有文档
  206. GET /lib3/user/_search
  207. {
  208. "query": {
  209. "match_all": {}
  210. }
  211. }
  212. # 指定多个字段
  213. GET lib3/user/_search
  214. {
  215. "query": {
  216. "multi_match": {
  217. "query": "游戏桐乡",
  218. "fields": ["address","desc"]
  219. }
  220. }
  221. }
  222. # 短语匹配查询
  223. GET lib3/user/_search
  224. {
  225. "query": {
  226. "match_phrase": {
  227. "desc": "玩游戏"
  228. }
  229. }
  230. }
  231. # 指定返回的字段
  232. GET lib3/user/_search
  233. {
  234. "_source": ["address","desc"],
  235. "query": {
  236. "match_phrase": {
  237. "desc": "玩游戏"
  238. }
  239. }
  240. }
  241. # 控制加载的字段
  242. GET /lib3/user/_search
  243. {
  244. "query": {
  245. "match_all": {}
  246. },
  247. "_source": {
  248. "includes": ["name"],
  249. "excludes": ["age"]
  250. }
  251. }
  252. # 通配符
  253. GET /lib3/user/_search
  254. {
  255. "query": {
  256. "match_all": {}
  257. },
  258. "_source": {
  259. "includes": ["n*"],
  260. "excludes": ["a?e"]
  261. }
  262. }
  263. # sort
  264. GET /lib3/user/_search
  265. {
  266. "query": {
  267. "match_all": {}
  268. },
  269. "sort": [
  270. {
  271. "age": {"order": "asc"}
  272. }
  273. ]
  274. }
  275. # 前缀匹配查询
  276. GET /lib3/user/_search
  277. {
  278. "query": {
  279. "match_phrase_prefix": {
  280. "name": "小"
  281. }
  282. }
  283. }
  284. # 范围查询
  285. GET /lib3/user/_search
  286. {
  287. "query": {
  288. "range": {
  289. "birthday": {
  290. "gte": "1999-01-01",
  291. "lte": "2000-01-01"
  292. }
  293. }
  294. }
  295. }
  296. # wildcard查询
  297. GET /lib3/user/_search
  298. {
  299. "query": {
  300. "wildcard": {
  301. "name": {
  302. "value": "*u"
  303. }
  304. }
  305. }
  306. }
  307. # fuzzy实现模糊查询
  308. GET /lib3/user/_search
  309. {
  310. "query": {
  311. "fuzzy": {"name":"u"}
  312. }
  313. }
  314. # 高亮
  315. GET /lib3/user/_search
  316. {
  317. "query": {
  318. "match": {
  319. "desc": "唱歌"
  320. }
  321. },
  322. "highlight": {
  323. "fields": {
  324. "desc": {}
  325. }
  326. }
  327. }
  328. POST /lib4/items/_bulk
  329. {"index": {"_id": 1}}
  330. {"price": 40,"itemID": "ID100123"}
  331. {"index": {"_id": 2}}
  332. {"price": 50,"itemID": "ID100124"}
  333. {"index": {"_id": 3}}
  334. {"price": 25,"itemID": "ID100124"}
  335. {"index": {"_id": 4}}
  336. {"price": 30,"itemID": "ID100125"}
  337. {"index": {"_id": 5}}
  338. {"price": null,"itemID": "ID100127"}
  339. GET lib4/items/_search
  340. {
  341. "post_filter": {
  342. "term": {
  343. "price": "40"
  344. }
  345. }
  346. }
  347. GET lib4/items/_search
  348. {
  349. "post_filter": {
  350. "terms": {
  351. "price":[25,40]
  352. }
  353. }
  354. }
  355. GET lib4/items/_search
  356. {
  357. "post_filter": {
  358. "term": {
  359. "itemID": "id100123"
  360. }
  361. }
  362. }
  363. # 组合过滤查询
  364. GET lib4/items/_search
  365. {
  366. "post_filter": {
  367. "bool": {
  368. "should":[
  369. {"term":{"price":25}},
  370. {"term":{"itemID":"id100123"}}
  371. ],
  372. "must_not":{"term":{"price":30}}
  373. }
  374. }
  375. }
  376. GET lib4/items/_search
  377. {
  378. "post_filter": {
  379. "bool": {
  380. "should":[{"term":{"price":25}}],
  381. "must_not":{"term":{"price":30}}
  382. }
  383. }
  384. }
  385. # 嵌套使用bool
  386. GET lib4/items/_search
  387. {
  388. "post_filter": {
  389. "bool": {
  390. "should":[{"term":{"itemID":"id100123"}},
  391. {"bool":{
  392. "must":[
  393. {"term":{"itemID":"id100124"}},
  394. {"term":{"price":40}}
  395. ]
  396. }
  397. }
  398. ]
  399. }
  400. }
  401. }
  402. # 范围过滤
  403. GET lib4/items/_search
  404. {
  405. "post_filter": {
  406. "range": {
  407. "price": {
  408. "gte": 40,
  409. "lt": 50
  410. }
  411. }
  412. }
  413. }
  414. # 过滤非空
  415. GET lib4/items/_search
  416. {
  417. "query": {
  418. "bool": {
  419. "filter": {
  420. "exists": {
  421. "field": "price"
  422. }
  423. }
  424. }
  425. }
  426. }
  427. # sum
  428. GET lib4/items/_search
  429. {
  430. "size": 0,
  431. "aggs": {
  432. "price_sum": {
  433. "sum": {
  434. "field": "price"
  435. }
  436. }
  437. }
  438. }
  439. # avg
  440. GET lib4/items/_search
  441. {
  442. "size": 0,
  443. "aggs": {
  444. "price_avg": {
  445. "avg": {
  446. "field": "price"
  447. }
  448. }
  449. }
  450. }
  451. #max
  452. GET lib4/items/_search
  453. {
  454. "size": 0,
  455. "aggs": {
  456. "price_max": {
  457. "max": {
  458. "field": "price"
  459. }
  460. }
  461. }
  462. }
  463. #min
  464. GET lib4/items/_search
  465. {
  466. "size": 0,
  467. "aggs": {
  468. "price_min": {
  469. "min": {
  470. "field": "price"
  471. }
  472. }
  473. }
  474. }
  475. # cardinality:求基数
  476. GET lib4/items/_search
  477. {
  478. "size": 0,
  479. "aggs": {
  480. "price_card": {
  481. "cardinality": {
  482. "field": "price"
  483. }
  484. }
  485. }
  486. }
  487. # 分组
  488. GET lib4/items/_search
  489. {
  490. "size": 0,
  491. "aggs":{
  492. "price_group":{
  493. "terms": {
  494. "field": "price"
  495. }
  496. }
  497. }
  498. }
  499. #
  500. GET lib3/user/_search
  501. {
  502. "query": {
  503. "match": {
  504. "desc": "唱歌"
  505. }
  506. },
  507. "size": 0
  508. , "aggs": {
  509. "avg_group_by": {
  510. "terms": {
  511. "field": "age",
  512. "order": {"avg_of_age": "desc"}
  513. },
  514. "aggs": {
  515. "avg_of_age": {
  516. "avg": {
  517. "field": "age"
  518. }
  519. }
  520. }
  521. }
  522. }
  523. }
  524. # 复合查询
  525. GET lib3/user/_search
  526. {
  527. "query": {
  528. "bool": {
  529. "must": [
  530. {"match": {
  531. "desc": "唱歌"
  532. }}
  533. ],
  534. "must_not": [
  535. {"match": {
  536. "desc": "吃饭"
  537. }}
  538. ],
  539. "should": [
  540. {"match": {
  541. "address": "杭州"
  542. }},
  543. {"range": {
  544. "birthday": {
  545. "gte": "1999-03-05",
  546. "lte": "2000-03-05"
  547. }
  548. }}
  549. ]
  550. }
  551. }
  552. }
  553. GET lib3/user/_search
  554. {
  555. "query": {
  556. "bool": {
  557. "must": [
  558. {"match": {
  559. "desc": "唱歌"
  560. }}
  561. ],
  562. "filter": {
  563. "bool": {
  564. "must":{
  565. "range": {
  566. "birthday": {
  567. "gte": "1999-03-05",
  568. "lt": "2000-03-05"
  569. }
  570. }
  571. }
  572. }
  573. }
  574. }
  575. }
  576. }