当前位置:   article > 正文

干货:23 个示例,吃透ES!

es示例

8d01448b319aaab3b00c679bddba3590.gif

为了说明Elasticsearch的不同查询类型,我们将搜索一个图书文档集合,其中有以下字段:标题、作者、摘要、发布日期和评论数量。但首先,让我们创建一个新的索引,并使用批量API索引一些文档:

  1. PUT /bookdb_index
  2. { "settings": { "number_of_shards": 1 }}
  1. POST /bookdb_index/book/_bulk
  2. { "index": { "_id": 1 }}
  3. { "title": "Elasticsearch: The Definitive Guide", "authors": ["clinton gormley", "zachary tong"], "summary" : "A distibuted real-time search and analytics engine", "publish_date" : "2015-02-07", "num_reviews": 20, "publisher": "oreilly" }
  4. { "index": { "_id": 2 }}
  5. { "title": "Taming Text: How to Find, Organize, and Manipulate It", "authors": ["grant ingersoll", "thomas morton", "drew farris"], "summary" : "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization", "publish_date" : "2013-01-24", "num_reviews": 12, "publisher": "manning" }
  6. { "index": { "_id": 3 }}
  7. { "title": "Elasticsearch in Action", "authors": ["radu gheorge", "matthew lee hinman", "roy russo"], "summary" : "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms", "publish_date" : "2015-12-03", "num_reviews": 18, "publisher": "manning" }
  8. { "index": { "_id": 4 }}
  9. { "title": "Solr in Action", "authors": ["trey grainger", "timothy potter"], "summary" : "Comprehensive guide to implementing a scalable search engine using Apache Solr", "publish_date" : "2014-04-05", "num_reviews": 23, "publisher": "manning" }

实例

基本匹配查询

有两种执行基本全文(匹配)查询的方法:使用 Search Lite API,它希望所有搜索参数都作为 URL 的一部分传入,或者使用完整的 JSON 请求正文,它允许您使用完整的 Elasticsearch DSL

这是一个在所有字段中搜索字符串“guide”的基本匹配查询:

  1. GET /bookdb_index/book/_search?q=guide
  2. [Results]
  3. "hits": [
  4. {
  5. "_index": "bookdb_index",
  6. "_type": "book",
  7. "_id": "4",
  8. "_score": 1.3278645,
  9. "_source": {
  10. "title": "Solr in Action",
  11. "authors": [
  12. "trey grainger",
  13. "timothy potter"
  14. ],
  15. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  16. "publish_date": "2014-04-05",
  17. "num_reviews": 23,
  18. "publisher": "manning"
  19. }
  20. },
  21. {
  22. "_index": "bookdb_index",
  23. "_type": "book",
  24. "_id": "1",
  25. "_score": 1.2871116,
  26. "_source": {
  27. "title": "Elasticsearch: The Definitive Guide",
  28. "authors": [
  29. "clinton gormley",
  30. "zachary tong"
  31. ],
  32. "summary": "A distibuted real-time search and analytics engine",
  33. "publish_date": "2015-02-07",
  34. "num_reviews": 20,
  35. "publisher": "oreilly"
  36. }
  37. }
  38. ]

该查询的完整体版本如下所示,产生的结果与上述搜索精简版相同:

  1. {
  2.     "query": {
  3.         "multi_match" : {
  4.             "query" : "guide",
  5.             "fields" : ["title""authors""summary""publish_date""num_reviews""publisher"]
  6.         }
  7.     }
  8. }

multi_match 关键字用于代替 match 关键字,作为对多个字段运行相同查询的便捷简写方式。 该 fields 属性指定要查询的字段,在这种情况下,我们要查询文档中的所有字段。

注意:在 ElasticSearch 6 之前,您可以使用“ _all”字段在所有字段中查找匹配项,而不必指定每个字段。" _all" 字段的工作原理是将所有字段连接成一个大字段,使用空格作为分隔符,然后对该字段进行分析和索引。在 ES6 中,默认情况下已弃用并禁用此功能。copy_to如果您有兴趣创建自定义“ _all”字段, ES6 提供了“ ”参数。有关详细信息,请参阅ElasticSearch 指南。

