A large-scale high-dimensional data approximate neighbor query system and method based on Spark

By adopting the approximate nearest neighbor query method of cluster partitioning and HNSW index structure on the Spark platform, the scalability and efficiency problems in retrieval of massive high-dimensional vector data are solved, and high throughput and low-latency query performance are achieved, which is suitable for for large-scale non-spatial high-dimensional vector data.

CN114329094BActive Publication Date: 2024-09-10SHANGHAI JIAOTONG UNIV
View PDF 0 Cites 0 Cited by

Patent Information

Application Number
CN202111672312.3
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2021-12-31
Publication Date
2024-09-10
Estimated Expiration
2041-12-31

AI Technical Summary

Technical Problem

Existing nearest neighbor search algorithms have scalability and efficiency problems when processing massive high-dimensional vector data. Especially for non-spatial high-dimensional vector data, existing index structures and query methods cannot meet the throughput and delay requirements, and cannot Supports fast retrieval of large-scale non-spatial high-dimensional vector data.

Method used

Adopt a Spark-based large-scale high-dimensional data approximate nearest neighbor query system, which is constructed through clustering partitions, global indexes and partition indexes. The HNSW index structure is used to collect partition information in advance to filter relevant partitions and only perform queries in relevant partitions to improve query throughput. and reduce latency.

Benefits of technology

It achieves high throughput and low-latency approximate neighbor query, can effectively process large-scale non-spatial high-dimensional vector data, improves the scalability and query efficiency of the system, and achieves significant performance improvements by sacrificing a certain degree of accuracy.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN114329094B
    Figure CN114329094B
Patent Text Reader

Abstract

The present invention provides a large-scale high-dimensional data approximate neighbor query system and method based on Spark, which mainly executes approximate neighbor query in memory. First, cluster partitions are performed according to the similarity of vectors, and each cluster partition corresponds to a partition of the Spark elastic distributed data set. The data of each partition is proportionally sampled and labeled with the partition. The sampled data is used to establish a global index on the master node, and a partition index is established on the corresponding partition. When querying, the corresponding partitions that need to be queried are found through the global index, and then the results of each partition are summarized and sorted to obtain the final result. The technical solution of the present invention provides a highly scalable distributed approximate neighbor query solution based on the Spark system, while achieving the characteristics of low latency and high throughput.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to the technical field of computer data management, and more specifically, to a method and system for rapid retrieval of large-scale high-dimensional data. Background Art

[0002] Nearest neighbor search is an important operation in many applications, such as image retrieval, recommendation system and data mining. With the rapid development of artificial intelligence related fields, machine learning algorithms have made major breakthroughs in computer vision, speech recognition, natural language processing and other application fields. A large amount of unstructured data (pictures, voices, texts) can be converted into vectors, which is a more efficient data representation, resulting in the generation of massive vector data. Vector nearest neighbor search algorithms need to meet the requirements of scalability, high throughput, and low latency in order to efficiently process massive vector data. For example, in Taobao recommendations, the number of pictures in the database exceeds one billion. On the one hand, in the face of massive data, single-machine algorithms are no longer applicable, and retrieval methods in a distributed environment must be considered to meet the scalability of the system; on the other hand, retrieval services need to respond within a few milliseconds to meet the real-time needs of users. Accurate nearest neighbor search cannot meet the actual requirements for efficiency and cost, but approximate nearest neighbor search can improve throughput and meet the above requirements by slightly relaxing the accuracy limit.

[0003] Apache Spark is a distributed open source processing system for big data processing based on the mapReduce paradigm. Compared with Hadoop, which is disk-oriented and batch-processing oriented, Spark can use distributed memory storage and computing, and has the characteristics of low query latency and high throughput. The structure of Spark architecture is as follows Figure 1 As shown, the driver node is the master node and the worker nodes are the slave nodes.

[0004] Before introducing the relevant technologies, some necessary definitions are given:

[0005] X is a data set containing n vectors, X={x1,x2,x3,x4,x5,x6.....x n}, the dimension of each vector is d, dist(a,b)=||a,b||2 represents the Euclidean distance between any two vectors a and b, given the query vector q.

[0006] Definition 1 KNN (k-nearest Neighbors): kNN(q) is the query result, which contains a set V of k vectors belonging to X, that is, kNN(q) = V, where the distance between any other vector in X and q is greater than the distance between any vector in V.

[0007] Definition 2 AKNN (Appropriate k-nearest Neighbors):

[0008] The result returned by AKNN is an approximate result, denoted as AKNN(q), and the accurate result is denoted as KNN(q). The recall rate is used as a measure of accuracy, that is, recall = AKKN(q) ∩ KNN(q).

[0009] GeoSpark is an in-memory cluster computing framework for processing large-scale spatial data. It supports different spatial objects such as geometric points, polygons, and rectangles. It implements various query processing algorithms including kNN. GeoSpark uses a network result as a partition global index. However, this is not applicable to non-spatial vector data: kNN queries will search all partitions and then perform heap sorting, which has a huge computational overhead.

