ElasticSearch6.2.4(3)——简单的搜索方式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haozhishang/article/details/80060233
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. }
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/haozhishang/article/details/80060233
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. }

猜你喜欢

转载自blog.csdn.net/zengye78/article/details/84937621
今日推荐