SearchLite API 还允许您指定要搜索的字段。例如,要搜索标题字段中带有“in Action”字样的书籍:

  1. GET /bookdb_index/book/_search?q=title:in action
  2. [Results]
  3. "hits": [
  4. {
  5. "_index": "bookdb_index",
  6. "_type": "book",
  7. "_id": "3",
  8. "_score": 1.6323128,
  9. "_source": {
  10. "title": "Elasticsearch in Action",
  11. "authors": [
  12. "radu gheorge",
  13. "matthew lee hinman",
  14. "roy russo"
  15. ],
  16. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  17. "publish_date": "2015-12-03",
  18. "num_reviews": 18,
  19. "publisher": "manning"
  20. }
  21. },
  22. {
  23. "_index": "bookdb_index",
  24. "_type": "book",
  25. "_id": "4",
  26. "_score": 1.6323128,
  27. "_source": {
  28. "title": "Solr in Action",
  29. "authors": [
  30. "trey grainger",
  31. "timothy potter"
  32. ],
  33. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  34. "publish_date": "2014-04-05",
  35. "num_reviews": 23,
  36. "publisher": "manning"
  37. }
  38. }
  39. ]

但是,完整的 DSL 在创建更复杂的查询(我们将在后面看到)和指定如何返回结果方面为您提供了更大的灵活性。在下面的示例中,我们指定了我们想要返回的结果的数量、开始的偏移量(用于分页)、我们想要返回的文档字段以及术语突出显示。请注意,我们使用“ match”查询而不是“ multi_match”查询,因为我们只关心在标题字段中进行搜索。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "match" : {
  5. "title" : "in action"
  6. }
  7. },
  8. "size": 2,
  9. "from": 0,
  10. "_source": [ "title", "summary", "publish_date" ],
  11. "highlight": {
  12. "fields" : {
  13. "title" : {}
  14. }
  15. }
  16. }
  17. [Results]
  18. "hits": {
  19. "total": 2,
  20. "max_score": 1.6323128,
  21. "hits": [
  22. {
  23. "_index": "bookdb_index",
  24. "_type": "book",
  25. "_id": "3",
  26. "_score": 1.6323128,
  27. "_source": {
  28. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  29. "title": "Elasticsearch in Action",
  30. "publish_date": "2015-12-03"
  31. },
  32. "highlight": {
  33. "title": [
  34. "Elasticsearch <em>in</em> <em>Action</em>"
  35. ]
  36. }
  37. },
  38. {
  39. "_index": "bookdb_index",
  40. "_type": "book",
  41. "_id": "4",
  42. "_score": 1.6323128,
  43. "_source": {
  44. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  45. "title": "Solr in Action",
  46. "publish_date": "2014-04-05"
  47. },
  48. "highlight": {
  49. "title": [
  50. "Solr <em>in</em> <em>Action</em>"
  51. ]
  52. }
  53. }
  54. ]

注意:对于多词查询, match 查询允许您指定是否使用 and 运算符而不是默认 or 运算符。您还可以指定 minimum_should_match 选项来调整返回结果的相关性。详细信息可以在 Elasticsearch 指南中找到。

提升

由于我们正在跨多个领域进行搜索,因此我们可能希望提高某个领域的分数。在下面的人为示例中,我们将摘要字段的分数提高了 3 倍,以增加摘要字段的重要性,这反过来又会增加文档的相关性 _id 4

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "multi_match" : {
  5. "query" : "elasticsearch guide",
  6. "fields": ["title", "summary^3"]
  7. }
  8. },
  9. "_source": ["title", "summary", "publish_date"]
  10. }
  11. [Results]
  12. "hits": {
  13. "total": 3,
  14. "max_score": 3.9835935,
  15. "hits": [
  16. {
  17. "_index": "bookdb_index",
  18. "_type": "book",
  19. "_id": "4",
  20. "_score": 3.9835935,
  21. "_source": {
  22. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  23. "title": "Solr in Action",
  24. "publish_date": "2014-04-05"
  25. }
  26. },
  27. {
  28. "_index": "bookdb_index",
  29. "_type": "book",
  30. "_id": "3",
  31. "_score": 3.1001682,
  32. "_source": {
  33. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  34. "title": "Elasticsearch in Action",
  35. "publish_date": "2015-12-03"
  36. }
  37. },
  38. {
  39. "_index": "bookdb_index",
  40. "_type": "book",
  41. "_id": "1",
  42. "_score": 2.0281231,
  43. "_source": {
  44. "summary": "A distibuted real-time search and analytics engine",
  45. "title": "Elasticsearch: The Definitive Guide",
  46. "publish_date": "2015-02-07"
  47. }
  48. }
  49. ]

注意:提升不仅仅意味着计算的分数乘以提升因子。应用的实际提升值经过标准化和一些内部优化。有关提升工作原理的更多信息,请参阅Elasticsearch 指南。

布尔查询

AND/OR/NOT 运算符可用于微调我们的搜索查询,以提供更相关或更具体的结果。这是在搜索 API 中作为 bool 查询实现的。该 bool 查询接受一个 must 参数(相当于 AND)、一个 must_not 参数(相当于 NOT)和一个 should 参数(相当于 OR)。例如,如果我想搜索标题中包含“Elasticsearch”或“Solr”一词的书,并且作者是“clinton gormley”,但不是“radu gheorge”:

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "bool": {
  5. "must": {
  6. "bool" : {
  7. "should": [
  8. { "match": { "title": "Elasticsearch" }},
  9. { "match": { "title": "Solr" }}
  10. ],
  11. "must": { "match": { "authors": "clinton gormely" }}
  12. }
  13. },
  14. "must_not": { "match": {"authors": "radu gheorge" }}
  15. }
  16. }
  17. }
  18. [Results]
  19. "hits": [
  20. {
  21. "_index": "bookdb_index",
  22. "_type": "book",
  23. "_id": "1",
  24. "_score": 2.0749094,
  25. "_source": {
  26. "title": "Elasticsearch: The Definitive Guide",
  27. "authors": [
  28. "clinton gormley",
  29. "zachary tong"
  30. ],
  31. "summary": "A distibuted real-time search and analytics engine",
  32. "publish_date": "2015-02-07",
  33. "num_reviews": 20,
  34. "publisher": "oreilly"
  35. }
  36. }
  37. ]