[0010] Location Spark uses grid and qd tree as global index, supports range query, kNN and other spatial query analysis, and designs memory management algorithm, which can dynamically store frequently accessed data into memory and infrequently accessed data into disk. STARK Hagedorn uses R-tree for spatial index, but provides different options for users to choose whether to create index. STARK also develops kNN and clustering query operations.

[0011] The above Spark-based work has achieved positive results in certain fields. However, there are still the following three shortcomings in the application of nearest neighbor query:

[0012] 1) It is specially designed for the characteristics of spatial data vectors, without considering the needs of non-spatial high-dimensional vectors. It cannot support the existing large amount of non-spatial high-dimensional vector data. It is necessary to mine the relationship between vectors and design more effective query methods.

[0013] 2) When querying, all partitions are often traversed, and then the results of each partition are collected and processed centrally to produce the final result, which is a large workload and time-consuming.

[0014] 3) All queries are accurate, with higher accuracy than actual requirements, but performance such as throughput and latency cannot meet actual requirements. Summary of the invention

[0015] The technical problems solved by the present invention are:

[0016] 1) Fully explore the relationship between vectors, and propose a suitable index structure based on the characteristics of non-spatial high-dimensional vectors, so as to support a large amount of non-spatial high-dimensional vector data and perform effective partition query.

[0017] 2) Before querying, relevant partitions are screened out by collecting information from each partition; when querying, only the relevant partitions are queried, which effectively improves the system query throughput.

[0018] 3) The AKNN approximate nearest neighbor query is implemented in the Spark architecture, which obtains additional acceleration by sacrificing the accuracy acceptable to the application and greatly improves the query throughput.

[0019] In a first aspect, an embodiment of the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark, the system comprising:

[0020] Vector acquisition module, index building module and query module.

[0021] The vector acquisition module is used to acquire the vector to be processed by the system, that is, the data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system.

[0022] The index building blocks include:

[0023] Clustering partitioning module, global index building module and partition index building module.

[0024] The cluster partition module is used to calculate the centroids of m partitions of the data set, and divide the data set into the m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, k∈N.

[0025] The global index building module is used to construct a global index in the master node of the system; the global index building module includes: a data sampling unit, a data marking unit and an indexing unit; the data sampling unit is used to uniformly sample n vectors to be processed, i.e., the points, from each partition according to the data set and the resource conditions of the master node, to form sampling data, and the distribution of the vectors to be processed in the partition is represented by the sampling data; the data marking unit is used to mark the partition label of the partition where the sampled data is located; the indexing unit is used to use the sampled data to build a global index in the master node and save the global index in the memory; the index structure of the global index adopts HNSW (a hierarchical neighbor search graph index, Hierarchical Navigable Skall World graph).

[0026] The partition index construction module is used to create a partition index for each partition and store the partition index in the memory; the index structure of the partition index adopts the HNSW.

[0027] The query module includes:

[0028] Query initiation module, global query module, partition query module and sorting module.

[0029] The query initiation module is used to initiate a query Q; the query Q specifies a query vector, the number of partitions to be queried s, and the number of result vectors k; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N.

[0030] The global index query module is used to search the p sampling data closest to the point represented by the query vector in the global index to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front column with a non-zero number of preliminary result vectors to become the partitions to be queried; p≥s, p∈N.

[0031] When the number of the partitions selected by the global index query module is less than s, s can be assigned a value according to the actual number of the partitions to be queried, and the partitions to be queried can be queried; or the number of the preliminary result vectors can be increased, that is, the value of p is increased, until the s partitions to be queried are selected.

[0032] The partition query module is used to query the point in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector.

[0033] The sorting module is used to sort the s×k partition result vectors obtained after the partition query module has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

[0034] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of sampled data, that is, the value of n, can be set by the user.

[0035] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of preliminary result vectors can also be set by the user.

[0036] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of partitions to be queried, that is, the value of s, can be set by the user.

[0037] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of partitions, that is, the value of m, can be set by the user.

[0038] In a second aspect, an embodiment of the present invention proposes a large-scale high-dimensional data approximate neighbor query method based on Spark, the method comprising:

[0039] Vector acquisition, index building and query;

[0040] The vector acquisition refers to acquiring a vector to be processed by the system, that is, a data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system;

[0041] The index build includes:

[0042] Cluster partitioning, global index building and partition index building;

[0043] The cluster partitioning refers to calculating the centroids of m partitions of the data set, dividing the data set into m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, m∈N;

[0044] The global index construction refers to constructing a global index in the master node of the system; the global index construction includes: data sampling, data labeling and indexing; the data sampling is to uniformly sample n vectors to be processed from each partition according to the data set and the resource conditions of the master node, that is, the points, to become sampled data, and the distribution of the vectors to be processed in the partition is represented by the sampled data; the data labeling is to label the sampled data with the partition label of the partition; the indexing is to use the sampled data to construct a global index in the master node and save the global index in the memory; the index structure of the global index adopts HNSW;

