1. 程式人生 > >ElasticSearch6.2.4(3)——簡單的搜尋方式

ElasticSearch6.2.4(3)——簡單的搜尋方式

1.準備工作,新增資料

    
  1. PUT /zoo/product/1
  2. {
  3. "name":"monkey",
  4. "age":10,
  5. "content":"xiao small but ke ai"
  6. }
  7. PUT /zoo/product/2
  8. {
  9. "name":"monkey",
  10. "age":13,
  11. "content":"xiao small but very big"
  12. }
  13. PUT /zoo/product/3
  14. {
  15. "name":"dog",
  16. "age":13,
  17. "content":"xiao big but very small"
  18. }
  19. PUT /zoo/product/4
  20. {
  21. "name":"cat",
  22. "age":18,
  23. "content":"xiao big but very small"
  24. }
  25. PUT /zoo/product/5
  26. {
  27. "name":"tig",
  28. "age":30,
  29. "content":"xiao big but very big"
  30. }

2.搜尋zoo下面product的所有document

GET /zoo/product/_search
took:耗費了幾毫秒
timed_out:是否超時,這裡是沒有
_shards:資料拆成了5個分片,所以對於搜尋請求,會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高

hits.hits:包含了匹配搜尋的document的詳細資料

3.查詢所有動物


    
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

4.查詢包含big的內容,並且按照age降序(desc是降序,asc是升序)


    
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "age": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

5.從0下標開始查詢出2包含big的動物


    
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big"
  6. }
  7. },
  8. "size": 2,
  9. "from": 0
  10. }

6.查詢出來的資料只要age和content屬性


    
  1. GET /zoo/product/_search
  2. {
  3. "_source": ["age","content"]
  4. }

7.查詢出包含big but整段短語


    
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "content": "big but"
  6. }
  7. }
  8. }

8.查詢出來的資料進行高亮處理


    
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big but"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "content": {}
  11. }
  12. }
  13. }
1.準備工作,新增資料

  
  1. PUT /zoo/product/1
  2. {
  3. "name":"monkey",
  4. "age":10,
  5. "content":"xiao small but ke ai"
  6. }
  7. PUT /zoo/product/2
  8. {
  9. "name":"monkey",
  10. "age":13,
  11. "content":"xiao small but very big"
  12. }
  13. PUT /zoo/product/3
  14. {
  15. "name":"dog",
  16. "age":13,
  17. "content":"xiao big but very small"
  18. }
  19. PUT /zoo/product/4
  20. {
  21. "name":"cat",
  22. "age":18,
  23. "content":"xiao big but very small"
  24. }
  25. PUT /zoo/product/5
  26. {
  27. "name":"tig",
  28. "age":30,
  29. "content":"xiao big but very big"
  30. }

2.搜尋zoo下面product的所有document

GET /zoo/product/_search
took:耗費了幾毫秒
timed_out:是否超時,這裡是沒有
_shards:資料拆成了5個分片,所以對於搜尋請求,會打到所有的primary shard(或者是它的某個replica shard也可以)
hits.total:查詢結果的數量,3個document
hits.max_score:score的含義,就是document對於一個search的相關度的匹配分數,越相關,就越匹配,分數也高

hits.hits:包含了匹配搜尋的document的詳細資料

3.查詢所有動物


  
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

4.查詢包含big的內容,並且按照age降序(desc是降序,asc是升序)


  
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "age": {
  11. "order": "desc"
  12. }
  13. }
  14. ]
  15. }

5.從0下標開始查詢出2包含big的動物


  
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big"
  6. }
  7. },
  8. "size": 2,
  9. "from": 0
  10. }

6.查詢出來的資料只要age和content屬性


  
  1. GET /zoo/product/_search
  2. {
  3. "_source": ["age","content"]
  4. }

7.查詢出包含big but整段短語


  
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match_phrase": {
  5. "content": "big but"
  6. }
  7. }
  8. }

8.查詢出來的資料進行高亮處理


  
  1. GET /zoo/product/_search
  2. {
  3. "query": {
  4. "match": {
  5. "content": "big but"
  6. }
  7. },
  8. "highlight": {
  9. "fields": {
  10. "content": {}
  11. }
  12. }
  13. }