注意:如您所见,bool 查询可以包装任何其他查询类型,包括其他 bool 查询,以创建任意复杂或深度嵌套的查询。

模糊查询

可以在 Match 和 Multi-Match 查询上启用模糊匹配以捕获拼写错误。模糊程度是根据与原始单词的Levenshtein 距离来指定的,即需要对一个字符串进行单个字符更改以使其与另一个字符串相同的次数。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "multi_match" : {
  5. "query" : "comprihensiv guide",
  6. "fields": ["title", "summary"],
  7. "fuzziness": "AUTO"
  8. }
  9. },
  10. "_source": ["title", "summary", "publish_date"],
  11. "size": 1
  12. }
  13. [Results]
  14. "hits": [
  15. {
  16. "_index": "bookdb_index",
  17. "_type": "book",
  18. "_id": "4",
  19. "_score": 2.4344182,
  20. "_source": {
  21. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  22. "title": "Solr in Action",
  23. "publish_date": "2014-04-05"
  24. }
  25. }
  26. ]

注意: 您可以指定数字 0、1 或 2 而不是指定“AUTO”,以指示可以对字符串进行的最大编辑次数以找到匹配项。使用“AUTO”的好处是它考虑了字符串的长度。对于只有 3 个字符长的字符串,允许模糊度为 2 将导致搜索性能不佳。因此,建议在大多数情况下坚持“自动”。

通配符查询

通配符查询允许您指定要匹配的模式而不是整个术语。 ? 匹配任何字符并 * 匹配零个或多个字符。例如,要查找作者姓名以字母“t”开头的所有记录:

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "wildcard" : {
  5. "authors" : "t*"
  6. }
  7. },
  8. "_source": ["title", "authors"],
  9. "highlight": {
  10. "fields" : {
  11. "authors" : {}
  12. }
  13. }
  14. }
  15. [Results]
  16. "hits": [
  17. {
  18. "_index": "bookdb_index",
  19. "_type": "book",
  20. "_id": "1",
  21. "_score": 1,
  22. "_source": {
  23. "title": "Elasticsearch: The Definitive Guide",
  24. "authors": [
  25. "clinton gormley",
  26. "zachary tong"
  27. ]
  28. },
  29. "highlight": {
  30. "authors": [
  31. "zachary <em>tong</em>"
  32. ]
  33. }
  34. },
  35. {
  36. "_index": "bookdb_index",
  37. "_type": "book",
  38. "_id": "2",
  39. "_score": 1,
  40. "_source": {
  41. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  42. "authors": [
  43. "grant ingersoll",
  44. "thomas morton",
  45. "drew farris"
  46. ]
  47. },
  48. "highlight": {
  49. "authors": [
  50. "<em>thomas</em> morton"
  51. ]
  52. }
  53. },
  54. {
  55. "_index": "bookdb_index",
  56. "_type": "book",
  57. "_id": "4",
  58. "_score": 1,
  59. "_source": {
  60. "title": "Solr in Action",
  61. "authors": [
  62. "trey grainger",
  63. "timothy potter"
  64. ]
  65. },
  66. "highlight": {
  67. "authors": [
  68. "<em>trey</em> grainger",
  69. "<em>timothy</em> potter"
  70. ]
  71. }
  72. }
  73. ]

