全文检索字段用match, 其他非text字段匹配用term。 不要使用term来进行文本字段查询

match_all query

  1. POST /lagou-company-index/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. }
  6. }

全文搜索(full-text query)

匹配搜索 match query

数据准备

  1. PUT /lagou-property
  2. {
  3. "settings": {},
  4. "mappings": {
  5. "properties": {
  6. "title": {
  7. "type": "text",
  8. "analyzer": "ik_max_word"
  9. },
  10. "images": {
  11. "type": "keyword"
  12. },
  13. "price": {
  14. "type": "float"
  15. }
  16. }
  17. }
  18. }
  19. POST /lagou-property/_doc/
  20. {
  21. "title": "小米电视4A",
  22. "images": "http://image.lagou.com/12479122.jpg",
  23. "price": 4288
  24. }
  25. POST /lagou-property/_doc/
  26. {
  27. "title": "小米手机",
  28. "images": "http://image.lagou.com/12479622.jpg",
  29. "price": 2699
  30. }
  31. POST /lagou-property/_doc/
  32. {
  33. "title": "华为手机",
  34. "images": "http://image.lagou.com/12479922.jpg",
  35. "price": 5699
  36. }
  • or : match默认就是or关系

    1. POST /lagou-property/_search
    2. {
    3. "query": {
    4. "match": {
    5. "title": "小米电视4A"
    6. }
    7. }
    8. }
  • and 分词后,多个词之间是and关系, 小米电视4A

    1. POST /lagou-property/_search
    2. {
    3. "query": {
    4. "match": {
    5. "title": {
    6. "query": "小米电视4A",
    7. "operator": "and"
    8. }
    9. }
    10. }
    11. }

    短语匹配 **match phrase query**

    短语中的空格去掉后,不需要移动作为一个整体就能查询到 ```yaml GET /lagou-property/_search { “query”: { “match_phrase”: {

    1. "title": "小米电视"

    } } }

slop移动因子, 可以指定跨动分词个数

GET /lagou-property/_search { “query”: { “match_phrase”: { “title”: { “query”: “小米 4A”, “slop”: 2 } } } }

  1. <a name="HWzL7"></a>
  2. #### query string
  3. 无需指定某字段而对文档全文进行匹配查询的一个高级查询
  4. ```yaml
  5. # 默认 全字段搜索
  6. GET /lagou-property/_search
  7. {
  8. "query": {
  9. "query_string": {
  10. "query": "2699"
  11. }
  12. }
  13. }
  14. # 指定字段
  15. GET /lagou-property/_search
  16. {
  17. "query": {
  18. "query_string": {
  19. "query": "2699",
  20. "default_field": "price"
  21. }
  22. }
  23. }
  24. # 逻辑查询
  25. GET /lagou-property/_search
  26. {
  27. "query": {
  28. "query_string": {
  29. "query": "手机 or 小米",
  30. "default_field": "title"
  31. }
  32. }
  33. }
  34. # 模糊查询 ~1 表示带有修正的查询
  35. GET /lagou-property/_search
  36. {
  37. "query": {
  38. "query_string": {
  39. "query": "大米~1",
  40. "default_field": "title"
  41. }
  42. }
  43. }
  44. # 多字段支持
  45. GET /lagou-property/_search
  46. {
  47. "query": {
  48. "query_string": {
  49. "query": "2699",
  50. "fields": [
  51. "title",
  52. "price"
  53. ]
  54. }
  55. }
  56. }

多字段匹配搜索 multi_match

  1. GET /lagou-property/_search
  2. {
  3. "query": {
  4. "multi_match": {
  5. "query": "2699",
  6. "fields": ["title","price"]
  7. }
  8. }
  9. }
  10. #可以用通配符来表示字段
  11. GET /lagou-property/_search
  12. {
  13. "query": {
  14. "multi_match": {
  15. "query": "http://image.lagou.com/12479622.jpg",
  16. "fields": [
  17. "title",
  18. "ima*"
  19. ]
  20. }
  21. }
  22. }

词条级搜索 term_level queries