[0045] The partition index construction refers to creating a partition index for each partition and storing the partition index in the memory; the index structure of the partition index adopts the HNSW;

[0046] The query includes:

[0047] Query initiation, global query, partition query and sorting;

[0048] The query is initiated by initiating a query Q; the query Q specifies a query vector, the number of partitions to be queried s, and the number of result vectors k; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N;

[0049] The global index query is to search the global index for the p sampled data closest to the point represented by the query vector to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front column with a non-zero number of the preliminary result vectors as the partition to be queried; p≥s, p∈N;

[0050] When the number of partitions selected by the global index query is less than s,

[0051] The value of s can be assigned according to the actual number of the partitions to be queried, and the partitions to be queried can be queried;

[0052] The number of the preliminary result vectors may also be increased, that is, the value of p is increased, until the s partitions to be queried are selected;

[0053] The partition query is to query the point in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector;

[0054] The sorting is to sort the s×k partition result vectors obtained after the partition query module has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

[0055] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of sampled data, that is, the value of n, can be set by the user.

[0056] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of preliminary result vectors can also be set by the user.

[0057] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of partitions to be queried, that is, the value of s, can be set by the user.

[0058] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of partitions, that is, the value of m, can be set by the user.

[0059] The beneficial effects of the present invention are: in view of the problem that only accurate neighbor query can be performed in the Spark architecture, but not approximate neighbor query, and the problem that the demand of non-spatial high-dimensional vectors is not considered and the existing large amount of non-spatial high-dimensional vector data cannot be supported, the relationship between vectors is fully explored, and a suitable index structure based on Spark is proposed for the characteristics of non-spatial high-dimensional vectors, and the support for a large amount of non-spatial high-dimensional vector data is realized, which is suitable for approximate neighbor search, and a corresponding query mechanism is proposed; in view of the problem that all partitions are traversed during query, the results of each partition are collected, and the final result is generated after centralized processing, resulting in a large workload and time-consuming, a double-layer index architecture of global index and partition index is proposed, and before querying, the relevant partitions are screened out by collecting the information of each partition; when querying, only the relevant partition is queried, which effectively improves the system query throughput. Using the technical solution provided by the present invention, high-throughput, low-latency, and scalable approximate neighbor query can be achieved, and additional acceleration is obtained by sacrificing the accuracy acceptable to the application, greatly improving the query throughput.

[0060] As a mainstream distributed memory computing engine, Spark has strong scalability, the most active open source community and a large number of users. Since its release, Spark has become a general processing engine, playing an important role in machine learning, graph processing and stream data processing. Therefore, based on the Spark system, a large-scale high-dimensional data approximate neighbor query system and method based on Spark is proposed, which has wide applicability and great significance. BRIEF DESCRIPTION OF THE DRAWINGS

[0061] In order to more clearly illustrate the technical solutions of the embodiments of the present invention, the drawings required for use in the embodiments or the description of the prior art will be briefly introduced below. Obviously, the drawings described below are only some embodiments of the present invention. For ordinary technicians in this field, other drawings can be obtained based on these drawings without paying creative labor.

[0062] Figure 1 This is a structural diagram of the Spark system.

[0063] Figure 2 The diagram is a schematic diagram of the main structure of the system stored in the memory of a specific embodiment of the large-scale high-dimensional data approximate neighbor query system based on Spark of the present invention.

[0064] Figure 3 This is a schematic diagram of the principle of HNSW.

[0065] Figure 4 This is a structural diagram of an index building module of a specific embodiment of the Spark-based large-scale high-dimensional data approximate neighbor query system of the present invention.

[0066] Figure 5 This is a structural diagram of a query module of a specific embodiment of the Spark-based large-scale high-dimensional data approximate neighbor query system of the present invention.

[0067] Figure 6 It is a schematic diagram of the overall structure of a specific embodiment of the large-scale high-dimensional data approximate neighbor query system based on Spark of the present invention.

[0068] Figure 7 The following is a comparison chart of query latency between the two query methods under different data volumes. DETAILED DESCRIPTION

[0069] The technical scheme in the embodiment of the present application will be clearly and completely described below in conjunction with the accompanying drawings in the embodiment of the present application. It should be understood by those skilled in the art that the described embodiment is a part of the embodiment of the present invention, rather than all the embodiments. Based on the embodiment in the present application, those skilled in the art can make any appropriate modifications or variations to obtain all other embodiments.

[0070] In a first aspect, an embodiment of the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark, the system comprising:

[0071] Vector acquisition module, index building module and query module.

[0072] The vector acquisition module is used to acquire the vector to be processed by the system, that is, the data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system.

[0073] The index building blocks include:

[0074] Clustering partitioning module, global index building module and partition index building module.

[0075] The cluster partition module is used to calculate the centroids of m partitions of the data set, and divide the data set into the m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, m∈N.