正则表达式查询

正则表达式查询允许您指定比通配符查询更复杂的模式。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "regexp" : {
  5. "authors" : "t[a-z]*y"
  6. }
  7. },
  8. "_source": ["title", "authors"],
  9. "highlight": {
  10. "fields" : {
  11. "authors" : {}
  12. }
  13. }
  14. }
  15. [Results]
  16. "hits": [
  17. {
  18. "_index": "bookdb_index",
  19. "_type": "book",
  20. "_id": "4",
  21. "_score": 1,
  22. "_source": {
  23. "title": "Solr in Action",
  24. "authors": [
  25. "trey grainger",
  26. "timothy potter"
  27. ]
  28. },
  29. "highlight": {
  30. "authors": [
  31. "<em>trey</em> grainger",
  32. "<em>timothy</em> potter"
  33. ]
  34. }
  35. }
  36. ]

匹配词组查询

匹配短语查询要求查询字符串中的所有术语都存在于文档中,按照查询字符串中指定的顺序并且彼此靠近。默认情况下,这些术语需要彼此完全相邻,但您可以指定一个值,该 slop 值指示在仍将文档视为匹配项的同时允许术语相距多远。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "multi_match" : {
  5. "query": "search engine",
  6. "fields": ["title", "summary"],
  7. "type": "phrase",
  8. "slop": 3
  9. }
  10. },
  11. "_source": [ "title", "summary", "publish_date" ]
  12. }
  13. [Results]
  14. "hits": [
  15. {
  16. "_index": "bookdb_index",
  17. "_type": "book",
  18. "_id": "4",
  19. "_score": 0.22327082,
  20. "_source": {
  21. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  22. "title": "Solr in Action",
  23. "publish_date": "2014-04-05"
  24. }
  25. },
  26. {
  27. "_index": "bookdb_index",
  28. "_type": "book",
  29. "_id": "1",
  30. "_score": 0.16113183,
  31. "_source": {
  32. "summary": "A distibuted real-time search and analytics engine",
  33. "title": "Elasticsearch: The Definitive Guide",
  34. "publish_date": "2015-02-07"
  35. }
  36. }
  37. ]

注意:在上面的例子中,对于一个非短语类型的查询,文档 _id 1 通常会有更高的分数并且出现在文档之前, _id 4 因为它的字段长度更短。然而,作为一个短语查询,术语的接近度被考虑在内,因此文档 _id 4 得分更好。

注意:还要注意,如果 slop 参数减少到 1 个文档 _id 1 将不再出现在结果集中。

匹配短语前缀

匹配短语前缀查询在查询时提供“即用型搜索”或“穷人版本”的自动完成功能,无需以任何方式准备数据。像 match_phrase 查询一样,它接受一个 slop 参数来使词序和相对位置不那么严格。它还接受 max_expansions 参数来限制匹配的术语数量,以降低资源强度。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "match_phrase_prefix" : {
  5. "summary": {
  6. "query": "search en",
  7. "slop": 3,
  8. "max_expansions": 10
  9. }
  10. }
  11. },
  12. "_source": [ "title", "summary", "publish_date" ]
  13. }
  14. [Results]
  15. "hits": [
  16. {
  17. "_index": "bookdb_index",
  18. "_type": "book",
  19. "_id": "4",
  20. "_score": 0.5161346,
  21. "_source": {
  22. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  23. "title": "Solr in Action",
  24. "publish_date": "2014-04-05"
  25. }
  26. },
  27. {
  28. "_index": "bookdb_index",
  29. "_type": "book",
  30. "_id": "1",
  31. "_score": 0.37248808,
  32. "_source": {
  33. "summary": "A distibuted real-time search and analytics engine",
  34. "title": "Elasticsearch: The Definitive Guide",
  35. "publish_date": "2015-02-07"
  36. }
  37. }
  38. ]

注意:查询时搜索即键入会产生性能成本。更好的解决方案是索引时搜索,即键入。查看Completion Suggester API或使用Edge-Ngram 过滤器了解更多信息。

请求参数