不分析词条,精确级搜索

  1. PUT /book
  2. {
  3. "settings": {},
  4. "mappings": {
  5. "properties": {
  6. "description": {
  7. "type": "text",
  8. "analyzer": "ik_max_word"
  9. },
  10. "name": {
  11. "type": "text",
  12. "analyzer": "ik_max_word"
  13. },
  14. "price": {
  15. "type": "float"
  16. },
  17. "timestamp": {
  18. "type": "date",
  19. "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis"
  20. }
  21. }
  22. }
  23. }
  24. PUT /book/_doc/1
  25. {
  26. "name": "lucene",
  27. "description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
  28. "price": 100.45,
  29. "timestamp": "2020-08-21 19:11:35"
  30. }
  31. PUT /book/_doc/2
  32. {
  33. "name": "solr",
  34. "description": "Lucene Core is a Java library providing powerful indexing and search features, as well as spellchecking, hit highlighting and advanced analysis/tokenization capabilities. The PyLucene sub project provides Python bindings for Lucene Core. ",
  35. "price": 100.45,
  36. "timestamp": "2020-08-21 19:11:35"
  37. }
  38. PUT /book/_doc/3
  39. {
  40. "name": "Hadoop",
  41. "description": "The Apache Hadoop software library is a framework that allows for the distributed processing of large data sets across clusters of computers using simple programming models.",
  42. "price": 620.45,
  43. "timestamp": "2020-08-22 19:18:35"
  44. }
  45. PUT /book/_doc/4
  46. {
  47. "name": "ElasticSearch",
  48. "description": "Elasticsearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力 的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java语言开发的,并作为Apache许可条 款下的开放源码发布,是一种流行的企业级搜索引擎。Elasticsearch用于云计算中,能够达到实时搜 索,稳定,可靠,快速,安装使用方便。官方客户端在Java、.NET(C#)、PHP、Python、Apache Groovy、Ruby和许多其他语言中都是可用的。根据DB-Engines的排名显示,Elasticsearch是最受欢 迎的企业搜索引擎,其次是Apache Solr,也是基于Lucene。",
  49. "price": 999.99,
  50. "timestamp": "2020-08-15 10:11:35"
  51. }

term query

用于查询指定字段包含某个词项的文档,类似于%xxx%

  1. POST /book/_search
  2. {
  3. "query": {
  4. "term": {
  5. "name": "solr"
  6. }
  7. }
  8. }
  9. POST /book/_search
  10. {
  11. "query": {
  12. "term": {
  13. "description": "solr"
  14. }
  15. }
  16. }

terms query

用于查询 指定字段 包含某些词项 ,类似于in %xxx%, %xxx%

  1. GET /book/_search
  2. {
  3. "query": {
  4. "terms": {
  5. "name": [
  6. "solr",
  7. "elasticsearch"
  8. ]
  9. }
  10. }
  11. }

范围搜索 range query

  • gte:大于等于
  • gt:大于
  • lte:小于等于
  • lt:小于
  • boost:查询权重 ```yaml GET /book/_search { “query”: { “range”: {
    1. "price": {
    2. "gte": 10,
    3. "lte": 200,
    4. "boost": 2
    5. }
    } } }

GET /book/_search { “query”: { “range”: { “timestamp”: { “gte”: “now-10d/d”, “lt”: “now/d” } } } }

指定日期格式

GET book/_search { “query”: { “range”: { “timestamp”: { “gte”: “18/08/2020”, “lte”: “2021”, “format”: “dd/MM/yyyy||yyyy” } } } }

  1. <a name="eFWzD"></a>
  2. #### exists query 不为空搜索
  3. 相当于: column is not null
  4. ```yaml
  5. GET /book/_search
  6. {
  7. "query": {
  8. "exists": {
  9. "field": "price"
  10. }
  11. }
  12. }

词项前缀搜索(prefix query)

  1. GET /book/_search
  2. {
  3. "query": {
  4. "prefix": {
  5. "name": "so"
  6. }
  7. }
  8. }

通配符搜索(wildcard query)

  1. GET /book/_search
  2. {
  3. "query": {
  4. "wildcard": {
  5. "name": "so*r"
  6. }
  7. }
  8. }
  9. GET /book/_search
  10. {
  11. "query": {
  12. "wildcard": {
  13. "name": {
  14. "value": "lu*",
  15. "boost": 2
  16. }
  17. }
  18. }
  19. }

模糊搜索(fuzzy query)

  1. GET /book/_search
  2. {
  3. "query": {
  4. "fuzzy": {
  5. "name": "so"
  6. }
  7. }
  8. }
  9. #fuzziness模糊值
  10. GET /book/_search
  11. {
  12. "query": {
  13. "fuzzy": {
  14. "name": {
  15. "value": "so",
  16. "boost": 1,
  17. "fuzziness": 2
  18. }
  19. }
  20. }
  21. }
  22. GET /book/_search
  23. {
  24. "query": {
  25. "fuzzy": {
  26. "name": {
  27. "value": "sorl",
  28. "boost": 1,
  29. "fuzziness": 2
  30. }
  31. }
  32. }
  33. }

ids搜索(id集合查询)

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

复合搜索(compound query)

constant_score query

用来包装一个查询,将查询的文档评分设为常值

  1. GET /book/_search
  2. {
  3. "query": {
  4. "term": {
  5. "description": "solr"
  6. }
  7. }
  8. }
  9. GET /book/_search
  10. {
  11. "query": {
  12. "constant_score": {
  13. "filter": {
  14. "term": {
  15. "description": "solr"
  16. }
  17. },
  18. "boost": 1.0
  19. }
  20. }
  21. }