[0076] The global index building module is used to construct a global index in the master node of the system; the global index building module includes: a data sampling unit, a data marking unit and an indexing unit; the data sampling unit is used to uniformly sample n vectors to be processed, i.e., the points, from each partition according to the data set and the resource conditions of the master node, to form sampling data, and the distribution of the vectors to be processed in the partition is represented by the sampling data; the data marking unit is used to mark the partition label of the partition where the sampled data is located; the indexing unit is used to use the sampled data to build a global index in the master node, and save the global index in the memory; the index structure of the global index adopts HNSW.

[0077] The partition index construction module is used to create a partition index for each partition and store the partition index in the memory; the index structure of the partition index adopts the HNSW.

[0078] The query module includes:

[0079] Query initiation module, global query module, partition query module and sorting module.

[0080] The query initiation module is used to initiate a query Q; the query Q specifies a query vector, the number of partitions to be queried s, and the number of result vectors k; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N.

[0081] The global index query module is used to search the p sampling data closest to the point represented by the query vector in the global index to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front column with a non-zero number of preliminary result vectors to become the partitions to be queried; p≥s, p∈N.

[0082] When the number of the partitions selected by the global index query module is less than s, s can be assigned a value according to the actual number of the partitions to be queried, and the partitions to be queried can be queried; or the number of the preliminary result vectors can be increased, that is, the value of p is increased, until the s partitions to be queried are selected.

[0083] The partition query module is used to query the point in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector.

[0084] The sorting module is used to sort the s×k partition result vectors obtained after the partition query module has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

[0085] In this embodiment, the system includes a vector acquisition module, an index construction module and a query module. Figure 2 The figure shows a schematic diagram of the main structure of the system stored in the memory. The vector acquisition module is used to obtain the vectors to be processed by the system, that is, the data set to be processed. The data contained in the data set to be processed are all vector data, including a large amount of vector data converted from unstructured data such as pictures, voices, and texts.

[0086] HNSW is a graph-based approximate neighbor search method. HNSW introduces a hierarchical structure based on NSW. The idea is similar to the skip list structure in one-dimensional space. The edges are layered, and the average degree of each vertex in the layer is a constant. The specific structure of HNSW is as follows: Figure 3 As shown in Figure 1. In HNSW, the bottom graph contains all vector data, and the vector data of the upper graph decreases as the level increases. The upper data will be included in the bottom graph. When querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer. Figure 3 In the example, point A is the query vector, and point B is the second-level result vector found in the second level; point B is the starting point of the first level, and the search continues until point C is the first-level result vector; point C is the starting point of the 0th level, and the search continues until point D is the 0th-level result vector, which is the result vector of all vectors included in the index. HNSW is used to build partitioned indexes, which implements the function of approximate neighbor query in the Spark system.

[0087] The index construction is divided into three steps: cluster partitioning, partition index construction, and global index construction. Figure 4 shown.

[0088] The goal of the cluster partitioning stage is to partition a data set into m different partitions so that the vectors to be processed in each partition are isomorphic. For example, the minimum Euclidean distance means that the points in each partition are close to each other. In the cluster partitioning process, the k-means method is used to calculate the m partition centroids of the data set, and a data set is partitioned into different partitions so that the formed partitions are isomorphic. The partition index construction stage is used to create a local partition index for each partition and save the partition index in memory. The index structure of the partition index adopts HNSW.

[0089] The global index is constructed as Figure 5, refers to constructing a global index on the master node of the Spark cluster. The construction of a global index includes three steps: data sampling, data labeling, and indexing. First, based on the amount of data and the resources of the master node, a certain proportion of data is sampled from different partitions, and then the sampled data is labeled with the partition in which it is located. For example, if the number of partitions is N, the label number is 0 to (N-1). Because it is randomly sampled, these data can largely represent the data distribution, data quantity, and other information of the partition in which they are located. Next, a global index is further established based on the sampled data.

[0090] The purpose of establishing a global index is to effectively prune irrelevant partitions as much as possible, reasonably reduce the number of partitions to be queried, thereby saving more CPU resources, reducing network costs, and improving query throughput. But on the other hand, it will lead to additional space and time overhead. In order to achieve a balance between the two, the global index should have the following characteristics: efficient space efficiency, fast retrieval, and strong pruning capabilities. Partition pruning is a huge advantage brought by clustering partitions, but it is not completely reasonable to select the partition to be accessed only by considering the distance between the query vector and the cluster center. Although the vectors of each partition are similar after clustering, the center of the cluster cannot fully represent the distribution of the partition vector. The center of the cluster may be far away from the query vector, but the partition contains several results similar to the query vector, which will lead to partition pruning errors. When building a global index, the data of each partition has been partitioned by clustering. A small amount of data can be evenly sampled from each partition to ensure that the sampled data can effectively represent the data distribution of the partition vector. Then, the sampled vector is labeled with the partition where it is located, and then the HNSW global index is built on the master node. If the amount of sampled data is too large, there will be too many global indexes, affecting the efficiency of global indexes. However, if the sampled data is too small, the sampled data cannot effectively represent the data distribution of the partition vector, which will make the establishment of the global index meaningless. Therefore, a balance must be struck between the two, so that the sampled data is just large enough to effectively represent the data distribution of the partition vector.