该 query_string 查询提供了一种以简洁的速记语法执行 multi_match 查询、布尔查询、提升、模糊匹配、通配符、正则表达式和范围查询的方法。在下面的示例中,我们对术语“搜索算法”执行模糊搜索,其中书籍作者之一是“grant ingersoll”或“tom morton”。我们搜索所有字段,但对汇总字段应用 2 的提升。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "query_string" : {
  5. "query": "(saerch~1 algorithm~1) AND (grant ingersoll) OR (tom morton)",
  6. "fields": ["title", "authors" , "summary^2"]
  7. }
  8. },
  9. "_source": [ "title", "summary", "authors" ],
  10. "highlight": {
  11. "fields" : {
  12. "summary" : {}
  13. }
  14. }
  15. }
  16. [Results]
  17. "hits": [
  18. {
  19. "_index": "bookdb_index",
  20. "_type": "book",
  21. "_id": "2",
  22. "_score": 3.571021,
  23. "_source": {
  24. "summary": "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization",
  25. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  26. "authors": [
  27. "grant ingersoll",
  28. "thomas morton",
  29. "drew farris"
  30. ]
  31. },
  32. "highlight": {
  33. "summary": [
  34. "organize text using approaches such as full-text <em>search</em>, proper name recognition, clustering, tagging"
  35. ]
  36. }
  37. }
  38. ]

简单查询字符串

 查询是更适合在向用户公开的单个搜索框中使用的查询版本,因为它将 AND/OR/NOT 的使用分别替换为 +/|/-,并丢弃了无效 simple_query_string 部分 query_string如果用户出错,则查询而不是抛出异常。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "simple_query_string" : {
  5. "query": "(saerch~1 algorithm~1) + (grant ingersoll) | (tom morton)",
  6. "fields": ["title", "authors" , "summary^2"]
  7. }
  8. },
  9. "_source": [ "title", "summary", "authors" ],
  10. "highlight": {
  11. "fields" : {
  12. "summary" : {}
  13. }
  14. }
  15. }

名词/术语查询

上面的例子都是全文搜索的例子。有时我们对结构化搜索更感兴趣,我们希望在其中找到完全匹配并返回结果。term 和 查询在 terms 这里帮助我们。在下面的示例中,我们正在搜索由 Manning Publications 出版的索引中的所有书籍。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "term" : {
  5. "publisher": "manning"
  6. }
  7. },
  8. "_source" : ["title","publish_date","publisher"]
  9. }
  10. [Results]
  11. "hits": [
  12. {
  13. "_index": "bookdb_index",
  14. "_type": "book",
  15. "_id": "2",
  16. "_score": 1.2231436,
  17. "_source": {
  18. "publisher": "manning",
  19. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  20. "publish_date": "2013-01-24"
  21. }
  22. },
  23. {
  24. "_index": "bookdb_index",
  25. "_type": "book",
  26. "_id": "3",
  27. "_score": 1.2231436,
  28. "_source": {
  29. "publisher": "manning",
  30. "title": "Elasticsearch in Action",
  31. "publish_date": "2015-12-03"
  32. }
  33. },
  34. {
  35. "_index": "bookdb_index",
  36. "_type": "book",
  37. "_id": "4",
  38. "_score": 1.2231436,
  39. "_source": {
  40. "publisher": "manning",
  41. "title": "Solr in Action",
  42. "publish_date": "2014-04-05"
  43. }
  44. }
  45. ]

可以通过使用 terms 关键字并传入搜索词数组来指定多个词。

  1. {
  2. "query": {
  3. "terms" : {
  4. "publisher": ["oreilly", "packt"]
  5. }
  6. }
  7. }

术语查询 - 排序

术语查询结果(与任何其他查询结果一样)可以轻松排序。也允许多级排序。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "term" : {
  5. "publisher": "manning"
  6. }
  7. },
  8. "_source" : ["title","publish_date","publisher"],
  9. "sort": [
  10. { "publish_date": {"order":"desc"}}
  11. ]
  12. }
  13. [Results]
  14. "hits": [
  15. {
  16. "_index": "bookdb_index",
  17. "_type": "book",
  18. "_id": "3",
  19. "_score": null,
  20. "_source": {
  21. "publisher": "manning",
  22. "title": "Elasticsearch in Action",
  23. "publish_date": "2015-12-03"
  24. },
  25. "sort": [
  26. 1449100800000
  27. ]
  28. },
  29. {
  30. "_index": "bookdb_index",
  31. "_type": "book",
  32. "_id": "4",
  33. "_score": null,
  34. "_source": {
  35. "publisher": "manning",
  36. "title": "Solr in Action",
  37. "publish_date": "2014-04-05"
  38. },
  39. "sort": [
  40. 1396656000000
  41. ]
  42. },
  43. {
  44. "_index": "bookdb_index",
  45. "_type": "book",
  46. "_id": "2",
  47. "_score": null,
  48. "_source": {
  49. "publisher": "manning",
  50. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  51. "publish_date": "2013-01-24"
  52. },
  53. "sort": [
  54. 1358985600000
  55. ]
  56. }
  57. ]

