语义/矢量搜索是一种强大的技术,可以大大提高搜索结果的准确性和相关性。 与传统的基于关键字的搜索方法不同,语义搜索使用单词的含义和上下文来理解查询背后的意图并提供更准确的结果。 Elasticsearch 是实现语义搜索最流行的工具之一,它是一种高度可扩展且功能强大的搜索引擎,可用于索引和搜索大量数据。 在本文中,我们将探讨语义搜索的基础知识以及如何使用 Elasticsearch 实现它。 到本文结束时,你将深入了解语义搜索的工作原理以及在你自己的项目中实现它的实用技能。

在进行下面的讲解之前,我需要特别指出的是:Elastic 提供了 eland 帮助我们上传在 huggingface.co 上的模型。我们在摄入文档的时候,可以使用 inference processor 来方便地进行数据字段的矢量化。eland 上传及机器学习是 Elastic 的收费项目。本文章将使用 Tensorflow 来通过代码的方式来获得矢量,并上传到 Elasticsearch。更多关于使用 eland 及机器学习上传模型的方法,请详细阅读 “ Elastic:开发者上手指南 ” 中的 “ NLP - 自然语言处理及矢量搜索 ” 章节。

Elasticsearch

Elasticsearch 是一个基于 Lucene 库的强大且可扩展的免费及开发的搜索引擎。 它旨在处理大量非结构化数据并提供快速准确的搜索结果。 Elasticsearch 使用分布式架构,这意味着它可以横向扩展到多个服务器以处理大量数据和流量。

Elasticsearch 建立在 RESTful API 之上,这使得它可以轻松地与各种编程语言和工具集成。 它支持复杂的搜索查询,包括全文搜索、分面搜索和地理搜索。 Elasticsearch 还提供了一个强大的聚合框架,允许你对搜索结果进行复杂的数据分析。

Transformers

Transformers 是一种机器学习模型,它彻底改变了自然语言处理 (NLP) 任务,例如语言翻译、文本摘要和情感分析。 Vaswani 等人首先介绍了 transformer。 在 2017 年的一篇论文 “Attention Is All You Need” 中,此后已成为许多 NLP 任务的最先进模型。

与循环神经网络 (RNN) 和卷积神经网络 (CNN) 的传统 NLP 模型不同,Transformer 使用 self-attention 机制来捕捉句子中单词之间的关系。 Self-attentiion 允许模型关注输入序列的不同部分,以确定单词之间最重要的关系。 这使得转换器比传统模型更有效地处理单词之间的远程依赖关系和上下文关系。

对于本文,我将使用 TensorFlow 的通用句子编码器对我的数据进行编码/矢量化。 你也可以选择任何其他形式的编码器。 另外值得指出的是 :tensorflow 在 Apple 的芯片上不能得到支持。你需要使用 x86 的机器来进行练习。

为了方便大家学习,我把代码放在地址: github.com/liu-xiao-gu…

Elasticsearch 及 Kibana