[0091] Accordingly, the index building module includes a cluster partitioning module, a partition index building module and a global index building module, such as Figure 4 shown.

[0092] The query module includes the query initiation module, the global index query module, the partition query module and the sorting module. Figure 5As shown. The query initiation module initiates a query Q after receiving the query vector set by the user. The query vector here may refer to a vector converted from the text entered by the user when searching on an APP (application) such as Taobao, or may be a vector converted from the customer preference inferred by the recommendation program of an APP such as Taobao based on the collected data. The user here refers to the user of the system provided by the present invention, which may be the final user or the client's program. In short, it refers to the designated person, program or device of the system that accepts the query vector. When querying, first search for multiple vectors that are neighbors to the query vector in the global index. Because the global index is also queried based on the global index constructed using HNSW, the aforementioned HNSW method is also used, that is: when querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer, and the partition result vector is obtained. Since similar vectors have been distributed in the same partition through clustering, similar vectors that are usually ranked high are usually in the same partition, so the partition labels of the partitions where the nearest neighbor vector is most likely to be can be obtained. The number s of partitions to be queried and the number k of result vectors can be specified by the user or set in the query initiation module. If set in the module, it can be a default value or a selection logic, and it is sufficient to select a value. When the query range is all partitions, the system takes the longest time and the result will be the most accurate, but the throughput will also decrease accordingly. If the query range is specified according to a certain principle, the query efficiency will be improved while the accuracy of the query result depends on the principle. The partition query module performs queries based on the partition index constructed using HNSW, and also uses the aforementioned HNSW method, that is: when querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer, and obtain the partition result vector. The sorting module is used to sort the s×k partition result vectors obtained after the partition query module has queried s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector. Figure 6 It is a schematic diagram of the overall structure of the system.

[0093] The technical solution of this embodiment mainly performs approximate neighbor queries in memory. First, cluster partitions are performed according to the similarity of vectors, and each cluster partition corresponds to a partition of the Spark elastic distributed data set. The data of each partition is proportionally sampled and labeled with the partition. The sampled data establishes the global index on the master node, and the partition index is established on the corresponding partition. When querying, the corresponding partitions that need to be queried are found through the global index, and then the results of each partition are summarized and sorted to obtain the final result. The technical solution of the present invention provides a highly scalable distributed approximate neighbor query solution based on the Spark system, while achieving the characteristics of low latency and high throughput.

[0094] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of sampled data, that is, the value of n, can be set by the user.

[0095] In this embodiment, the number of sampled data, that is, the value of n, can be set by the user. Because the user will be more clear about his or her own quantity and data distribution, opening the setting authority to the user can meet the user's needs for more powerful custom queries.

[0096] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of preliminary result vectors can also be set by the user.

[0097] In this embodiment, the number of the preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of the preliminary result vectors can also be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for customized queries with greater authority.

[0098] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of partitions to be queried, that is, the value of s, can be set by the user.

[0099] In this embodiment, the number of partitions to be queried, that is, the value of s, can be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for more powerful custom queries.

[0100] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query system based on Spark: the number of partitions, that is, the value of m, can be set by the user.

[0101] In this embodiment, the number of partitions, that is, the value of m, can be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for customized queries with greater authority.

[0102] In a second aspect, an embodiment of the present invention proposes a large-scale high-dimensional data approximate neighbor query method based on Spark, the method comprising:

[0103] Vector acquisition, index building and query;

[0104] The vector acquisition refers to acquiring a vector to be processed by the system, that is, a data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system;

[0105] The index build includes:

[0106] Cluster partitioning, global index building and partition index building;

[0107] The cluster partitioning refers to calculating the centroids of m partitions of the data set, dividing the data set into m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, m∈N;

[0108] The global index construction refers to constructing a global index in the master node of the system; the global index construction includes: data sampling, data labeling and indexing; the data sampling is to uniformly sample n vectors to be processed from each partition according to the data set and the resource conditions of the master node, that is, the points, to become sampled data, and the distribution of the vectors to be processed in the partition is represented by the sampled data; the data labeling is to label the sampled data with the partition label of the partition; the indexing is to use the sampled data to construct a global index in the master node and save the global index in the memory; the index structure of the global index adopts HNSW;

[0109] The partition index construction refers to creating a partition index for each partition and storing the partition index in the memory; the index structure of the partition index adopts the HNSW;

[0110] The query includes:

[0111] Query initiation, global query, partition query and sorting;

[0112] The query is initiated by initiating a query Q; the query Q specifies the query vector, the number of partitions to be queried s, and the number of result vectors k, and the total number of partitions is m; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N;

[0113] The global index query is to search the global index for the p sampled data closest to the point represented by the query vector to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front column with a non-zero number of the preliminary result vectors as the partition to be queried; p≥s, p∈N;

[0114] When the number of partitions selected by the global index query is less than s,