注意:在 ES6 中,要按文本字段(例如标题)进行排序或聚合,您需要在该字段上启用 fielddata。有关这方面的更多详细信息,请参阅ElasticSearch 指南。 

范围查询

另一个结构化查询示例是范围查询。在此示例中,我们搜索 2015 年出版的书籍。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "range" : {
  5. "publish_date": {
  6. "gte": "2015-01-01",
  7. "lte": "2015-12-31"
  8. }
  9. }
  10. },
  11. "_source" : ["title","publish_date","publisher"]
  12. }
  13. [Results]
  14. "hits": [
  15. {
  16. "_index": "bookdb_index",
  17. "_type": "book",
  18. "_id": "1",
  19. "_score": 1,
  20. "_source": {
  21. "publisher": "oreilly",
  22. "title": "Elasticsearch: The Definitive Guide",
  23. "publish_date": "2015-02-07"
  24. }
  25. },
  26. {
  27. "_index": "bookdb_index",
  28. "_type": "book",
  29. "_id": "3",
  30. "_score": 1,
  31. "_source": {
  32. "publisher": "manning",
  33. "title": "Elasticsearch in Action",
  34. "publish_date": "2015-12-03"
  35. }
  36. }
  37. ]

注意:范围查询适用于日期、数字和字符串类型字段。

过滤布尔查询

使用 bool 查询时,您可以使用 filter 子句来过滤查询结果。在我们的示例中,我们正在查询标题或摘要中包含“Elasticsearch”一词的书籍,但我们希望将结果过滤为仅包含 20 条或更多评论的书籍。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "query" : {
  6. "multi_match": {
  7. "query": "elasticsearch",
  8. "fields": ["title","summary"]
  9. }
  10. },
  11. "filter": {
  12. "range" : {
  13. "num_reviews": {
  14. "gte": 20
  15. }
  16. }
  17. }
  18. }
  19. },
  20. "_source" : ["title","summary","publisher", "num_reviews"]
  21. }
  22. [Results]
  23. "hits": [
  24. {
  25. "_index": "bookdb_index",
  26. "_type": "book",
  27. "_id": "1",
  28. "_score": 0.5955761,
  29. "_source": {
  30. "summary": "A distibuted real-time search and analytics engine",
  31. "publisher": "oreilly",
  32. "num_reviews": 20,
  33. "title": "Elasticsearch: The Definitive Guide"
  34. }
  35. }
  36. ]

通过使用过滤器可以组合多个过滤 bool 器。在下一个示例中,过滤器确定返回的结果必须至少有 20 条评论,不得在 2015 年之前发布,并且应该由 O'Reilly 发布。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "filtered": {
  5. "query" : {
  6. "multi_match": {
  7. "query": "elasticsearch",
  8. "fields": ["title","summary"]
  9. }
  10. },
  11. "filter": {
  12. "bool": {
  13. "must": {
  14. "range" : { "num_reviews": { "gte": 20 } }
  15. },
  16. "must_not": {
  17. "range" : { "publish_date": { "lte": "2014-12-31" } }
  18. },
  19. "should": {
  20. "term": { "publisher": "oreilly" }
  21. }
  22. }
  23. }
  24. }
  25. },
  26. "_source" : ["title","summary","publisher", "num_reviews", "publish_date"]
  27. }
  28. [Results]
  29. "hits": [
  30. {
  31. "_index": "bookdb_index",
  32. "_type": "book",
  33. "_id": "1",
  34. "_score": 0.5955761,
  35. "_source": {
  36. "summary": "A distibuted real-time search and analytics engine",
  37. "publisher": "oreilly",
  38. "num_reviews": 20,
  39. "title": "Elasticsearch: The Definitive Guide",
  40. "publish_date": "2015-02-07"
  41. }
  42. }
  43. ]

功能得分:字段值因子

