【无标题】es搜索基本操作

时间:2024-10-30 20:57:15

一,准备数据

1.创建索引

     PUT /lagou-book/

2.创建mapping

  1. PUT /lagou-book/doc/_mapping
  2. {
  3. "properties":{
  4. "description":{
  5. "type":"text",
  6. "analyzer":"ik_max_word"
  7. },
  8. "name":{
  9. "type":"text",
  10. "analyzer":"ik_max_word"
  11. },
  12. "price":{
  13. "type":"float"
  14. },
  15. "timestamp":{
  16. "type":"date",
  17. "format":"yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  18. }
  19. }
  20. }

3.数据准备

  1. PUT /lagou-book/doc/1
  2. {
  3. "name": "lucene",
  4. "description": "Lucene Core is a Java library providing powerful indexing",
  5. "price":100.45,
  6. "timestamp":"2020-08-21 19:11:35"
  7. }
  8. PUT /lagou-book/doc/2
  9. {
  10. "name": "solr",
  11. "description": "Solr is highly scalable providing fully fault tolerant",
  12. "price":320.45,
  13. "timestamp":"2020-07-21 17:11:35"
  14. }
  15. PUT /lagou-book/doc/3
  16. {
  17. "name": "Hadoop",
  18. "description": "The Apache Hadoop solr software library is a framework ",
  19. "price":620.45,
  20. "timestamp":"2020-08-22 19:18:35"
  21. }
  22. PUT /lagou-book/doc/4
  23. {
  24. "name": "ElasticSearch",
  25. "description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力",
  26. "price":999.99,
  27. "timestamp":"2020-08-15 10:11:35"
  28. }

4.查询

     (1)词条搜索

       term 用户查询某个字段包含某些词的文档

     

  1. GET /lagou-book/_search
  2. {
  3. "query": {
  4. "term": {
  5. "name": "solr"
  6. }
  7. }
  8. }

      (2) terms query 用户指定字段包含多个词语的文档

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "description": ["hadoop","elasticsearch"]
  6. }
  7. }
  8. }

      (3)range query 范围搜索

   gte:大于等于;  gt:大于;  lte:小于等于  lt:小于;

   boost:查询权重。boost可以将某个搜索条件的权重加大,此时当这个搜索条件和另一个搜索条件计算score时,搜索条件权重更大的document,score也会更高,当然也就会优先反回来。

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "range": {
  5. "price": {
  6. "gte": 100,
  7. "lte": 500
  8. }
  9. }
  10. }
  11. }
  12. GET /lagou-book/_search
  13. {
  14. "query": {
  15. "range": {
  16. "timestamp": {
  17. "gte": "18/08/2020",
  18. "lte": "01/01/2021",
  19. "format":"dd/MM/yyyy||yyyy"
  20. }
  21. }
  22. }
  23. }

  (4)不为空搜索

    查询指定字段不为空的文档

  1. GET /lagou-book/_search
  2. {
  3. "query": {
  4. "exists":
  5. {
  6. "field":"name"
  7. }
  8. }
  9. }

    (5) ids集合搜索

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "ids": {
  5. "type": "doc",
  6. "values": [1,3]
  7. }
  8. }
  9. }

(6)bool query布尔搜索

       bool查询用bool操作来组合多个字段为一个查询字段,可用的字段:

       must:必须满足

       must:必须满足,但执行的是filter上下文,不参与、不影响评分

       should:或

        must_not:必须不满足     

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": [
  6. {
  7. "match": {
  8. "description": "solr"
  9. }
  10. }
  11. ],
  12. "should": {
  13. "term": {
  14. "name": "hadoop"
  15. }
  16. },
  17. "must_not":{
  18. "range":{
  19. "price":{
  20. "gte":300,
  21. "lte":500
  22. }
  23. }
  24. }
  25. }
  26. }
  27. }

(7)排序

       7.1 相关性评分排序

      默认情况下,查询返回的结果是按照相关性排序的—最相关的文档排在前面。在es中,相关性由_score表示,默认排序是_score降序排。

 

 

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "match": {
  5. "description": "solr"
  6. }
  7. },
  8. "sort": [
  9. {
  10. "_score": {
  11. "order": "asc"
  12. }
  13. }
  14. ]
  15. }

7.2多个字段排序(先按照价格降序排,再按照时间戳生序排)

  1. GET lagou-book/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "sort": [
  7. {
  8. "price": {
  9. "order": "desc"
  10. },
  11. "timestamp":{
  12. "order": "asc"
  13. }
  14. }
  15. ]
  16. }

(8)分页

    size:每页显示多少条

     form:  当前页起始索引, int start = (pageNum - 1) * size

(9)  高亮显示

    

  1. GET /lagou-book/_search
  2. {
  3. "query": {
  4. "match": {
  5. "description": "solr"
  6. }
  7. },
  8. "highlight": {
  9. "pre_tags": "<font color='pink'>",
  10. "post_tags": "</font>",
  11. "fields": [
  12. {
  13. "name": {}
  14. },
  15. {
  16. "description": {}
  17. }
  18. ]
  19. }
  20. }

 pre_tags :前置标签;

post_tags:后置标签

fields:需要高亮的字段

(10)filter