[0115] The value of s can be assigned according to the actual number of the partitions to be queried, and the partitions to be queried can be queried;

[0116] The number of the preliminary result vectors may also be increased, that is, the value of p is increased, until the s partitions to be queried are selected;

[0117] The partition query is to query the point in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector;

[0118] The sorting is to sort the s×k partition result vectors obtained after the partition query module has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

[0119] In this embodiment, the system includes a vector acquisition module, an index construction module and a query module. Figure 2 The figure shows a schematic diagram of the main structure of the system stored in the memory. The vector acquisition module is used to obtain the vectors to be processed by the system, that is, the data set to be processed. The data contained in the data set to be processed are all vector data, including a large amount of vector data converted from unstructured data such as pictures, voices, and texts.

[0120] HNSW is a graph-based approximate neighbor search method. HNSW introduces a hierarchical structure based on NSW. The idea is similar to the skip list structure in one-dimensional space. The edges are layered, and the average degree of each vertex in the layer is a constant. The specific structure of HNSW is as follows: Figure 3 As shown in Figure 1. In HNSW, the bottom graph contains all vector data, and the vector data of the upper graph decreases as the level increases. The upper data will be included in the bottom graph. When querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer. Figure 3 In the example, point A is the query vector, and point B is the second-level result vector found in the second level; point B is the starting point of the first level, and the search continues until point C is the first-level result vector; point C is the starting point of the 0th level, and the search continues until point D is the 0th-level result vector, which is the result vector of all vectors included in the index. HNSW is used to build partitioned indexes, which implements the function of approximate neighbor query in the Spark system.

[0121] The index construction is divided into three steps: cluster partitioning, partition index construction, and global index construction. Figure 4 shown.

[0122] The goal of the cluster partitioning stage is to partition a data set into m different partitions so that the vectors to be processed in each partition are isomorphic. For example, the minimum Euclidean distance means that the points in each partition are close to each other. In the cluster partitioning process, the k-means method is used to calculate the m partition centroids of the data set, and a data set is partitioned into different partitions so that the formed partitions are isomorphic. The partition index construction stage is used to create a local partition index for each partition and save the partition index in memory. The index structure of the partition index adopts HNSW.

[0123] The global index construction refers to constructing a global index on the master node of the Spark cluster. The global index construction includes three steps: data sampling, data labeling, and indexing. First, based on the amount of data and the master node resources, a certain proportion of data is sampled from different partitions, and then the sampled data is labeled with the partition in which it is located. For example, if the number of partitions is N, the label number is 0 to (N-1). Because it is randomly sampled, these data can largely represent the data distribution, data quantity, and other information of the partition in which they are located. Next, a global index is further established based on the sampled data.

[0124] The purpose of establishing a global index is to effectively prune irrelevant partitions as much as possible, reasonably reduce the number of partitions to be queried, thereby saving more CPU resources, reducing network costs, and improving query throughput. But on the other hand, it will lead to additional space and time overhead. In order to achieve a balance between the two, the global index should have the following characteristics: efficient space efficiency, fast retrieval, and strong pruning capabilities. Partition pruning is a huge advantage brought by clustering partitions, but it is not completely reasonable to select the partition to be accessed only by considering the distance between the query vector and the cluster center. Although the vectors of each partition are similar after clustering, the center of the cluster cannot fully represent the distribution of the partition vector. The center of the cluster may be far away from the query vector, but the partition contains several results similar to the query vector, which will lead to partition pruning errors. When building a global index, the data of each partition has been partitioned by clustering. A small amount of data can be evenly sampled from each partition to ensure that the sampled data can effectively represent the data distribution of the partition vector. Then, the sampled vector is labeled with the partition where it is located, and then the HNSW global index is built on the master node. If the amount of sampled data is too large, there will be too many global indexes, affecting the efficiency of global indexes. However, if the sampled data is too small, the sampled data cannot effectively represent the data distribution of the partition vector, which will make the establishment of the global index meaningless. Therefore, a balance must be struck between the two, so that the sampled data is just large enough to effectively represent the data distribution of the partition vector.

[0125] Accordingly, the index building module includes a cluster partitioning module, a partition index building module and a global index building module, such as Figure 4 shown.