在某些情况下,您可能希望将文档中特定字段的值考虑到相关性分数的计算中。这在您希望根据其受欢迎程度提高文档相关性的情况下很典型。在我们的示例中,我们希望提升更受欢迎的书籍(根据评论数量来判断)。这可以使用 field_value_factor 函数 score。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match" : {
  7. "query" : "search engine",
  8. "fields": ["title", "summary"]
  9. }
  10. },
  11. "field_value_factor": {
  12. "field" : "num_reviews",
  13. "modifier": "log1p",
  14. "factor" : 2
  15. }
  16. }
  17. },
  18. "_source": ["title", "summary", "publish_date", "num_reviews"]
  19. }
  20. [Results]
  21. "hits": [
  22. {
  23. "_index": "bookdb_index",
  24. "_type": "book",
  25. "_id": "1",
  26. "_score": 0.44831306,
  27. "_source": {
  28. "summary": "A distibuted real-time search and analytics engine",
  29. "num_reviews": 20,
  30. "title": "Elasticsearch: The Definitive Guide",
  31. "publish_date": "2015-02-07"
  32. }
  33. },
  34. {
  35. "_index": "bookdb_index",
  36. "_type": "book",
  37. "_id": "4",
  38. "_score": 0.3718407,
  39. "_source": {
  40. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  41. "num_reviews": 23,
  42. "title": "Solr in Action",
  43. "publish_date": "2014-04-05"
  44. }
  45. },
  46. {
  47. "_index": "bookdb_index",
  48. "_type": "book",
  49. "_id": "3",
  50. "_score": 0.046479136,
  51. "_source": {
  52. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  53. "num_reviews": 18,
  54. "title": "Elasticsearch in Action",
  55. "publish_date": "2015-12-03"
  56. }
  57. },
  58. {
  59. "_index": "bookdb_index",
  60. "_type": "book",
  61. "_id": "2",
  62. "_score": 0.041432835,
  63. "_source": {
  64. "summary": "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization",
  65. "num_reviews": 12,
  66. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  67. "publish_date": "2013-01-24"
  68. }
  69. }
  70. ]

注意 1:我们可以只运行常规 multi_match 查询并按 num_reviews 字段排序,但随后我们失去了相关性评分的好处。

注意 2:还有许多附加参数可以调整对原始相关性分数的提升效果,例如“修饰符”、“因子”、“提升模式”等。这些在Elasticsearch 指南中进行了详细探讨。

函数得分:衰减函数

假设您不想通过字段的值递增地提升,而是有一个想要定位的理想值,并且您希望提升因子随着您远离该值而衰减。这通常在基于纬度/经度、价格或日期等数字字段的提升中很有用。在我们设计的示例中,我们正在搜索最好在 2014 年 6 月左右出版的关于“搜索引擎”的书籍。

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match" : {
  7. "query" : "search engine",
  8. "fields": ["title", "summary"]
  9. }
  10. },
  11. "functions": [
  12. {
  13. "exp": {
  14. "publish_date" : {
  15. "origin": "2014-06-15",
  16. "offset": "7d",
  17. "scale" : "30d"
  18. }
  19. }
  20. }
  21. ],
  22. "boost_mode" : "replace"
  23. }
  24. },
  25. "_source": ["title", "summary", "publish_date", "num_reviews"]
  26. }
  27. [Results]
  28. "hits": [
  29. {
  30. "_index": "bookdb_index",
  31. "_type": "book",
  32. "_id": "4",
  33. "_score": 0.27420625,
  34. "_source": {
  35. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  36. "num_reviews": 23,
  37. "title": "Solr in Action",
  38. "publish_date": "2014-04-05"
  39. }
  40. },
  41. {
  42. "_index": "bookdb_index",
  43. "_type": "book",
  44. "_id": "1",
  45. "_score": 0.005920768,
  46. "_source": {
  47. "summary": "A distibuted real-time search and analytics engine",
  48. "num_reviews": 20,
  49. "title": "Elasticsearch: The Definitive Guide",
  50. "publish_date": "2015-02-07"
  51. }
  52. },
  53. {
  54. "_index": "bookdb_index",
  55. "_type": "book",
  56. "_id": "2",
  57. "_score": 0.000011564,
  58. "_source": {
  59. "summary": "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization",
  60. "num_reviews": 12,
  61. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  62. "publish_date": "2013-01-24"
  63. }
  64. },
  65. {
  66. "_index": "bookdb_index",
  67. "_type": "book",
  68. "_id": "3",
  69. "_score": 0.0000059171475,
  70. "_source": {
  71. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  72. "num_reviews": 18,
  73. "title": "Elasticsearch in Action",
  74. "publish_date": "2015-12-03"
  75. }
  76. }
  77. ]


功能评分:脚本评分

