• Elasticsearch(简称ES)简易介绍


    一、简单了解 Elasticsearch

    Elasticsearch(简称ES)是一个开源的分布式搜索引擎,在实时数据索引、搜索和分析方面有着优秀的性能和功能。

    一、原理介绍

    1. 倒排索引
      倒排索引(Inverted Index)是ES最重要的原理之一,它将每个文档中的每个词(term)和出现的位置记录下来,然后构建一个反向索引,词作为关键词,而文档则作为关联文档的列表。以此方式保存数据,使得当我们要搜索文档中的某个关键词时,可以非常快速地找到相关的文档。倒排索引对于全文检索有着非常重要的意义。

    2. Lucene
      ES底层使用了强大的全文搜索引擎——Lucene。Lucene是一个高性能的全文搜索引擎库,并提供了包括分析、索引和搜索等功能。ES在Lucene的基础上加入了分布式搜索和分析功能,使得其能够处理PB级别的数据。

    3. 分布式架构
      ES是一个分布式系统,通过分片和副本机制,可以使得数据在多个节点间自动平衡,并实现高可用和高性能。

    二、是否还有其他检索引擎?

    目前市面上还有一些比如:Apache Solr、Amazon CloudSearch、Sphinx和Microsoft Azure Search  检索引擎!

    作者带领大家了解一下这些检索引擎从不同的角度探讨它们的特点和适用场景,以帮助您选择适合您的搜索引擎解决方案。

    1、先简单了解一下这四个检索引擎

    1. Apache Solr:
      Apache Solr是基于Apache Lucene构建的企业级搜索平台,提供全文搜索、分布式搜索、多语言支持、复杂查询和过滤、高亮显示、相关性排名等功能。Solr也是一个开源项目,拥有强大的社区支持。Solr是一个强大的搜索引擎解决方案,它用于建立搜索引擎、采集和索引文档、实现搜索功能等等。

    2. Amazon CloudSearch:
      Amazon CloudSearch是亚马逊提供的托管搜索服务,充分利用了亚马逊规模和弹性基础设施,并提供全文搜索、自定义搜索、多语言支持、自动缩放和高可靠性等功能。Amazon CloudSearch可以让用户在几分钟内进行搜索、自定义搜索体验、通过查询API与现有应用程序集成并支持多种企业用例。

    3. Sphinx:
      Sphinx是一个快速、高效的全文搜索引擎,适用于从各种数据源(如数据库、文本文件)中提供快速和准确的搜索。它支持全文搜索、实时索引、分布式搜索、多种查询语法、可扩展性和灵活的结果排序。Sphinx是一个使用C++编写的高性能的搜索引擎,具有较大的处理速度和较小的内存消耗。

    4. Microsoft Azure Search:
      Microsoft Azure Search是微软提供的托管搜索服务,可轻松地将搜索功能添加到应用程序中。它提供全文搜索、过滤、排序、分页、查询语法、自动缩放、多语言支持等功能,与Azure生态系统集成紧密。Microsoft Azure Search可以轻松地实现从各种数据源提供搜索功能的要求,并且可与Microsft其他服务(如Azure Cosmos DB,Azure SQL Database)的应用程序集成。

    2、从不同的角度探讨它们的特点和适用场景

    1. 架构和可扩展性比较
      Elasticsearch将数据分片和复制到多个节点上,实现了分布式的存储和处理。它具有简单而灵活的扩展能力,可以轻松地增加或减少节点,提高或降低系统的性能。同样地,Solr也采用了分布式架构,并可以水平扩展。而CloudSearch、Sphinx和Azure Search则是托管服务,并提供自动缩放和可靠性保证。

    2. 查询和分析功能比较
      Elasticsearch具有强大的查询和分析功能,支持全文搜索、模糊查询、多字段查询、范围查询、过滤条件查询等多种查询方式,还提供聚合、排序、高亮显示等功能。Solr也提供类似的功能,支持复杂的查询和过滤,具有丰富的插件生态系统。CloudSearch、Sphinx和Azure Search的查询和分析功能较少,更适合简单的搜索需求。

    3. 社区和生态系统比较
      Elasticsearch和Solr都是开源项目,有着活跃的开源社区和丰富的插件和工具支持。它们有广泛的使用案例和文档资料可供参考。CloudSearch是亚马逊提供的托管服务,依托于亚马逊的基础设施和生态系统。Sphinx和Azure Search的社区和插件生态系统相对较小。

    4. 部署和管理比较
      Elasticsearch和Solr都需要自行管理部署和维护。它们提供了丰富的配置选项和监控工具,但需要花费一定的时间和精力来管理和优化。CloudSearch、Sphinx和Azure Search则是托管服务,无需担心底层基础设施,可以专注于应用程序的开发和功能实现。

    5. 数据源和集成比较
      Elasticsearch和Solr可以从多种数据源中提取数据,包括数据库、文件系统和API。它们具有广泛的集成和插件支持,可以与各种外部系统无缝集成。CloudSearch、Sphinx和Azure Search更倾向于特定的数据源和集成方案。

    结论:根据不同的需求和场景,选择适合自己的搜索引擎解决方案至关重要。如果需要灵活性和应对复杂查询和分析需求,Elasticsearch和Solr是首选。对于快速部署和无需管理基础设施,CloudSearch、Sphinx和Azure Search提供了更便利的托管服务。在选择之前,应综合考虑架构、功能、生态系统、管理和集成等因素,以提供满足您的搜索和分析需求的最佳解决方案

    3、单独对ES和solr做一个独家采访

    Elasticsearch:

    优点:

    1. 简单的分布式集群管理:Elasticsearch提供了简单易用的集群管理工具,可以轻松地扩展和管理分布式环境。
    2. 强大的分析和聚合功能:Elasticsearch具有丰富的聚合功能,可以进行复杂的数据分析和统计操作。
    3. 高级的搜索能力:通过使用Elasticsearch的各种查询语法和过滤条件,可以实现高级的全文搜索和相关性排名。
    4. 数据复制和冗余:Elasticsearch支持数据的自动复制和冗余,可以提供高可用性并保护数据免受硬件故障或数据中心故障的影响。
    5. 大型社区和广泛接受度:Elasticsearch拥有庞大的开源社区,并且得到了广泛的采用和认可。

    缺点:

    1. 内存消耗较高:由于Elasticsearch需要将大量数据加载到内存中以提供快速的搜索和查询,因此它对内存的消耗较高。
    2. 索引更新的延迟:当进行数据更新时,Elasticsearch的索引更新可能会有一定的延迟,因此在需要实时更新的场景下可能不太适合。

    Solr:

    优点:

    1. 成熟的全文搜索功能:Solr基于Lucene构建,提供了完善的全文搜索和查询功能,支持各种搜索选项和高级特性。
    2. 易于自定义和扩展:Solr提供了灵活的配置选项和插件机制,可以自定义和扩展搜索功能。
    3. 大型社区和广泛接受度:Solr拥有庞大的开源社区,并且已经被广泛采用和使用。

    缺点:

    1. 部署和配置相对复杂:相对于Elasticsearch而言,Solr的部署和配置需要一定的技术知识和经验。
    2. 相对较少的实时特性:Solr的实时搜索和索引更新相对Elasticsearch来说可能略显不足,因此在某些实时数据处理场景下可能不太适合。

    三、简单认识认识 Elasticsearch 分词器 Analyzer

           作者这里只是简单举例介绍一下,具体详细说明,请大家查看ES官网文档

    1. Standard Analyzer(标准分析器):

      • 优点:适用于一般的文本处理和搜索场景,能够进行词项切分和转换为小写形式。
      • 缺点:不适用于特定语言的处理,未考虑语义和上下文。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "standard",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "the",
        11. "start_offset" : 0,
        12. "end_offset" : 3,
        13. "type" : "",
        14. "position" : 0
        15. },
        16. {
        17. "token" : "quick",
        18. "start_offset" : 4,
        19. "end_offset" : 9,
        20. "type" : "",
        21. "position" : 1
        22. },
        23. {
        24. "token" : "brown",
        25. "start_offset" : 10,
        26. "end_offset" : 15,
        27. "type" : "",
        28. "position" : 2
        29. },
        30. {
        31. "token" : "fox",
        32. "start_offset" : 16,
        33. "end_offset" : 19,
        34. "type" : "",
        35. "position" : 3
        36. },
        37. {
        38. "token" : "jumps",
        39. "start_offset" : 20,
        40. "end_offset" : 25,
        41. "type" : "",
        42. "position" : 4
        43. },
        44. {
        45. "token" : "over",
        46. "start_offset" : 26,
        47. "end_offset" : 30,
        48. "type" : "",
        49. "position" : 5
        50. },
        51. {
        52. "token" : "the",
        53. "start_offset" : 31,
        54. "end_offset" : 34,
        55. "type" : "",
        56. "position" : 6
        57. },
        58. {
        59. "token" : "lazy",
        60. "start_offset" : 35,
        61. "end_offset" : 39,
        62. "type" : "",
        63. "position" : 7
        64. },
        65. {
        66. "token" : "dog",
        67. "start_offset" : 40,
        68. "end_offset" : 43,
        69. "type" : "",
        70. "position" : 8
        71. }
        72. ]
        73. }
    2. Simple Analyzer(简单分析器):

      • 优点:简单快速,将输入文本按照非字母字符进行切分。
      • 缺点:不执行其他过滤或处理操作。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "simple",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "the",
        11. "start_offset" : 0,
        12. "end_offset" : 3,
        13. "type" : "word",
        14. "position" : 0
        15. },
        16. {
        17. "token" : "quick",
        18. "start_offset" : 4,
        19. "end_offset" : 9,
        20. "type" : "word",
        21. "position" : 1
        22. },
        23. {
        24. "token" : "brown",
        25. "start_offset" : 10,
        26. "end_offset" : 15,
        27. "type" : "word",
        28. "position" : 2
        29. },
        30. {
        31. "token" : "fox",
        32. "start_offset" : 16,
        33. "end_offset" : 19,
        34. "type" : "word",
        35. "position" : 3
        36. },
        37. {
        38. "token" : "jumps",
        39. "start_offset" : 20,
        40. "end_offset" : 25,
        41. "type" : "word",
        42. "position" : 4
        43. },
        44. {
        45. "token" : "over",
        46. "start_offset" : 26,
        47. "end_offset" : 30,
        48. "type" : "word",
        49. "position" : 5
        50. },
        51. {
        52. "token" : "the",
        53. "start_offset" : 31,
        54. "end_offset" : 34,
        55. "type" : "word",
        56. "position" : 6
        57. },
        58. {
        59. "token" : "lazy",
        60. "start_offset" : 35,
        61. "end_offset" : 39,
        62. "type" : "word",
        63. "position" : 7
        64. },
        65. {
        66. "token" : "dog",
        67. "start_offset" : 40,
        68. "end_offset" : 43,
        69. "type" : "word",
        70. "position" : 8
        71. }
        72. ]
        73. }
    3. Whitespace Analyzer(空格分析器):

      • 优点:简单、快速,按照空格字符进行切分。
      • 缺点:不进行其他处理操作,不适用于所有场景。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "whitespace",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "The",
        11. "start_offset" : 0,
        12. "end_offset" : 3,
        13. "type" : "word",
        14. "position" : 0
        15. },
        16. {
        17. "token" : "quick",
        18. "start_offset" : 4,
        19. "end_offset" : 9,
        20. "type" : "word",
        21. "position" : 1
        22. },
        23. {
        24. "token" : "brown",
        25. "start_offset" : 10,
        26. "end_offset" : 15,
        27. "type" : "word",
        28. "position" : 2
        29. },
        30. {
        31. "token" : "fox",
        32. "start_offset" : 16,
        33. "end_offset" : 19,
        34. "type" : "word",
        35. "position" : 3
        36. },
        37. {
        38. "token" : "jumps",
        39. "start_offset" : 20,
        40. "end_offset" : 25,
        41. "type" : "word",
        42. "position" : 4
        43. },
        44. {
        45. "token" : "over",
        46. "start_offset" : 26,
        47. "end_offset" : 30,
        48. "type" : "word",
        49. "position" : 5
        50. },
        51. {
        52. "token" : "the",
        53. "start_offset" : 31,
        54. "end_offset" : 34,
        55. "type" : "word",
        56. "position" : 6
        57. },
        58. {
        59. "token" : "lazy",
        60. "start_offset" : 35,
        61. "end_offset" : 39,
        62. "type" : "word",
        63. "position" : 7
        64. },
        65. {
        66. "token" : "dog.",
        67. "start_offset" : 40,
        68. "end_offset" : 44,
        69. "type" : "word",
        70. "position" : 8
        71. }
        72. ]
        73. }
    4. Stop Analyzer(停用词分析器):

      • 优点:基于标准分析器,在切分基础上删除指定的停用词,适用于忽略常用词汇的搜索。
      • 缺点:某些情况下,可能会过滤掉具有特定意义的词汇。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "stop",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "quick",
        11. "start_offset" : 4,
        12. "end_offset" : 9,
        13. "type" : "word",
        14. "position" : 1
        15. },
        16. {
        17. "token" : "brown",
        18. "start_offset" : 10,
        19. "end_offset" : 15,
        20. "type" : "word",
        21. "position" : 2
        22. },
        23. {
        24. "token" : "fox",
        25. "start_offset" : 16,
        26. "end_offset" : 19,
        27. "type" : "word",
        28. "position" : 3
        29. },
        30. {
        31. "token" : "jumps",
        32. "start_offset" : 20,
        33. "end_offset" : 25,
        34. "type" : "word",
        35. "position" : 4
        36. },
        37. {
        38. "token" : "over",
        39. "start_offset" : 26,
        40. "end_offset" : 30,
        41. "type" : "word",
        42. "position" : 5
        43. },
        44. {
        45. "token" : "lazy",
        46. "start_offset" : 35,
        47. "end_offset" : 39,
        48. "type" : "word",
        49. "position" : 7
        50. },
        51. {
        52. "token" : "dog",
        53. "start_offset" : 40,
        54. "end_offset" : 43,
        55. "type" : "word",
        56. "position" : 8
        57. }
        58. ]
        59. }
    5. Keyword Analyzer(关键词分析器):

      • 优点:将整个输入作为单独的关键词进行处理,适用于需要完整保留输入内容的场景。
      • 缺点:不进行切分或其他处理,无法进行词项搜索。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "keyword",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "The quick brown fox jumps over the lazy dog.",
        11. "start_offset" : 0,
        12. "end_offset" : 44,
        13. "type" : "word",
        14. "position" : 0
        15. }
        16. ]
        17. }
    6. Pattern Analyzer(模式分析器):

      • 优点:可以根据用户指定的正则表达式对输入文本进行切分和处理。
      • 缺点:需要了解和编写正则表达式,适用于特定模式的文本处理。
      • 示例:
        1. GET /pdf_data/_analyze
        2. {
        3. "analyzer": "pattern",
        4. "text": "The quick brown fox jumps over the lazy dog.",
        5. "tokenizer": {
        6. "pattern": "\\W+" // 使用非字母或数字字符进行切分
        7. }
        8. }
        9. -- 分词结果
        10. {
        11. "tokens" : [
        12. {
        13. "token" : "the",
        14. "start_offset" : 0,
        15. "end_offset" : 3,
        16. "type" : "word",
        17. "position" : 0
        18. },
        19. {
        20. "token" : "quick",
        21. "start_offset" : 4,
        22. "end_offset" : 9,
        23. "type" : "word",
        24. "position" : 1
        25. },
        26. {
        27. "token" : "brown",
        28. "start_offset" : 10,
        29. "end_offset" : 15,
        30. "type" : "word",
        31. "position" : 2
        32. },
        33. {
        34. "token" : "fox",
        35. "start_offset" : 16,
        36. "end_offset" : 19,
        37. "type" : "word",
        38. "position" : 3
        39. },
        40. {
        41. "token" : "jumps",
        42. "start_offset" : 20,
        43. "end_offset" : 25,
        44. "type" : "word",
        45. "position" : 4
        46. },
        47. {
        48. "token" : "over",
        49. "start_offset" : 26,
        50. "end_offset" : 30,
        51. "type" : "word",
        52. "position" : 5
        53. },
        54. {
        55. "token" : "the",
        56. "start_offset" : 31,
        57. "end_offset" : 34,
        58. "type" : "word",
        59. "position" : 6
        60. },
        61. {
        62. "token" : "lazy",
        63. "start_offset" : 35,
        64. "end_offset" : 39,
        65. "type" : "word",
        66. "position" : 7
        67. },
        68. {
        69. "token" : "dog",
        70. "start_offset" : 40,
        71. "end_offset" : 43,
        72. "type" : "word",
        73. "position" : 8
        74. }
        75. ]
        76. }
    7. Language Analyzers(语言分析器):

      • 优点:根据特定语言的语法和习惯进行优化,提供更好的文本处理和搜索效果。
      • 缺点:仅适用于对应语言的处理,不够通用。
      • 示例:
        1. GET /your_index/_analyze
        2. {
        3. "analyzer": "english",
        4. "text": "The quick brown fox jumps over the lazy dog."
        5. }
        6. -- 分词结果
        7. {
        8. "tokens" : [
        9. {
        10. "token" : "quick",
        11. "start_offset" : 4,
        12. "end_offset" : 9,
        13. "type" : "",
        14. "position" : 1
        15. },
        16. {
        17. "token" : "brown",
        18. "start_offset" : 10,
        19. "end_offset" : 15,
        20. "type" : "",
        21. "position" : 2
        22. },
        23. {
        24. "token" : "fox",
        25. "start_offset" : 16,
        26. "end_offset" : 19,
        27. "type" : "",
        28. "position" : 3
        29. },
        30. {
        31. "token" : "jump",
        32. "start_offset" : 20,
        33. "end_offset" : 25,
        34. "type" : "",
        35. "position" : 4
        36. },
        37. {
        38. "token" : "over",
        39. "start_offset" : 26,
        40. "end_offset" : 30,
        41. "type" : "",
        42. "position" : 5
        43. },
        44. {
        45. "token" : "lazi",
        46. "start_offset" : 35,
        47. "end_offset" : 39,
        48. "type" : "",
        49. "position" : 7
        50. },
        51. {
        52. "token" : "dog",
        53. "start_offset" : 40,
        54. "end_offset" : 43,
        55. "type" : "",
        56. "position" : 8
        57. }
        58. ]
        59. }
    8. Edge N-gram Analyzer(前缀分析器):

      • 优点:生成输入文本的所有前缀,适用于前缀匹配和自动完成等场景。
      • 缺点:会产生较大的词项量,占用较多的存储空间。
      • 示例:
        1. # 创建一个索引 指定分词器
        2. PUT my_index
        3. {
        4. "settings": {
        5. "analysis": {
        6. "analyzer": {
        7. "my_analyzer": {
        8. "tokenizer": "my_tokenizer"
        9. }
        10. },
        11. "tokenizer": {
        12. "my_tokenizer": {
        13. "type": "edge_ngram",
        14. "min_gram": 2,
        15. "max_gram": 10,
        16. "token_chars": [
        17. "letter",
        18. "digit"
        19. ]
        20. }
        21. }
        22. }
        23. }
        24. }
        25. # 执行分词
        26. POST my_index/_analyze
        27. {
        28. "analyzer": "my_analyzer",
        29. "text": "2 Quick Foxes."
        30. }
        31. -- 分词结果
        32. {
        33. "tokens" : [
        34. {
        35. "token" : "Qu",
        36. "start_offset" : 2,
        37. "end_offset" : 4,
        38. "type" : "word",
        39. "position" : 0
        40. },
        41. {
        42. "token" : "Qui",
        43. "start_offset" : 2,
        44. "end_offset" : 5,
        45. "type" : "word",
        46. "position" : 1
        47. },
        48. {
        49. "token" : "Quic",
        50. "start_offset" : 2,
        51. "end_offset" : 6,
        52. "type" : "word",
        53. "position" : 2
        54. },
        55. {
        56. "token" : "Quick",
        57. "start_offset" : 2,
        58. "end_offset" : 7,
        59. "type" : "word",
        60. "position" : 3
        61. },
        62. {
        63. "token" : "Fo",
        64. "start_offset" : 8,
        65. "end_offset" : 10,
        66. "type" : "word",
        67. "position" : 4
        68. },
        69. {
        70. "token" : "Fox",
        71. "start_offset" : 8,
        72. "end_offset" : 11,
        73. "type" : "word",
        74. "position" : 5
        75. },
        76. {
        77. "token" : "Foxe",
        78. "start_offset" : 8,
        79. "end_offset" : 12,
        80. "type" : "word",
        81. "position" : 6
        82. },
        83. {
        84. "token" : "Foxes",
        85. "start_offset" : 8,
        86. "end_offset" : 13,
        87. "type" : "word",
        88. "position" : 7
        89. }
        90. ]
        91. }
    9. IK 分词器(IK Analyzer):

      • 优点:
        • 支持智能切分:IK Analyzer 根据中文文本的词汇和语法规则进行切分,可以识别并区分词语中的各个成分,如汉字、字母、数字、符号等,实现了较为准确的细粒度分词。

        • 支持多种切分模式:IK Analyzer 提供了多种切分模式,包括最细粒度切分模式和最大词长切分模式。用户可以根据具体需求选择合适的切分模式,在精度和效率之间做出权衡。

        • 支持自定义词典:IK Analyzer 允许用户通过配置自定义词典来增加或修改已有词汇。这样可以根据实际业务场景,将特定的领域名词、品牌名词等纳入分词器的词库中,提高分词准确性。

        • 支持拼写纠错:IK Analyzer 在切分过程中,可以对输入文本的拼写错误进行纠正,并输出正确的分词结果。这对于提高搜索召回率和纠正用户输入错误非常有帮助。

        • 支持同义词扩展:IK Analyzer 提供了同义词扩展的功能,可以将同义词扩展为多个近义词进行分词,从而提高搜索的召回率。

        • 支持停用词过滤:IK Analyzer 内置了中文常用的停用词词库,可以过滤掉停用词,如常见的介词、连词等,减少干扰词对搜索结果的影响。

        • 容易集成:IK Analyzer 是一个开源的分词器,具有良好的可扩展性和易集成性。它可以与 Elasticsearch 无缝集成,作为其内置的中文分词器使用。

      • 缺点:不适用于其他语言的处理,可能对英文等其他语言的分词效果不如专为该语言设计的分词器。
      • IK分词 不同切分器
        • 细粒度切分模式(ik_smart):这种切分模式是一种比较智能的中文切分模式,它可以根据上下文进行分词,能够处理一些歧义词语。

            1. GET /pdf_data/_analyze
            2. {
            3. "analyzer": "ik_smart",
            4. "text": "我们是共产主义接班人"
            5. }
            6. -- 分词结果
            7. {
            8. "tokens" : [
            9. {
            10. "token" : "我们",
            11. "start_offset" : 0,
            12. "end_offset" : 2,
            13. "type" : "CN_WORD",
            14. "position" : 0
            15. },
            16. {
            17. "token" : "是",
            18. "start_offset" : 2,
            19. "end_offset" : 3,
            20. "type" : "CN_CHAR",
            21. "position" : 1
            22. },
            23. {
            24. "token" : "共产主义",
            25. "start_offset" : 3,
            26. "end_offset" : 7,
            27. "type" : "CN_WORD",
            28. "position" : 2
            29. },
            30. {
            31. "token" : "接班人",
            32. "start_offset" : 7,
            33. "end_offset" : 10,
            34. "type" : "CN_WORD",
            35. "position" : 3
            36. }
            37. ]
            38. }

        • 最细粒度切分模式(ik_max_word):这种切分模式是一种针对文本最细粒度的分词模式,可以将文本中每个可以成词的字都切分出来。

            1. GET /pdf_data/_analyze
            2. {
            3. "analyzer": "ik_max_word",
            4. "text": "我们是共产主义接班人"
            5. }
            6. -- 分词结果
            7. {
            8. "tokens" : [
            9. {
            10. "token" : "我们",
            11. "start_offset" : 0,
            12. "end_offset" : 2,
            13. "type" : "CN_WORD",
            14. "position" : 0
            15. },
            16. {
            17. "token" : "是",
            18. "start_offset" : 2,
            19. "end_offset" : 3,
            20. "type" : "CN_CHAR",
            21. "position" : 1
            22. },
            23. {
            24. "token" : "共产主义",
            25. "start_offset" : 3,
            26. "end_offset" : 7,
            27. "type" : "CN_WORD",
            28. "position" : 2
            29. },
            30. {
            31. "token" : "共产",
            32. "start_offset" : 3,
            33. "end_offset" : 5,
            34. "type" : "CN_WORD",
            35. "position" : 3
            36. },
            37. {
            38. "token" : "主义",
            39. "start_offset" : 5,
            40. "end_offset" : 7,
            41. "type" : "CN_WORD",
            42. "position" : 4
            43. },
            44. {
            45. "token" : "接班人",
            46. "start_offset" : 7,
            47. "end_offset" : 10,
            48. "type" : "CN_WORD",
            49. "position" : 5
            50. },
            51. {
            52. "token" : "接班",
            53. "start_offset" : 7,
            54. "end_offset" : 9,
            55. "type" : "CN_WORD",
            56. "position" : 6
            57. },
            58. {
            59. "token" : "人",
            60. "start_offset" : 9,
            61. "end_offset" : 10,
            62. "type" : "CN_CHAR",
            63. "position" : 7
            64. }
            65. ]
            66. }

      备注:

    1. # 查询指定索引的分词器
    2. GET /pdf_data/_mapping

    四、Elasticsearch 简单查询语法

    作者这边只对ES的一些简易查询,做一些举例,复杂查询将单独推出文章介绍。

    1. # 创建索引,创建一些测试数据
    2. POST /pdf_data/_doc?pretty
    3. {
    4. "id": "3",
    5. "name": "面试题文件1.pdf",
    6. "age": 18,
    7. "type": "file",
    8. "money": 1111,
    9. "createBy": "阿杰",
    10. "createTime": "2022-11-03T10:41:51.851Z",
    11. "attachment": {
    12. "content": "面试官:如何保证消息不被重复消费啊?如何保证消费的时候是幂等的啊?Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么区别,以及适合哪些场景?",
    13. "date": "2022-11-02T10:41:51.851Z",
    14. "language": "en"
    15. }
    16. }
    17. # 无条件查询 查询所有数据
    18. GET pdf_data/_search
    19. {
    20. }
    21. # 简单 单条件查询
    22. GET /pdf_data/_search
    23. {
    24. "query": {
    25. "match": {
    26. "createBy": "阿杰"
    27. }
    28. }
    29. }
    30. # 简单 单条件查询 文档内容检索
    31. GET /pdf_data/_search
    32. {
    33. "query": {
    34. "match": {
    35. "attachment.content": "面试官:如何保证消息不被重复消费啊?如何保证消费的时候是幂等的啊?"
    36. }
    37. }
    38. }
    39. # 多条件查询 and的关系
    40. GET /pdf_data/_search
    41. {
    42. "query": {
    43. "bool": {
    44. "must": [
    45. { "match": { "age": "18" } },
    46. { "match": { "attachment.content": "Kafka、ActiveMQ、RabbitMQ、RocketMQ 都有什么区别,以及适合哪些场景?" } }
    47. ]
    48. }
    49. }
    50. }
    51. # 范围查询
    52. GET /pdf_data/_search
    53. {
    54. "query": {
    55. "range": {
    56. "age": {
    57. "gte": 10,
    58. "lte": 20
    59. }
    60. }
    61. }
    62. }
    63. # 带排序的检索
    64. GET /pdf_data/_search
    65. {
    66. "query": {
    67. "match_all": {}
    68. },
    69. "sort": [
    70. { "money": { "order": "asc" } },
    71. { "age": { "order": "desc" } }
    72. ]
    73. }
    74. # 聚合查询
    75. GET /pdf_data/_search
    76. {
    77. "aggs": {
    78. "group_by_field": {
    79. "terms": {
    80. "field": "age",
    81. "size": 10
    82. }
    83. }
    84. }
    85. }

    制作不易,给个小赞,可好!

  • 相关阅读:
    18.四数之和
    改进粒子速度和位置更新公式的粒子群优化算法
    高效、智能、安全:小型机房EasyCVR+AI视频综合监控解决方案
    春节静态HTML网页作业模板 传统节日文化网站设计作品 静态学生网页设计作业简单网页制作
    面试中常问到的C++11的题目和答案
    Android 全栈的进击之路
    英语四级XYZ字母开头单词+托福高频单词
    写一个基于C语言的保守垃圾回收器
    OpenCV实战(29)——视频对象追踪
    L1-098 再进去几个人 - java
  • 原文地址:https://blog.csdn.net/lijie0213/article/details/134232010