[0126] The query module includes the query initiation module, the global index query module, the partition query module and the sorting module. Figure 5As shown. The query initiation module initiates a query Q after receiving the query vector set by the user. The query vector here may refer to a vector converted from the text entered by the user when searching on an APP (application) such as Taobao, or may be a vector converted from the customer preference inferred by the recommendation program of an APP such as Taobao based on the collected data. The user here refers to the user of the system provided by the present invention, which may be the final user or the client's program. In short, it refers to the designated person, program or device of the system that accepts the query vector. When querying, first search for multiple vectors that are neighbors to the query vector in the global index. Because the global index is also queried based on the global index constructed using HNSW, the aforementioned HNSW method is also used, that is: when querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer, and the partition result vector is obtained. Since similar vectors have been distributed in the same partition through clustering, similar vectors that are usually ranked high are usually in the same partition, so the partition labels of the partitions where the nearest neighbor vector is most likely to be can be obtained. The number s of partitions to be queried and the number k of result vectors can be specified by the user or set in the query initiation module. If set in the module, it can be a default value or a selection logic, and it is sufficient to select a value. When the query range is all partitions, the system takes the longest time and the result will be the most accurate, but the throughput will also decrease accordingly. If the query range is specified according to a certain principle, the query efficiency will be improved while the accuracy of the query result depends on the principle. The partition query module performs queries based on the partition index constructed using HNSW, and also uses the aforementioned HNSW method, that is: when querying, the nearest neighbor queried in the previous layer is used as the starting point of the next layer to start the query, all the way to the bottom layer, and obtain the partition result vector. The sorting module is used to sort the s×k partition result vectors obtained after the partition query module has queried s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector. Figure 6 This is a schematic diagram of the overall structure of the system.

[0127] The technical solution of this embodiment mainly performs approximate neighbor queries in memory. First, cluster partitions are performed according to the similarity of vectors, and each cluster partition corresponds to a partition of the Spark elastic distributed data set. The data of each partition is proportionally sampled and labeled with the partition. The sampled data establishes the global index on the master node, and the partition index is established on the corresponding partition. When querying, the corresponding partitions that need to be queried are found through the global index, and then the results of each partition are summarized and sorted to obtain the final result. The technical solution of the present invention provides a highly scalable distributed approximate neighbor query solution based on the Spark system, while achieving the characteristics of low latency and high throughput.

[0128] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of sampled data, that is, the value of n, can be set by the user.

[0129] In this embodiment, the number of sampled data, that is, the value of n, can be set by the user. Because the user will be more clear about his or her own quantity and data distribution, opening the setting authority to the user can meet the user's needs for more powerful custom queries.

[0130] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of preliminary result vectors can also be set by the user.

[0131] In this embodiment, the number of the preliminary result vectors, that is, the value of p, can be set by the user; the increase rate of the number of the preliminary result vectors can also be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for customized queries with greater authority.

[0132] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of partitions to be queried, that is, the value of s, can be set by the user.

[0133] In this embodiment, the number of partitions to be queried, that is, the value of s, can be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for more powerful custom queries.

[0134] In some embodiments, the present invention provides a large-scale high-dimensional data approximate neighbor query method based on Spark: the number of partitions, that is, the value of m, can be set by the user.

[0135] In this embodiment, the number of partitions, that is, the value of m, can be set by the user. Because the user will be more clear about their own quantity and data distribution, opening the setting authority to the user can meet the user's needs for more powerful custom queries.

[0136] After using the technical solution provided by the present invention, the experimental results also fully demonstrate that the technical solution provided by the present invention can simultaneously achieve the characteristics of low latency and high throughput.

[0137] The software versions used in the experiment are: Spark version 2.3.0, Scala version 2.11.8, OpenJDk 64-Bit Server Vk, Java 1.8.0_232, and the operating system version is Ubuntu 18.04.5LTS.

[0138] The machine configuration used in the experiment is: AMD Ryzen 7 5800H 8core, 16GB, Spark local mode.

[0139] The experimental dataset is the SIFT dataset, which contains 1 billion vector sets and is used to evaluate the approximate nearest neighbor search method.

[0140] The methods for effect comparison are as follows: Query I is a brute force search query, that is, the data is randomly partitioned, and no index is established for the partitions. When querying, heap sort is used for each partition to obtain the most similar vector, and then the results of each partition are collected and sorted to find the final result; Query II is the technical solution proposed in this paper, namely cluster partitioning + global index + partition index query, that is, the data is distributed to each partition after cluster partitioning, and the partition HNSW index is established on the corresponding partition, and the global index is established on the master node. When querying, the corresponding partitions that need to be queried are found through the global index, and each partition uses the HNSW index to query similar results, and then the results of each partition are summarized and sorted to get the final result.

[0141] The parameters used in the experiment are: there are 10 cluster partitions in total, 10 partitions to be queried in query I, and 3 partitions to be queried in query II.

[0142] Figure 7 When the number of query vectors is 500 and the number of base vectors is 100,000, 200,000, and 400,000 respectively, the time taken by the two methods to query 500 query vectors is maintained at 90%. Figure 7It shows that Query II greatly improves the system throughput compared with Query I. Using the technical solution provided by the present invention, high-throughput, low-latency, and scalable approximate neighbor query can be achieved. By sacrificing the accuracy acceptable to the application, additional acceleration is obtained, and the query throughput is greatly improved, which fully demonstrates the effectiveness of the technical solution provided by the present invention.

Claims