如果内置评分功能不能满足您的需求,可以选择指定一个 Groovy 脚本用于评分。在我们的示例中,我们希望指定一个脚本, publish_date 在决定将多少因素考虑到评论数量之前将其考虑在内。较新的书籍可能还没有那么多评论,所以他们不应该因此受到惩罚。

评分脚本如下所示:

  1. publish_date = doc['publish_date'].value
  2. num_reviews = doc['num_reviews'].value
  3. if (publish_date > Date.parse('yyyy-MM-dd', threshold).getTime()) {
  4. my_score = Math.log(2.5 + num_reviews)
  5. } else {
  6. my_score = Math.log(1 + num_reviews)
  7. }
  8. return my_score

要动态使用评分脚本,我们使用以下 script_score 参数:

  1. POST /bookdb_index/book/_search
  2. {
  3. "query": {
  4. "function_score": {
  5. "query": {
  6. "multi_match" : {
  7. "query" : "search engine",
  8. "fields": ["title", "summary"]
  9. }
  10. },
  11. "functions": [
  12. {
  13. "script_score": {
  14. "params" : {
  15. "threshold": "2015-07-30"
  16. },
  17. "script": "publish_date = doc['publish_date'].value; num_reviews = doc['num_reviews'].value; if (publish_date > Date.parse('yyyy-MM-dd', threshold).getTime()) { return log(2.5 + num_reviews) }; return log(1 + num_reviews);"
  18. }
  19. }
  20. ]
  21. }
  22. },
  23. "_source": ["title", "summary", "publish_date", "num_reviews"]
  24. }
  25. [Results]
  26. "hits": {
  27. "total": 4,
  28. "max_score": 0.8463001,
  29. "hits": [
  30. {
  31. "_index": "bookdb_index",
  32. "_type": "book",
  33. "_id": "1",
  34. "_score": 0.8463001,
  35. "_source": {
  36. "summary": "A distibuted real-time search and analytics engine",
  37. "num_reviews": 20,
  38. "title": "Elasticsearch: The Definitive Guide",
  39. "publish_date": "2015-02-07"
  40. }
  41. },
  42. {
  43. "_index": "bookdb_index",
  44. "_type": "book",
  45. "_id": "4",
  46. "_score": 0.7067348,
  47. "_source": {
  48. "summary": "Comprehensive guide to implementing a scalable search engine using Apache Solr",
  49. "num_reviews": 23,
  50. "title": "Solr in Action",
  51. "publish_date": "2014-04-05"
  52. }
  53. },
  54. {
  55. "_index": "bookdb_index",
  56. "_type": "book",
  57. "_id": "3",
  58. "_score": 0.08952084,
  59. "_source": {
  60. "summary": "build scalable search applications using Elasticsearch without having to do complex low-level programming or understand advanced data science algorithms",
  61. "num_reviews": 18,
  62. "title": "Elasticsearch in Action",
  63. "publish_date": "2015-12-03"
  64. }
  65. },
  66. {
  67. "_index": "bookdb_index",
  68. "_type": "book",
  69. "_id": "2",
  70. "_score": 0.07602123,
  71. "_source": {
  72. "summary": "organize text using approaches such as full-text search, proper name recognition, clustering, tagging, information extraction, and summarization",
  73. "num_reviews": 12,
  74. "title": "Taming Text: How to Find, Organize, and Manipulate It",
  75. "publish_date": "2013-01-24"
  76. }
  77. }
  78. ]
  79. }

注意 1:要使用动态脚本,必须在 config/elasticsearch.yaml 文件中为您的 Elasticsearch 实例启用它。也可以使用存储在 Elasticsearch 服务器上的脚本。查看Elasticsearch 参考文档了解更多信息。

注意 2: JSON 不能包含嵌入的换行符,因此分号用于分隔语句。

推荐阅读:《深入理解Elasticsearch》


推荐理由:资深软件开发专家、架构师撰写,从设计原理、部署调优、高级特性、扩展开发等方面助你全面进阶。

75149ad2315de4e0544a5bc482637885.gif

更多精彩回顾

  1. 书讯 |8月书讯(上) | 重磅新书来袭!书讯 |8月书讯(下) | 重磅新书来袭!资讯 |《Java核心技术》基于Java 17全面升级!干货 |再见了Java8,Java17:我要取代你干货 | 李三红:Java版本升级需要纳入到可持续性维度
  2. 干货 |市面上的大前端岗位到底是做什么的?

667735e85f1cb0768b37cc7fcd98b66c.gif

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/article/detail/45829
推荐阅读
相关标签
  

闽ICP备14008679号