如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考文章:

  • 如何在 Linux,MacOS 及 Windows 上进行安装 Elasticsearch
  • Kibana:如何在 Linux,MacOS 及 Windows 上安装 Elastic 栈中的 Kibana
  • 在我们的本次练习中,我们将使用 Elastic Stack 8.8 版本。在 Elasticsearch 首次启动的时候,它会出现如下的屏幕:

    我们记下 elastic 用户的密码及 fingerprint。这些信息在一下的代码中进行使用。

    Python

    你需要在自己的电脑上安装 Python:

    1. $ python --version 2. Python 3.10.6

    你同时需要安装如下的 Python 库:

    1. pip3 install elasticsearch 2. pip3 install tensorflow _hub 3. pip3 install tensorflow 4. pip3 install pandas 5. pip3 install numpy

    Tensorflow 模型

    你需要去地址 tfhub.dev/google/univ… 下载 universal-sentence-encoder 模型。下载完后,你把它置于代码根目录下的 model 子目录下:

    1. $ pwd 2. /Users/liuxg/python/Semantic-Search-ElasticSearch 3. $ ls 4. README.md model 5. Semantic _Search_ ElasticSearch.py sample.csv 6. $ tree -L 3 8. ├── README.md 9. ├── Semantic _Search_ ElasticSearch.py 10. ├── model 11. │   ├── assets 12. │   ├── saved _model.pb 13. │   ├── universal-sentence-encoder_ 4.tar.gz 14. │   └── variables 15. │   ├── variables.data-00000-of-00001 16. │   └── variables.index 17. └── sample.csv

    我们在 model 子目录下,打入如下的命令来解压缩文件 universal-sentence-encoder_4.tar.gz:

    tar xzf universal-sentence-encoder_4.tar.gz
    

    如上所示,我准备了一个叫做 sample.csv 的文件。它的内容非常之简单:

    sample.csv

    1. Text,Price,Quantity 2. "The latest phone model",5000,10 3. "The best seller phone",2000,50

    也就是只有两个文档。你可以根据自己的情况修改这个文档。

    我先把代码贴出来:

    Semantic_Search_ElasticSearch.py

    1. from elasticsearch import Elasticsearch 2. import tensorflow_hub as hub 3. import tensorflow.compat.v1 as tf 4. import pandas as pd 5. import numpy as np 7. df = pd.read_csv('./sample.csv') 8. print(df['Text'][0]) 10. model = hub.load("./model") 12. graph = tf.Graph() 14. with tf.Session(graph = graph) as session: 15. print("Loading pre-trained embeddings") 16. embed = hub.load("./model") 17. text_ph = tf.placeholder(tf.string) 18. embeddings = embed(text_ph) 20. print("Creating tensorflow session…") 21. session = tf.Session() 22. session.run(tf.global_variables_initializer()) 23. session.run(tf.tables_initializer()) 25. vectors = session.run(embeddings, feed_dict={text_ph: df['Text']}) 27. print("vectors length: ", len(vectors)) 28. print(vectors) 30. vector = [] 31. for i in vectors: 32. vector.append(i) 34. df["Embeddings"] = vector 36. # Connect to the elastic cluster 37. # Password for the 'elastic' user generated by Elasticsearch 38. USERNAME = "elastic" 39. PASSWORD = "GHI8C685oSpq_kNtUJV1" 40. ELATICSEARCH_ENDPOINT = "https://localhost:9200" 41. CERT_FINGERPRINT = "abec585e4d6c383032d19f8c535369107f063ae91491e20b5e25b75afb308f13" 43. es = Elasticsearch(ELATICSEARCH_ENDPOINT, 44. ssl_assert_fingerprint = (CERT_FINGERPRINT), 45. basic_auth=(USERNAME, PASSWORD), 46. verify_certs = True) 47. resp = es.info() 48. print(resp) 50. configurations = { 51. "settings": { 52. "index": {"number_of_replicas": 2}, 53. "analysis": { 54. "filter": { 55. "ngram_filter": { 56. "type": "edge_ngram", 57. "min_gram": 2, 58. "max_gram": 15, 59. } 60. }, 61. "analyzer": { 62. "ngram_analyzer": { 63. "type": "custom", 64. "tokenizer": "standard", 65. "filter": ["lowercase", "ngram_filter"], 66. } 67. } 68. } 69. }, 70. "mappings": { 71. "properties": { 72. "Embeddings": { 73. "type": "dense_vector", 74. "dims": 512, 75. "index": True, 76. "similarity": "cosine" 77. }, 78. } 79. } 80. } 83. INDEX_NAME = "vectors" 85. if(es.indices.exists(index=INDEX_NAME)): 86. print("The index has already existed, going to remove it") 87. es.options(ignore_status=404).indices.delete(index=INDEX_NAME) 89. es.indices.create( index=INDEX_NAME, 90. settings=configurations["settings"], 91. mappings=configurations["mappings"] 92. ) 94. actions = [] 95. for index, row in df.iterrows(): 96. action = {"index": {"_index": INDEX_NAME, "_id": index}} 97. doc = { 98. "id": index, 99. "Text": row["Text"], 100. "Price": row["Price"], 101. "Quantity": row["Quantity"], 102. "Embeddings": row["Embeddings"] 103. } 104. actions.append(action) 105. actions.append(doc) 107. es.bulk(index=INDEX_NAME, operations=actions, refresh=True) 109. query = "Which is the latest phone available in your shop" 112. def embed_text(text): 113. vectors = session.run(embeddings, feed_dict={text_ph: text}) 114. return [vector.tolist() for vector in vectors] 116. query_vector = embed_text([query])[0] 117. print(query_vector) 119. query = { 120. "field": "Embeddings", 121. "query_vector": query_vector, 122. "k": 10, 123. "num_candidates": 100 124. } 126. source_fields = ["Text", "Price", "Quantity"] 128. response = es.search( 129. index="vectors", 130. fields=source_fields, 131. knn=query, 132. source=False) 134. print(response)

    这是整个代码。虽然看起来简单,但是在调试的时候还是出现了一些状况。

    安装 Python 依赖项后,你将需要文本数据作为开始。 获取文本数据后,在你喜欢的 IDE 中使用 python 读取它。

    1. from elasticsearch import Elasticsearch 2. import tensorflow_hub as hub 3. import tensorflow.compat.v1 as tf 4. import pandas as pd 5. import numpy as np 7. df = pd.read_csv('./sample.csv') 8. print(df['Text'][0])

    读取文本数据后,第一个任务是将其转换为向量或嵌入。 在这里,正如我之前提到的,我使用的是 TensorFlow 的通用句子编码器,它在提供字符串后输出 “512” 维度的向量/嵌入。

    这对于其他转换器/矢量化器会有所不同,你需要记住这一点以便进一步执行步骤。

    model = hub.load("./model")
    

    成功加载模型后,现在我们的下一个任务是将数据集中的文本转换为向量/嵌入,并将其存储在名为 “Embeddings” 的新字段/列中。

    1. graph = tf.Graph() 3. with tf.Session(graph = graph) as session: 4. print("Loading pre-trained embeddings") 5. embed = hub.load("./model") 6. text_ph = tf.placeholder(tf.string) 7. embeddings = embed(text_ph) 9. print("Creating tensorflow session…") 10. session = tf.Session() 11. session.run(tf.global_variables_initializer()) 12. session.run(tf.tables_initializer()) 14. vectors = session.run(embeddings, feed_dict={text_ph: df['Text']}) 16. print("vectors length: ", len(vectors)) 17. print(vectors) 19. vector = [] 20. for i in vectors: 21. vector.append(i) 23. df["Embeddings"] = vector

    注意:在我的数据集中,我有一个名为 “Text” 的字段/列。 根据你的数据集将其更改为字段名称。

    一旦嵌入完成并存储在新字段中,就可以将此数据插入我们系统中的 Elasticsearch,你应该已经在本教程开始时安装了它。

    要插入数据,我们首先必须连接到 Elasticsearch,所有这一切都将使用 python 进行。

    1. USERNAME = "elastic" 2. PASSWORD = "GHI8C685oSpq_kNtUJV1" 3. ELATICSEARCH_ENDPOINT = "https://localhost:9200" 4. CERT_FINGERPRINT = "abec585e4d6c383032d19f8c535369107f063ae91491e20b5e25b75afb308f13" 6. es = Elasticsearch(ELATICSEARCH_ENDPOINT, 7. ssl_assert_fingerprint = (CERT_FINGERPRINT), 8. basic_auth=(USERNAME, PASSWORD), 9. verify_certs = True) 10. resp = es.info() 11. print(resp)

    有关这个部分的描述,请详细阅读我之前的文章 “Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”。

    要验证连接是否已建立,你可以在首选浏览器上打开 https://localhost:9200 并检查。 你还可以通过运行 es.ping() 从你的 IDE 检查连接。 对于成功的连接,输出应该是 True。

    现在我们已经建立了与 Elasticsearch 的连接,让我们继续配置 Elasticsearch 索引。

    1. configurations = { 2. "settings": { 3. "index": {"number_of_replicas": 2}, 4. "analysis": { 5. "filter": { 6. "ngram_filter": { 7. "type": "edge_ngram", 8. "min_gram": 2, 9. "max_gram": 15, 10. } 11. }, 12. "analyzer": { 13. "ngram_analyzer": { 14. "type": "custom", 15. "tokenizer": "standard", 16. "filter": ["lowercase", "ngram_filter"], 17. } 18. } 19. } 20. }, 21. "mappings": { 22. "properties": { 23. "Embeddings": { 24. "type": "dense_vector", 25. "dims": 512, 26. "index": True, 27. "similarity": "cosine" 28. }, 29. } 30. } 31. } 34. INDEX_NAME = "vectors" 36. if(es.indices.exists(index=INDEX_NAME)): 37. print("The index has already existed, going to remove it") 38. es.options(ignore_status=404).indices.delete(index=INDEX_NAME) 40. es.indices.create( index=INDEX_NAME, 41. settings=configurations["settings"], 42. mappings=configurations["mappings"] 43. )

    在上述配置的帮助下,我们能够配置插入数据的索引。 也就是说,让我们仔细看看一些重要的参数。

  • “type”:类型必须始终设置为 “dense_vector”。 这样做是为了让 ElasticSearch 知道这些是向量,并且不会自行将浮动类型分配给该字段。
  • “dims”:也即维度。 就像我之前提到的,Universal Sentence Encoder 产生和输出 512 维度,这就是我们在参数中提供 512 的原因。
  • “index”:Index 必须设置为 True,以便创建该字段并在 ElasticSearch 中具有 dense_vector 类型。
  • “similarity”:我们正在寻找余弦相似性并已经提到了它。 你也可以选择其他选项。具体可以参考链接
  • 配置索引后,现在让我们继续创建这个索引。在我们的应用中,我们选择 index 的名字为 vectors。

    在这里,我将索引命名为 vectors。 有了这个,我们的索引已经用我们的配置创建了,最后我们准备好将我们的数据插入到 Elasticsearch 上的这个索引中。

    1. actions = [] 2. for index, row in df.iterrows(): 3. action = {"index": {"_index": INDEX_NAME, "_id": index}} 4. doc = { 5. "id": index, 6. "Text": row["Text"], 7. "Price": row["Price"], 8. "Quantity": row["Quantity"], 9. "Embeddings": row["Embeddings"] 10. } 11. actions.append(action) 12. actions.append(doc) 14. es.bulk(index=INDEX_NAME, operations=actions, refresh=True)

    在上面的代码中,我们必须注意的是 refresh 必须设置为 True,否则在下面立马进行搜索的时候,我们可能得不到任何的结果,这是因为在通常的情况下,需要 1 分钟的时间才能使得刚写入的文档变为可以搜索的。借助以上代码,你将能够将数据插入 Elasticsearch。

    插入数据后,我们现在可以搜索此数据并提出一些相关问题。 为此,让我们从一个我们想要获得答案的问题开始。

    query = "Which is the latest phone available in your shop?"
    

    现在,由于我们需要在 Elasticsearch 上进行语义搜索,我们需要将此文本转换为嵌入/向量。

    1. query = "Which is the latest phone available in your shop" 4. def embed_text(text): 5. vectors = session.run(embeddings, feed_dict={text_ph: text}) 6. return [vector.tolist() for vector in vectors] 8. query_vector = embed_text([query])[0] 9. print(query_vector)

    现在 query_vector 含有 “Which is the latest phone available in your shop” 所转换而来的向量。

    将文本转换为嵌入/向量后,我们就可以根据 Elasticsearch 中的现有数据搜索此文本。 为此,我们首先必须构建一个查询以从 Elasticsearch 获取数据。

    1. query = { 2. "field": "Embeddings", 3. "query_vector": query_vector, 4. "k": 10, 5. "num_candidates": 100 6. } 8. source_fields = ["Text", "Price", "Quantity"] 10. response = es.search( 11. index="vectors", 12. fields=source_fields, 13. knn=query, 14. source=False) 16. print(response)

    使用上面提供的代码,我们可以从 Elasticsearch 进行查询。 但在我们看下一步之前,让我们仔细看看这个查询并理解它。

  • “knn”:Elasticsearch 支持 K-Nearest Neighbors a.k.a kNN 算法并且已经在 Elasticsearch 中可用。 你不需要单独训练它。
  • “field”:你的嵌入/向量存储在 Elasticsearch 中的字段。
  • “query_vector”:你以向量/嵌入形式输入。
  • “k”:你需要的输出/搜索结果数。
  • “num_candidates”:earch API finds a num_candidates number of approximate nearest neighbor candidates on each shard.
  • 借助上述查询,你将能够从之前存储数据的索引中获取搜索结果。

    请记住,你只能对具有配置字段的索引执行语义搜索,该字段包含嵌入/向量作为 "type": "dense_vector" 并且向量维度必须与你的查询/问题和存储在 Elasticsearch 中的数据完全相同 . 例如,在上面的教程中,我们在 Elasticsearch 中的数据是 512 维度,在我们继续搜索操作之前,query/question 也被转换为 512 维度。 

    总之,语义搜索是一种强大的工具,可以通过理解单词的含义和上下文来大大提高搜索结果的准确性和相关性。 Elasticsearch 是一个高度可扩展且灵活的搜索引擎,可用于为从电子商务到医疗保健的各种应用程序实现语义搜索。 通过利用 Elasticsearch 强大的搜索和索引功能,以及查询扩展、同义词检测和实体识别等技术,你可以构建一个提供快速准确结果的语义搜索系统。 无论你是开发人员、数据科学家还是企业主,使用 Elasticsearch 掌握语义搜索都可以帮助你从数据中获得新的见解和机会。 那为什么还要等? 立即开始使用 Elasticsearch 探索语义搜索的强大功能!