1. A large-scale high-dimensional data approximate neighbor query system based on Spark, characterized in that: The system comprises: Vector acquisition module, index building module and query module; The vector acquisition module is used to acquire the vector to be processed by the system, that is, the data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system; The index building module includes: Clustering partitioning module, global index building module and partition index building module; The cluster partition module is used to calculate the centroids of m partitions of the data set, and divide the data set into the m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, m∈N; The global index building module is used to construct a global index in the master node of the system; the global index building module includes: a data sampling unit, a data marking unit and an indexing unit; the data sampling unit is used to uniformly sample n vectors to be processed, i.e., the points, from each partition according to the data set and the resource conditions of the master node, to form sampling data, and the distribution of the vectors to be processed in the partition is represented by the sampling data; the data marking unit is used to mark the partition label of the partition where the sampled data is located; the indexing unit is used to use the sampled data to build a global index in the master node and save the global index in the memory; the index structure of the global index adopts HNSW; The partition index construction module is used to create a partition index for each partition and store the partition index in the memory; the index structure of the partition index adopts the HNSW; The query module comprises: Query initiation module, global query module, partition query module and sorting module; The query initiation module is used to initiate a query Q; the query Q specifies a query vector, the number of partitions to be queried s, and the number of result vectors k; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N; The global index query module is used to search the global index for the p sampling data closest to the point represented by the query vector to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front column with a non-zero number of the preliminary result vectors as the partitions to be queried; p≥s, p∈N; When the number of the partitions selected by the global index query module is less than s, The s may be assigned a value according to the actual number of the partitions to be queried, and the partitions to be queried may be queried; The number of the preliminary result vectors may also be increased, that is, the value of p is increased, until the s partitions to be queried are selected; The partition query module is used to query the points in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector; The sorting module is used to sort the s×k partition result vectors obtained after the partition query module has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

2. The system according to claim 1, characterized in that The number of the sampled data, that is, the value of n, is set by the user.

3. The system according to claim 1, characterized in that The number of the preliminary result vectors, that is, the value of p, is set by the user; the increase rate of the number of the preliminary result vectors is also set by the user.

4. The system according to any one of claims 1 to 3, characterized in that: The number of partitions to be queried, that is, the value of s, is set by the user.

5. The system according to any one of claims 1 to 3, characterized in that: The number of partitions, that is, the value of k, is set by the user.

6. A method for implementing a large-scale high-dimensional data approximate neighbor query system based on Spark, characterized in that: The method comprises: Vector acquisition, index building and query; The vector acquisition refers to acquiring the vector to be processed by the system, that is, the data set to be processed, including the vector to be processed converted from the unstructured data to be processed; one vector to be processed can be regarded as a point in the system; The index building includes: Cluster partitioning, global index building and partition index building; The cluster partitioning refers to calculating the centroids of m partitions of the data set, dividing the data set into the m different partitions, so that the vectors to be processed in each partition are isomorphic, that is, the points in each partition are close to each other; m≥2, m∈N; The global index construction refers to constructing a global index on the master node of the system; the global index construction includes: data sampling, data labeling and indexing; the data sampling is to uniformly sample n vectors to be processed, i.e., points, from each partition according to the data set and the resource conditions of the master node, and the points become sampling data, and the distribution of the vectors to be processed in the partition is represented by the sampling data; the data labeling is to label the partition of the partition to which the sampled data belongs; the indexing is to use the sampled data to construct a global index on the master node, and save the global index in the memory; the index structure of the global index adopts HNSW; The partition index construction refers to creating a partition index for each partition and storing the partition index in the memory; the index structure of the partition index adopts the HNSW; The query includes: Query initiation, global query, partition query and sorting; The query initiation is to initiate a query Q; the query Q specifies a query vector, the number of partitions to be queried s, and the number of result vectors k; the query vector is set by the user; 1≤s≤m, s∈N; k≥1, k∈N; The global index query is to search the global index for the p sampled data closest to the point represented by the query vector to obtain a preliminary result vector, and count the number of the preliminary result vectors contained in each partition according to the partition label, sort them in order from most to least, and select the s partitions in the front row with a non-zero number of the preliminary result vectors as the partitions to be queried; p≥s, p∈N; When the number of the partitions selected by the global index query is less than the s number, The s may be assigned a value according to the actual number of the partitions to be queried, and the partitions to be queried may be queried; The number of the preliminary result vectors may also be increased, that is, the value of p is increased, until the s partitions to be queried are selected; The partition query is to query the points in the s partitions to be queried, and obtain the k points in each of the partitions to be queried that are closest to the query vector, that is, the partition result vector; The sorting is to sort the s×k partition result vectors obtained after the partition has queried the s partitions to be queried, and select the k points closest to the point represented by the query vector to obtain the result vector.

7. The method according to claim 6, characterized in that The number of the sampled data, that is, the value of n, is set by the user.

8. The method according to claim 6, characterized in that The number of the preliminary result vectors, that is, the value of p, is set by the user; the increase rate of the number of the preliminary result vectors is also set by the user.

9. The method according to any one of claims 6 to 8, characterized in that: The number of partitions to be queried, that is, the value of s, is set by the user.

10. The method according to any one of claims 6 to 8, characterized in that: The number of partitions, that is, the value of m, is set by the user.