布尔搜索(bool query)

bool 查询用bool操作来组合多个查询字句为一个查询。 可用的关键字

  • must:必须满足
  • should:或
  • filter:必须满足
  • must_not:必须不满足 ```yaml POST /book/_search { “query”: { “bool”: {
    1. "should": {
    2. "match": {
    3. "description": "java"
    4. }
    5. },
    6. "filter": {
    7. "term": {
    8. "name": "solr"
    9. }
    10. },
    11. "must_not": {
    12. "range": {
    13. "price": {
    14. "gte":300,
    15. "lte":400
    16. }
    17. }
    18. },
    19. "minimum_should_match": 1,
    20. "boost": 1
    } } }
  1. minimum_should_match代表了最小匹配精度,如果设置minimum_should_match=1,那么should 语句中至少需要有一个条件满足。
  2. <a name="jG4xV"></a>
  3. #### 排序
  4. - 元数据分数,按相关性得分升序
  5. ```yaml
  6. POST /book/_search
  7. {
  8. "query": {
  9. "match": {
  10. "description": "solr"
  11. }
  12. },
  13. "sort": [
  14. {
  15. "_score": {
  16. "order": "asc"
  17. }
  18. }
  19. ]
  20. }
  • 按字段排序

    1. POST /book/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "sort": [
    7. {
    8. "price": {
    9. "order": "desc"
    10. }
    11. }
    12. ]
    13. }
  • 多级排序

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

    分页

    1. POST /book/_search
    2. {
    3. "query": {
    4. "match_all": {}
    5. },
    6. "sort": [
    7. {
    8. "price": {
    9. "order": "desc"
    10. }
    11. }
    12. ],
    13. "size": 2,
    14. "from": 2
    15. }

    高亮

    1. POST /book/_search
    2. {
    3. "query": {
    4. "match": {
    5. "name": "solr"
    6. }
    7. },
    8. "highlight": {
    9. "pre_tags": "<font color='pink'>",
    10. "post_tags": "</font>",
    11. "fields": [
    12. {
    13. "name": {}
    14. }
    15. ]
    16. },
    17. "size": 2,
    18. "from": 0
    19. }
    20. # 因为match的 字段是name,所以只能高亮name
    21. POST /book/_search
    22. {
    23. "query": {
    24. "match": {
    25. "name": "elasticsearch"
    26. }
    27. },
    28. "highlight": {
    29. "pre_tags": "<font color='pink'>",
    30. "post_tags": "</font>",
    31. "fields": [
    32. {
    33. "name": {}
    34. },
    35. {
    36. "description": {}
    37. }
    38. ]
    39. }
    40. }
    41. # 匹配所有字段
    42. POST /book/_search
    43. {
    44. "query": {
    45. "query_string": {
    46. "query": "elasticsearch"
    47. }
    48. },
    49. "highlight": {
    50. "pre_tags": "<font color='pink'>",
    51. "post_tags": "</font>",
    52. "fields": [
    53. {
    54. "name": {}
    55. },
    56. {
    57. "description": {}
    58. }
    59. ]
    60. }
    61. }

    文档批量操作

  • mget ```yaml

    mget 不同索引

    GET /_mget { “docs”: [ {

    1. "_index": "book",
    2. "_id": 1

    }, {

    1. "_index": "book",
    2. "_id": 2

    } ] }

mget同一个索引

GET /book/_mget { “docs”: [ { “_id”: 2 }, { “_id”: 3 } ] }

简化写法

POST /book/_search { “query”: { “ids”: { “values”: [ “1”, “4” ] } } }

  1. - bulk
  2. 批量操作增删改
  3. ```yaml
  4. POST /_bulk
  5. {"delete":{"_index":"book","_id":"1"}}
  6. {"create":{"_index":"book","_id":"5"}}
  7. {"name":"test14","price":100.99}
  8. {"update":{"_index":"book","_id":"2"}}
  9. {"doc":{"name":"test"}}
  • delete:删除一个文档,只要1个json串就可以了 删除的批量操作不需要请求体
  • create:相当于强制创建 PUT /index/type/id/_create
  • index:普通的put操作,可以是创建文档,也可以是全量替换文档
  • update:执行的是局部更新partial update操作

    格式:每个json不能换行。相邻json必须换行。 隔离:每个操作互不影响。操作失败的行会返回其失败信息。 一般建议是1000-5000个文档,大小建议是5-15MB,默认不能超过100M,可以在es的配置文件(ES的 confifig下的elasticsearch.yml)中配置。 http.max_content_length: 10mb