作者:来自 Elastic Mark_Laney
想要将常规 Elasticsearch 查询与新的 AI 搜索功能结合起来吗?那么,你不需要连接到某个第三方的大型语言模型(LLM)吗?不。你可以使用 Elastic 的 ELSER 模型来改进现有搜索,以创建语义向量,并将其与常规搜索相结合,以获得更相关的搜索结果!
介绍
当谈到使用 AI 并将向量搜索应用于 Elastic 上的数据时,我看到很多教程和文章,它们可能变得相当复杂。我想表明,你可以利用今天正在运行的现有搜索,并通过语义和向量搜索来增强它们。你无需连接任何外部 LLM 或付费给任何大公司来处理你的数据。仅在 Elasticsearch 和 Kibana 中,你就可以下载 Elastic 的语义模型 ELSER,处理你的数据以添加描述向量,并增强你的搜索以探索改进。你不需要 LangChain、Python、chatGPT 或任何其他外部工具。
注:目前 ELSER 只提供对英文的支持。它是一种稀疏向量的搜索方式。更多阅读,请参阅 “Elasticsearch:使用 ELSER 释放语义搜索的力量:Elastic Learned Sparse EncoderR”。
平台
我们将运行的搜索是在 Elasticsearch 和 Kibana 8.13.4 版本上完成的。你可以在任何你喜欢的平台上运行它们,无论是在本地还是在云端。
数据
我们将搜索的数据来自 Kaggle 的一组称为 “recipes” 的开放许可数据集。原始数据集可以从这里下载(在《使用 Elastic 的从业者向量搜索》第 173 页中讨论)。我下载了该文件并将其命名为 “allrecipes.csv”。
或者你可以从我的 GitHub 下载数据集。请打开该链接以在后续说明中下载其他文件。
删除重复数据
原始数据集中存在重复的条目。我编写了一个小型 Python 脚本来对它们进行重复数据删除。该脚本位于 GitHub 项目文件夹中,名为 dedupecsv.py。去重的结果文件叫做 allrecipes_dedupe.csv,它也在项目文件中。如果要自己运行 dedupecsv.py,则需要安装 python 库 pandas。 (例如 pip install pandas)。但是你不必执行这个重复数据删除,因为它已经完成,结果是 allrecipes_dedupe.csv。下载该文件。
索引数据
我能够使用 Filebeat 和(甚至更简单的)Kibana 中的文件上传器将数据导入 Elasticsearch 中的索引,最近使用的是 8.13.4 版本,但旧版本也可以使用。如果你使用 Filebeat 将 allrecipes_dedupe.csv 导入 Elasticsearch,则配置文件为 filebeat.yml ,并且副本位于项目文件夹中。我按照 “快速安装” 文档安装并使用 filebeat 作为 “自托管” 系统。如果你正在使用远程系统(如 Cloud 或 Strigo),请将 filebeat.yml 和 allrecipes_dedupe.csv 都 scp 到你的实例。
但使用文件上传器更容易,只需下载 allrecipes_dedupe.csv 并将其拖放到 Kibana 中。
使用任一方法,创建一个名为 “recipes” 的 Elasticsearch 索引。 Filebeat 将为你完成此操作。你在文件上传器中输入该名称。
Kibana
步骤 1:让我们验证食谱索引是否已成功导入 Elasticsearch。
在 Kibana 控制台(Kibana 主菜单 -> Management -> Dev Tools)中运行此命令:
GET _cat/indices?v&s=index
_cat API 提供行和列的输出。问号 (?v&s=index) 后面的字符是显示标题标签 (v) 和按 “index” 列排序 (&s=index) 的选项。
验证 recipes 索引的另一种方法是使用 _count API。
GET recipes/_count
步骤 2:下载 ELSER
Kibana UI 让我们可以在机器学习区域下载模型。转到主菜单-> Machine Learning。找到并点击 “Trained Models”。你应该会看到一个可用模型的简短列表。对于非英特尔环境,请下载 .elser_model_2。如果你的平台使用的是Intel x86 芯片,请下载专门准备的.elser_model_2_linux-x86_64。
步骤 3:启动模型
开始部署该模型进行推理。使用一个分配和四个线程。大多数情况下,分配会消耗一个核心,而线程就是该核心上的线程。下面的 API 调用为模型提供了一个名称 “elser_model”。
如果你运行的是 Intel x86 芯片:
POST _ml/trained_models/.elser_model_2_linux-x86_64/deployment/_start?deployment_id=elser_model&number_of_allocations=1&threads_per_allocation=4
否则:
POST _ml/trained_models/.elser_model_2/deployment/_start?deployment_id=elser_model&number_of_allocations=1&threads_per_allocation=4
如果需要启动和停止模型,可以执行以下操作:
POST _ml/trained_models/elser_model/deployment/_stop
如果管道正在调用模型(我们稍后会讲到):
POST _ml/trained_models/elser_model/deployment/_stop?force=true
确保模型处于启动状态:
1. GET _ml/trained_models/_stats/
2. GET _ml/trained_models/_stats?filter_path=trained_model_stats.deployment_stats.state
步骤 4:浏览食谱索引
运行这些命令来检查 recipes 索引的内容和属性。
检查索引中的某些文档:
GET recipes/_search
要查看字段及其数据类型:
GET recipes/_mapping
要查看索引的所有属性(映射、设置、别名),实际上更容易:
GET recipes
步骤 5:改进映射
如果我们运行这个:
GET recipes/_search?filter_path=hits.hits._source.id
请注意,所有 ID 都是小整数。然而,如果我们回过头来检查映射,我们会发现 ID 的数据类型可能是文本/关键字或长整型(取决于我们用来索引配方的工具)。
还要注意,summary 字段是数据类型 text,默认使用标准分析器。如果有英语分析器结果(提供词干结果),这将对我们的搜索很有用。这样,如果我们搜索“stewed” 西红柿的 summary,结果就会是 stewed、stew、stews、stewing等,这可能有助于更轻松地获得相关食谱。
此外,我们还需要创建一个字段来保存数据向量化的结果。稍后我们将配置一个摄取节点管道处理器来指示模型将结果写入名为 ml.tokens 的字段。正如这里所解释的,ELSER 模型结果应该存储为我们的向量嵌入的数据类型 “稀疏向量”。
考虑到这些改变,让我们创建一个具有所有改进的映射属性的不存在的索引。
1. PUT recipes_embeddings
2. {
3. "mappings": {
4. "properties": {
5. "id": {
6. "type": "short"
7. },
8. "group": {
9. "type": "text",
10. "fields": {
11. "keyword": {
12. "type": "keyword",
13. "ignore_above": 256
14. }
15. }
16. },
17. "ingredient": {
18. "type": "text",
19. "fields": {
20. "keyword": {
21. "type": "keyword",
22. "ignore_above": 256
23. }
24. }
25. },
26. "n_rater": {
27. "type": "text",
28. "fields": {
29. "keyword": {
30. "type": "keyword",
31. "ignore_above": 256
32. }
33. }
34. },
35. "n_reviewer": {
36. "type": "text",
37. "fields": {
38. "keyword": {
39. "type": "keyword",
40. "ignore_above": 256
41. }
42. }
43. },
44. "name": {
45. "type": "text",
46. "fields": {
47. "keyword": {
48. "type": "keyword",
49. "ignore_above": 256
50. }
51. }
52. },
53. "process": {
54. "type": "text",
55. "fields": {
56. "keyword": {
57. "type": "keyword",
58. "ignore_above": 256
59. }
60. }
61. },
62. "rating": {
63. "type": "text",
64. "fields": {
65. "keyword": {
66. "type": "keyword",
67. "ignore_above": 256
68. }
69. }
70. },
71. "summary": {
72. "type": "text",
73. "analyzer": "english",
74. "fields": {
75. "keyword": {
76. "type": "keyword",
77. "ignore_above": 256
78. }
79. }
80. },
81. "ml.tokens": {
82. "type": "sparse_vector"
83. }
84. }
85. }
86. }
步骤 6:其他文档修复
让我们再次检查一些有关食谱的文件。
1. GET recipes/_search
2. {
3. "size": 2000,
4. "_source": ["ingredient"]
5. }
如果你在控制台的右侧按 cntr+f find,你可能会注意到有些文档带有双引号或三引号。
在这里,我们创建一个摄取节点管道,将三重引号替换为单引号。
我们将我们的管道称为 “doublequotes”。
1. PUT _ingest/pipeline/doublequotes
2. {
3. "processors": [
4. {
5. "gsub": {
6. "field": "ingredient",
7. "pattern": "\"(.*?)",
8. "replacement": ""
9. }
10. }
11. ]
12. }
为了测试我们的管道,我们可以使用_simulate API。
1. POST _ingest/pipeline/doublequotes/_simulate
2. {
3. "docs": [
4. {
5. "_index": "recipes",
6. "_id": "id",
7. "_source": {
8. "ingredient": """shredded cheese, and even some cilantro for a great-tasting breakfast burrito that will keep your appetite curbed all day long.","prep: 15 mins,cook: 5 mins,total: 20 mins,Servings: 2,Yield: 2 burritos","2 (10 inch) flour tortillas + 1 tablespoon butter + 4 medium eggs + 1 cup shredded mild Cheddar cheese + 1 Hass avocado - peeled, pitted, and sliced + 1 small tomato, chopped + 1 small bunch fresh cilantro, chopped, or to taste (Optional) + 1 pinch salt and ground black pepper to taste + 1 dash hot sauce, or to taste (Optional)""",
9. "tags": 2342
10. }
11. }
12. ]
13. }
步骤 7:带推理的管道
下面我们创建一个管道来清理引文并应用推理处理器。推理处理器是我们用来针对索引中的成分字段运行我们的模型(在本例中为 ELSER)的工具。回想一下,我们部署的名称是 else_model,我们在这里看到它被称为 model_id。我们将此管道称为 else_clean_recipes。
1. PUT _ingest/pipeline/elser_clean_recipes
2. {
3. "processors": [
4. {
5. "pipeline": {
6. "name": "doublequotes"
7. },
8. "inference": {
9. "model_id": "elser_model",
10. "target_field": "ml",
11. "field_map": {
12. "ingredient": "text_field"
13. },
14. "inference_config": {
15. "text_expansion": {
16. "results_field": "tokens"
17. }
18. }
19. }
20. }
21. ]
22. }
请注意,模型默认对 “text_field” 字段进行向量化。在“field_map”行中,我们配置推理处理器以使用不同的字段(在本例中为成分)。
一定要测试。
1. POST _ingest/pipeline/elser_clean_recipes/_simulate
2. {
3. "docs": [
4. {
5. "_index": "recipes",
6. "_id": "id",
7. "_source": {
8. "ingredient": """prep: 20 mins,cook: 20 mins,total: 40 mins,Servings: 4,Yield: 4 servings","½ small onion, chopped + ½ tomato, chopped + 1 jalapeno pepper, seeded and minced + 1 sprig fresh cilantro, chopped + 6 eggs, beaten + 4 (10 inch) flour tortillas + 2 cups shredded Cheddar cheese + ¼ cup sour cream, for topping + ¼ cup guacamole, for topping""",
9. "tags": 2342
10. }
11. },
12. {
13. "_index": "recipes",
14. "_id": "id",
15. "_source": {
16. "ingredient":
17. """"shredded cheese, and even some cilantro for a great-tasting breakfast burrito that will keep your appetite curbed all day long.","prep: 15 mins,cook: 5 mins,total: 20 mins,Servings: 2,Yield: 2 burritos","2 (10 inch) flour tortillas + 1 tablespoon butter + 4 medium eggs + 1 cup shredded mild Cheddar cheese + 1 Hass avocado - peeled, pitted, and sliced + 1 small tomato, chopped + 1 small bunch fresh cilantro, chopped, or to taste (Optional) + 1 pinch salt and ground black pepper to taste + 1 dash hot sauce, or to taste (Optional)"""
18. }},
19. {"_index": "recipes",
20. "_id": "id",
21. "_source": {
22. "ingredient": """shredded cheese, and even some cilantro for a great-tasting breakfast burrito that will keep your appetite curbed all day long.","prep: 15 mins,cook: 5 mins,total: 20 mins,Servings: 2,Yield: 2 burritos","2 (10 inch) flour tortillas + 1 tablespoon butter + 4 medium eggs + 1 cup shredded mild Cheddar cheese + 1 Hass avocado - peeled, pitted, and sliced + 1 small tomato, chopped + 1 small bunch fresh cilantro, chopped, or to taste (Optional) + 1 pinch salt and ground black pepper to taste + 1 dash hot sauce, or to taste (Optional)"""}}
23. ]
24. }
步骤 8:通过管道处理数据
现在我们有了管道,是时候处理我们的索引了。我们将使用 _reindex API 将数据从 recipes 索引发送到 recipes_embeddings。在此过程中,数据将通过我们的管道来创建嵌入。
重新索引可能需要很长时间,因此我们在这里使用一个名为 wait_for_completion=false 的选项进行运行。
当我们运行命令时,它将生成一个 ID 号,我们可以使用它来检查进度。确保将此 ID 复制并粘贴到某处。
此外,重新索引正在使用选项......
requests_per_second=-1&timeout=60m
...分别以尽可能快的速度运行并且不会太快超时。
**警告:**可能需要大约 15-30 分钟
1. POST _reindex?wait_for_completion=false&requests_per_second=-1&timeout=60m
2. {
3. "conflicts": "proceed",
4. "source": {
5. "index": "recipes",
6. "size": 500
7. },
8. "dest": {
9. "index": "recipes_embeddings",
10. "pipeline": "elser_clean_recipes"
11. }
12. }
复制并使用任务编号来跟踪重新索引过程。
GET _tasks/< paste task number here >
例如,我复制并粘贴了这个ID:LAV3l8oZTmaR9p8VUVqO3g:373447
如果需要,你可以像这样删除 recipes_embeddings 索引。
DELETE recipes_embeddings
步骤 9:检查已处理的文件
至少一批文档完成后,检查结果。
1. GET _cat/indices?v&s=i
3. GET recipes_embeddings/_search
冗长的 ml.tokens 字段使得输出看起来不太好看。你可以像这样抑制它。
1. GET recipes_embeddings/_search
2. { "_source": { "excludes": "ml"} }
1. GET recipes_embeddings/_count
2. 4808 documents
我们可以运行这个聚合来找到我们可以查询的 “group” 中的所有不同值。
1. GET recipes_embeddings/_search?size=0
2. {
3. "aggs": {
4. "all the groups": {
5. "terms": {
6. "field": "group.keyword",
7. "size": 200
8. }
9. }
10. }
11. }
有多少个桶?
1. GET recipes_embeddings/_search
2. {
3. "size": 0,
4. "aggs": {
5. "how many buckets": {
6. "cardinality": {
7. "field": "group.keyword",
8. "precision_threshold": 200
9. }
10. }
11. }
12. }
我得到了 174 个不同的组。
为了隔离输出中的组以便我们可以在文档中看到它们,你可以运行以下命令:
GET recipes_embeddings/_search?size=1000&_source=group
搜索
最后,数据准备好了,我们可以在 recipes_embeddings 索引上执行搜索。让我们比较一下没有 ELSER 的运行结果和有 ELSER 的运行结果。
搜索 1:Old fashion(老式鸡尾酒)
首先,我们来寻找一种名为 Old Fashion 的鸡尾酒的配方。下面介绍如何进行此操作。
-- 老式波本鸡尾酒 --
- 将一到两颗樱桃放入老式老式玻璃杯中,并用捣碎器轻轻捣碎。
- 取橙皮并擦拭玻璃杯边缘内侧,然后将果皮放在樱桃上。
- 加入冰块、黑麦威士忌、糖和苦味酒。
- 搅拌均匀即可食用。
首先,没有 ELSER 的情况下:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml", "includes": ["name","group","summary","ingredient"]},
5. "query": {
6. "bool": {
7. "should": [ { "wildcard": { "group": {"value": "drinks*" }}},
8. {
9. "multi_match": {
10. "type": "phrase",
11. "query": "old fashion",
12. "fields": [
13. "summary",
14. "name"]}},
15. {
16. "match": {
17. "summary": "delicious sensational"
18. }
19. }
20. ]
21. }
22. }
23. }
在你的控制台中运行它,你将看到热门点击中有一些非常糟糕的结果。
对我来说,它们都不是饮料(尽管我们搜索了饮料*组)。
我的许多搜索结果中都有诸如 “good old fashion meals and dishes...” 这样的短语,但这并不是我们想要的。
现在让我们用 ELSER 搜索...并看看出色的结果!
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml",
5. "includes": [
6. "name",
7. "group",
8. "summary",
9. "ingredient"
10. ]
11. },
12. "sub_searches": [
13. {
14. "query": {
15. "bool": {
16. "should": [
17. {
18. "wildcard": {
19. "group": {
20. "value": "drinks*"
21. }
22. }
23. },
24. {
25. "multi_match": {
26. "query": "old fashioned",
27. "type": "phrase",
28. "fields": [
29. "summary",
30. "name"
31. ]
32. }
33. }
34. ]
35. }
36. }
37. },
38. {
39. "query": {
40. "text_expansion": {
41. "ml.tokens": {
42. "model_id": "elser_model",
43. "model_text": "old fashioned bourbon whiskey whisky drink"
44. }
45. }
46. }
47. }
48. ],
49. "rank": {
50. "rrf": {
51. "window_size": 500,
52. "rank_constant": 60
53. }
54. }
55. }
请注意,所有这些都是成人饮料,并且有几种饮料的名称以 “Old Fashion” 开头。
搜索 2:Shrimp dishes
没有 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "query": {
7. "bool": {
8. "should": [
9. {
10. "wildcard": {
11. "group": {
12. "value": "main*"
13. }
14. }
15. },
16. {
17. "multi_match": {
18. "type": "phrase",
19. "query": "tempura shrimp",
20. "fields": [
21. "ingredient",
22. "name^2"
23. ]
24. }
25. },
26. {
27. "match": {
28. "summary": "tasty delightful"
29. }
30. }
31. ]
32. }
33. }
34. }
前五名的结果相当糟糕:我得到了像 pork tenderloin、Spanish sauce、carrot salad 这样的食谱……甚至没有虾。
使用 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "sub_searches": [
7. {
8. "query": {
9. "bool": {
10. "should": [
11. {
12. "wildcard": {
13. "group": {
14. "value": "main*"
15. }
16. }
17. },
18. {
19. "multi_match": {
20. "query": "tempura shrimp",
21. "type":"phrase",
22. "fields": [
23. "ingredient",
24. "name^2"
25. ]
26. }
27. }/*,
28. {
29. "match": {
30. "summary": "tasty delightful"
31. }
32. }*/
33. ]
34. }
35. }
36. },
37. {
38. "query": {
39. "text_expansion": {
40. "ml.tokens": {
41. "model_id": "elser_model",
42. "model_text": "tempura shrimp"
43. }
44. }
45. }
46. }
47. ],
48. "rank": {
49. "rrf": {
50. "window_size": 500,
51. "rank_constant": 60
52. }
53. }
54. }
排名前五的菜品要好得多:Shrimp with Pasta、Shrimp Scampis、Penne with Shrimp、Penne with Shrimp、Shrimp Quiche
请注意,Elasticsearch 如何通过语义搜索发现 shrimp 和 scampi 以及 prawns 等其他术语也具有相关性。
搜索 3:Spaghetti dishes
没有 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "query": {
7. "bool": {
8. "should": [
9. {
10. "wildcard": {
11. "group": {
12. "value": "main*"
13. }
14. }
15. },
16. {
17. "multi_match": {
18. "type": "phrase",
19. "query": "Spaghetti Bolognese",
20. "fields": [
21. "ingredient",
22. "name^2"
23. ]
24. }
25. },
26. {
27. "match": {
28. "summary": "tasty delightful"
29. }
30. }
31. ]
32. }
33. }
34. }
前五名中又有糟糕的:pork tenderloin, Med. sauce, carrot salad, lime chicken……
我根本不喜欢意大利面。
使用 ELSER:
1. GET recipes_embeddings/_search?filter_path=hits.hits._source
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "sub_searches": [
7. {
8. "query": {
9. "bool": {
10. "should": [
11. {
12. "wildcard": {
13. "group": {
14. "value": "main*"
15. }
16. }
17. },
18. {
19. "multi_match": {
20. "query": "Spaghetti Bolognese",
21. "type":"phrase",
22. "fields": [
23. "ingredient",
24. "name^2"
25. ]
26. }
27. }/*,
28. {
29. "match": {
30. "summary": "tasty delightful"
31. }
32. }*/
33. ]
34. }
35. }
36. },
37. {
38. "query": {
39. "text_expansion": {
40. "ml.tokens": {
41. "model_id": "elser_model",
42. "model_text": "main Spaghetti Bolognese"
43. }
44. }
45. }
46. }
47. ],
48. "rank": {
49. "rrf": {
50. "window_size": 500,
51. "rank_constant": 60
52. }
53. }
54. }
改进了很多。很多食材都有意大利面或通心粉
再次排在最前面:Pennes, pastas, spaghettis。
搜索 4:巧克力
没有 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {"excludes": "ml"},
4. "query": {
5. "bool": {
6. "should": [
7. {
8. "match": {
9. "name": "dessert"
10. }
11. },
12. {
13. "match": {
14. "ingredient": "chocolate"
15. }
16. },
17. {
18. "match": {
19. "summary": "tasty delightful"
20. }
21. }
22. ]
23. }
24. }
25. }
奶昔和薄饼……?
我找不到任何含巧克力的配料。
使用 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {"excludes": "ml"},
4. "sub_searches": [
5. {
6. "query": {
7. "bool": {
8. "should": [
9. {
10. "match": {
11. "name": "dessert"
12. }
13. },
14. {
15. "match": {
16. "ingredient": "chocolate"
17. }
18. },
19. {
20. "match": {
21. "summary": "tasty delightful"
22. }
23. }
24. ]
25. }
26. }
27. },
28. {
29. "query": {
30. "text_expansion": {
31. "ml.tokens": {
32. "model_id": "elser_model",
33. "model_text": "dessert chocolate"
34. }
35. }
36. }
37. }
38. ],
39. "rank": {
40. "rrf": {
41. "window_size": 50,
42. "rank_constant": 20
43. }
44. }
45. }
Peppermint bark 是巧克力的一个晦涩术语。
Nanaimo bars 是一种巧克力顶上的饼干。我还看到hot chocolate, chocolate muffins, chocolate cake, Oreo truffles 和 cake balls,其中的配料包括巧克力。
我看到很多配料都含有巧克力。
我们再看一下我们可以查询的 “groups”。
1. GET recipes_embeddings/_search?size=0
2. {
3. "aggs": {
4. "all the groups": {
5. "terms": {
6. "field": "group.keyword",
7. "size": 200
8. }
9. }
10. }
11. }
everyday-cooking 有多少种菜谱?
1. GET recipes_embeddings/_count
2. {
3. "query": {
4. "wildcard": {
5. "group.keyword": {
6. "value": "everyday-cooking*"
7. }
8. }
9. }
10. }
我得到 310。
鱼肉三明治 - Fish Sandwich
让我们在日常烹饪中寻找 “Fish Sandwich”。
没有 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "query": {
7. "bool": {
8. "should": [
9. {
10. "wildcard": {
11. "group": {
12. "value": "everyday-cooking*"
13. }
14. }
15. },
16. {
17. "multi_match": {
18. "type": "phrase",
19. "query": "fish sandwich",
20. "fields": [
21. "ingredient",
22. "name^2"
23. ]
24. }
25. }
26. ]
27. }
28. }
29. }
没有!
哇,根本没有鱼肉三明治……?
使用 ELSER 添加嵌入搜索。
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "sub_searches": [
7. {
8. "query": {
9. "bool": {
10. "should": [
11. {
12. "wildcard": {
13. "group": {
14. "value": "everyday-cooking*"
15. }
16. }
17. },
18. {
19. "multi_match": {
20. "query": "fish sandwich",
21. "type":"phrase",
22. "fields": [
23. "ingredient",
24. "name^2"
25. ]
26. }
27. }
28. ]
29. }
30. }
31. },
32. {
33. "query": {
34. "text_expansion": {
35. "ml.tokens": {
36. "model_id": "elser_model",
37. "model_text": "fish sandwich"
38. }
39. }
40. }
41. }
42. ],
43. "rank": {
44. "rrf": {
45. "window_size": 500,
46. "rank_constant": 60
47. }
48. }
49. }
在右侧搜索 “三明治”。
我看到 tuna patties, tuna salads - 很多鱼,很多都是三明治。
让我们看看“主菜”类别中有多少食谱?
让我们检查一下 “Main” 类别中有多少个食谱?
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml"
5. },
6. "query": {
7. "bool": {
8. "must": [
9. {
10. "match": {
11. "group": "main*"
12. }
13. }
14. ]
15. }
16. }
17. }
我得到了458。
里脊牛排 - Tenderloin Steak
请注意,在美国我们说 “tenderloins or tenderloin steaks”,但在法国它被称为 Chateaubriand。还请注意,“Chateaubriand” 不在任何食谱中。可以使用 multi_match 命令来搜索许多字段,如下所示:
1. GET recipes_embeddings/_search
2. {
3. "query": {
4. "multi_match": {
5. "query": "Chateaubriand",
6. "fields": ["ingredient","summary","name"]
7. }
8. }
9. }
零结果
不使用 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml", "includes": ["name","group","summary","ingredient"]},
5. "query": {
6. "bool": {
7. "should": [ { "wildcard": { "group": {"value": "main*" }}},
8. {
9. "multi_match": {
10. //"type": "phrase",
11. "query": "tenderloin steak Chateaubriand beef",
12. "fields": [
13. "ingredient",
14. "name^2"
15. ]
16. }
17. },
18. {
19. "match": {
20. "summary": "delicious Chateaubriand"
21. }
22. },
23. {
24. "match": {
25. "ingredient": "beef"
26. }
27. }
28. ]
29. }
30. }
31. }
前十名的成绩相当糟糕。Salt and pepper fries, salt bread, Pork Tenderloin 等等。
使用 ELSER:
1. GET recipes_embeddings/_search
2. {
3. "_source": {
4. "excludes": "ml", "includes": ["name","group","summary","ingredient"]},
5. "sub_searches": [
6. {
7. "query": {
8. "bool": {
9. "should": [
10. {
11. "wildcard": {"group": {"value": "main*"}}},
12. {
13. "multi_match": {
14. "query": "tenderloin steak Chateaubriand beef",
15. //"type":"phrase",
16. "fields": [
17. "ingredient",
18. "name^2"
19. ]
20. }
21. },
22. {"match":{
23. "ingredient":{
24. "query": "beef"
25. }
26. }}
27. ]
28. }
29. }
30. },
31. {
32. "query": {
33. "text_expansion": {
34. "ml.tokens": {
35. "model_id": "elser_model",
36. "model_text": "tenderloin steak Chateaubriand beef"
37. }
38. }
39. }
40. }
41. ],
42. "rank": {
43. "rrf": {
44. "window_size": 500,
45. "rank_constant": 60
46. }
47. }
48. }
更多实际牛排。
恭喜!我们已经研究了许多示例,其中使用 ELSER 对数据进行向量化使我们能够进行语义搜索。我们将常规的、可能预先存在的术语搜索与向量搜索相结合,发现混合搜索结果通常比仅搜索术语本身更相关。