Vector retrieval method for database and computer program product
By evaluating the directional consistency between neighbor vector points and query vector points and dynamically adjusting the threshold, the processing of redundant neighbor points is reduced, thus solving the problem of high distance calculation overhead in the HNSW retrieval algorithm and improving the retrieval efficiency and performance of the database.
Patent Information
- Application Number
- CN202511081683.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2025-08-01
- Publication Date
- 2025-11-18
AI Technical Summary
The existing HNSW retrieval algorithm suffers from high distance calculation overhead due to its greedy strategy, resulting in low retrieval efficiency and decreased database performance.
By evaluating the consistency between the current access vector point and its neighbor vector points, neighbor vector points with a consistency level greater than or equal to a set threshold are included in the candidate set. The search process is guided by directional information, reducing redundant neighbor point processing and distance calculation. Dynamic threshold adjustment is used to improve search efficiency.
This effectively reduces the overhead of processing redundant neighbor vector points and calculating distances, thereby improving the retrieval efficiency and performance of the database.
Smart Images

Figure CN120973808A_ABST
Abstract
Description
TECHNICAL FIELD
[0001] The present application relates to the field of database technology, and in particular, to a vector retrieval method of a database and a computer program product. BACKGROUND
[0002] Vector retrieval technology is one of the core technologies in the fields of modern information retrieval, recommendation system, image recognition, natural language processing, etc., and its core task is to efficiently find the most similar neighbor vectors to the query vector in a large-scale vector database. Among them, the hierarchical navigable small world graph (HNSW) is widely used as an efficient graph index structure due to its good balance between retrieval accuracy and speed. Specifically, HNSW constructs a multi-layer graph structure, each layer is a navigable small world graph, the high layer is sparse and the low layer is dense. When searching, it starts from the top layer and explores layer by layer, and finally performs fine search in the bottom layer graph, so as to realize fast positioning of approximate nearest neighbors.
[0003] At present, in the prior art, the typical HNSW retrieval algorithm starts from the entry point of the top layer of the graph index structure, moves along the graph edge to the direction of the query vector step by step, selects the nearest neighbor node to the query vector at each step, and finally reaches the bottom layer graph. And in each layer, a greedy strategy is executed to find the nearest neighbor vector. However, the greedy strategy will calculate the distance of each point on the query path as much as possible, which will bring huge distance calculation overhead, and further lead to low retrieval efficiency and low database working performance. SUMMARY
[0004] An object of the present application is to provide a vector retrieval method of a database and a computer program product capable of solving at least one of the above-mentioned defects in the prior art.
[0005] A further object of the present application is to improve the retrieval efficiency and working performance of the database.
[0006] In particular, the present application provides a vector retrieval method of a database, comprising:
[0007] Obtaining a query vector point and a hierarchical navigable small world graph index of a database;
[0008] Starting a search process and configuring the search process to access the vector points on the hierarchical navigable small world graph index from the top layer to the bottom layer to find the nearest neighbor vector point of the query vector point;
[0009] In the case that the search process searches to the bottom layer, determining the vector point currently accessed by the search process as a current access vector point, and determining the neighbor vector point adjacent to the current access vector point;
[0010] evaluating a consistency degree between two directions respectively pointed by the current access vector point to the neighbor vector point and the query vector point;
[0011] including the neighbor vector point with the consistency degree greater than or equal to a set threshold into a candidate set of the most adjacent vector points.
[0012] Further, the step of evaluating the consistency degree between two directions respectively pointed by the current access vector point to the neighbor vector point and the query vector point comprises:
[0013] constructing a neighbor direction vector and a query direction vector respectively pointed by the current access vector point to the neighbor vector point and the query vector point;
[0014] calculating a similarity degree of the neighbor direction vector and the query direction vector to obtain the consistency degree.
[0015] Further, the step of calculating the similarity degree of the neighbor direction vector and the query direction vector comprises:
[0016] calculating a cosine similarity degree of the neighbor direction vector and the query direction vector, and using the obtained cosine similarity degree to evaluate the consistency degree.
[0017] Further, before the step of evaluating the consistency degree between two directions respectively pointed by the current access vector point to the neighbor vector point and the query vector point, and after the step of determining the neighbor vector point adjacent to the current access vector point, the vector retrieval method further comprises:
[0018] calculating a distance between the current access vector point and the query vector point to obtain a query distance value;
[0019] calculating the set threshold based on the query distance value, wherein the set threshold is configured to decrease with the decrease of the query distance value.
[0020] Further, before the step of calculating the set threshold based on the query distance value, and after the step of calculating the distance between the access vector point and the query vector point, the vector retrieval method further comprises:
[0021] calculating an accessed maximum distance value, wherein the accessed maximum distance value is a maximum value among distance values between the query vector point and each vector point accessed by the search process; and
[0022] The step of calculating the set threshold based on the query distance value comprises:
[0023] calculating an attenuation factor of the set threshold according to the query distance value and the accessed maximum distance value;
[0024] The setting threshold is calculated according to the attenuation factor and the preset initial static threshold, wherein the attenuation factor is configured to increase with the increase of the accessed maximum distance value, the attenuation factor is configured to decrease with the increase of the query distance value, and the setting threshold is configured to decrease with the increase of the attenuation factor.
[0025] Further, the relationship among the query distance value, the accessed maximum distance value and the attenuation factor is:
[0026] And,
[0027] The relationship among the attenuation factor, the initial static threshold and the setting threshold is:
[0028] threshold adjusted = threshold - a(d u ) x delta threshold;
[0029] Wherein, delta threshold is a constant that can be adjusted, and is a positive value;
[0030] D max is the accessed maximum distance value;
[0031] d u is the query distance value;
[0032] threshold is the initial static threshold;
[0033] threshold adjusted is the setting threshold.
[0034] Further, before the step of calculating the setting threshold based on the query distance value, after the step of calculating the accessed maximum distance value, the vector retrieval method further comprises:
[0035] Judging whether the query distance value is greater than or equal to the accessed maximum distance value;
[0036] If yes, the accessed maximum distance value is assigned as the query distance value.
[0037] Further, when performing the step of including the neighbor vector point with the consistency degree greater than or equal to the setting threshold into the candidate set of the most adjacent vector point, the vector retrieval method further comprises:
[0038] Pruning the neighbor vector point with the consistency degree less than the setting threshold; and
[0039] After the step of including the neighbor vector point with the consistency degree greater than or equal to the setting threshold into the candidate set of the most adjacent vector point, the vector retrieval method further comprises:
[0040] determine the neighbor vector point closest to the query vector point in the candidate set, and configure the search process to access the neighbor vector point;
[0041] return to the step of determining the vector point currently accessed by the search process as the current access vector point.
[0042] Further, before the step of determining the vector point currently accessed by the search process as the current access vector point, and in the case that the current access vector point is the entry point of the search process at the bottom layer, the vector retrieval method further comprises:
[0043] obtaining the number of vector points to be accessed by the search process at the bottom layer;
[0044] determining whether the number is greater than a preset threshold to be accessed;
[0045] if yes, performing the step of determining the vector point currently accessed by the search process as the current access vector point;
[0046] if no, configuring the search process to search according to the cluster search strategy.
[0047] In particular, the present application also provides a computer program product comprising a computer program, which, when executed by a processor, implements the above-mentioned vector retrieval method of a database.
[0048] The vector retrieval method of a database of the present application can, in the case that the search process searches to the bottom layer, evaluate the consistency degree between the two directions respectively pointed by the current access vector point to the neighbor vector point and the query vector point, and include the neighbor vector point with a consistency degree greater than or equal to a set threshold into the candidate set of the nearest neighbor vector point, and then use the directional information of the overall search direction from the current access vector point to the query vector point to guide the search process of the search process, so as to exclude the neighbor vector point obviously deviating from the search direction, and include the neighbor vector point roughly consistent with the search direction into the candidate set, which can effectively reduce the processing and distance calculation overhead of redundant neighbor vector points, avoid unnecessary calculation on irrelevant vector points, quickly guide the search process to access in the correct direction, and improve the search efficiency. Therefore, the vector retrieval method of a database of the present application can effectively improve the retrieval efficiency and working performance of the database.
[0049] The computer program product of the present application can implement the above-mentioned vector retrieval method of a database, and thus has the beneficial technical effects of the above-mentioned vector retrieval method of a database.
[0050] The above and other objects, advantages and features of the present application will become more apparent from the following detailed description of specific embodiments thereof, taken in conjunction with the accompanying drawings. BRIEF DESCRIPTION OF DRAWINGS
[0051] Some specific embodiments of the present application will be described in detail in the following with reference to the attached drawings. The same reference numerals in the drawings denote the same or similar components or parts. Those skilled in the art will appreciate that the drawings are not necessarily drawn to scale. In the drawings:
[0052] Figure 1 is a flowchart of a vector retrieval method of a database according to an embodiment of the present application;
[0053] Figure 2 is a flowchart of a process of calculating a set threshold in a vector retrieval method of a database according to an embodiment of the present application;
[0054] Figure 3 is a flowchart of a vector retrieval method of a database according to another embodiment of the present application;
[0055] Figure 4 is a flowchart of a process of calculating a consistency degree in a vector retrieval method of a database according to another embodiment of the present application;
[0056] Figure 5 is a flowchart of a process of assigning a visited maximum distance value in a vector retrieval method of a database according to another embodiment of the present application;
[0057] Figure 6 is a flowchart of a vector retrieval method of a database according to another embodiment of the present application;
[0058] Figure 7 is a flowchart of a vector retrieval method of a database according to another embodiment of the present application;
[0059] Figure 8 is a flowchart of a search process of a search progress on a hierarchical navigation small world graph index in a vector retrieval method of a database according to another embodiment of the present application;
[0060] Figure 9 is a position structure diagram of a query point q, a current node u and a neighbor node v in a vector retrieval method of a database according to another embodiment of the present application;
[0061] Figure 10 is a flowchart of a search process of a search progress on a lowest layer in a vector retrieval method of a database according to another embodiment of the present application;
[0062] Figure 11 is a structural block diagram of a computer program product according to an embodiment of the present application;
[0063] Figure 12is a structural block diagram of a computer readable storage medium according to an embodiment of the present application;
[0064] Figure 13 is a structural block diagram of a computer device according to an embodiment of the present application. DETAILED DESCRIPTION
[0065] In the description of the present embodiments, it is to be understood that the meaning of "a plurality" is at least two, for example, two, three, and the like, unless explicitly and specifically limited otherwise. When a certain feature "includes", or "comprises" a certain or certain encompassing feature, unless otherwise specifically described, it indicates that other features can be further included, and can further include other features.
[0066] Unless otherwise explicitly specified and limited, the term "connection" and the like should be understood in a broad sense, for example, can be fixed connection, or detachable connection, or integral; can be mechanical connection, or electrical connection; can be directly connected, or indirectly connected through an intermediate medium, can be internal communication of two elements or interaction relationship between two elements, unless otherwise explicitly limited. Those skilled in the art should be able to understand the specific meaning of the above terms in the present application according to the specific circumstances.
[0067] Unless otherwise limited, all terms (including technical terms and scientific terms) used in the description of the present embodiments have the same meaning as generally understood by those skilled in the art to which the present application belongs.
[0068] In the description of the present embodiments, the description of the terms "the present embodiments", "some other embodiments", and the like means that the specific features, structures, materials or characteristics described in conjunction with the embodiments or examples are included in at least one embodiment or example of the present application. In the present specification, the illustrative description of the above terms does not necessarily refer to the same embodiment or example. Moreover, the specific features, structures, materials or characteristics described can be combined in any one or more embodiments or examples in a suitable manner.
[0069] The database vector retrieval method of the present embodiments will be described in detail below in conjunction with Figures 1 to 10 The database vector retrieval method of the present embodiments will be described in detail below in conjunction with
[0070] Figure 1 is a flowchart of a database vector retrieval method according to an embodiment of the present application. Referring to Figure 1 In the present embodiments, the database vector retrieval method can include:
[0071] In step S101, a query vector point and a hierarchical navigation small world graph index of a database are acquired.
[0072] Step S102, start the search process, and configure the search process to access the vector points on the most top layer to the most bottom layer of the hierarchical navigation small world graph index to find the most adjacent vector points of the query vector point.
[0073] Step S103, in the case that the search process searches to the most bottom layer, determine the vector point currently accessed by the search process as the current access vector point, and determine the neighbor vector points adjacent to the current access vector point.
[0074] Step S104, evaluate the consistency degree between the two directions respectively pointed by the current access vector point to the neighbor vector points and the query vector point. This step can include steps S311 to S312 of the following embodiment.
[0075] Step S105, include the neighbor vector points with the consistency degree greater than or equal to the set threshold value into the candidate set of the most adjacent vector points.
[0076] Since the vector retrieval method of the database of the embodiment can evaluate the consistency degree between the two directions respectively pointed by the current access vector point to the neighbor vector points and the query vector point in the case that the search process searches to the most bottom layer, and include the neighbor vector points with the consistency degree greater than or equal to the set threshold value into the candidate set of the most adjacent vector points, the search process of the search process can be guided by the direction information of the overall search direction from the current access vector point to the query vector point, so as to exclude the neighbor vector points obviously deviating from the search direction, and include the neighbor vector points roughly consistent with the search direction into the candidate set, which can effectively reduce the processing and distance calculation overhead of redundant neighbor vector points, avoid unnecessary calculation on irrelevant vector points, quickly guide the search process to access in the correct direction, and improve the search efficiency. Therefore, the vector retrieval method of the database of the embodiment can effectively improve the retrieval efficiency and working performance of the database.
[0077] Reference Figure 2 In the embodiment, before the step of evaluating the consistency degree between the two directions respectively pointed by the current access vector point to the neighbor vector points and the query vector point, and after the step of determining the neighbor vector points adjacent to the current access vector point, i.e., after step S103 before step S104, the vector retrieval method further includes:
[0078] Step S201, calculate the distance between the current access vector point and the query vector point to obtain a query distance value.
[0079] Step S202, calculate the set threshold value based on the query distance value, wherein the set threshold value is configured to decrease with the decrease of the query distance value. This step can include steps S309 to S310 of the following embodiment.
[0080] Since the threshold value in the vector retrieval method of the database of the embodiment is a dynamic threshold value, which varies according to the change of the query distance value, specifically, the threshold value can decrease with the decrease of the query distance value, and in the search process of the search progress, for example, from the top layer to the bottom layer of the graph index structure, as the vector points accessed by the search progress are closer and closer to the query vector point, the query distance value is gradually reduced, and the threshold value is also gradually reduced. That is, in the search process of the search progress, the consistency requirement of the neighbor vector points that can be put into the candidate set is more and more relaxed, so as to ensure that the search progress can add fewer vector points to the candidate set in the early stage of the search to quickly locate the vector points close to the nearest neighbor vector point, and in the later stage of the search, the situation that the nearest neighbor vector point is missed can be avoided, and the risk of missing detection is reduced. Therefore, the vector retrieval method of the database of the embodiment can improve the retrieval efficiency and working performance of the database while ensuring the accuracy of the direction of the guidance of the search progress and ensuring the retrieval effect.
[0081] Figure 3 is a flowchart of a vector retrieval method of a database according to yet another embodiment of the application. Referring to Figure 3 In the embodiment, the vector retrieval method of the database can include:
[0082] Step S301, obtaining a query vector point and a hierarchical navigation small-world graph index of a database.
[0083] Step S302, starting a search progress, and configuring the search progress to access the vector points on the hierarchical navigation small-world graph index from the top layer to the bottom layer, to find the nearest neighbor vector point of the query vector point.
[0084] Step S303, in the case that the current access vector point is the entry point of the search progress in the bottom layer, obtaining the number of vector points to be accessed by the search progress on the bottom layer; steps S303 to S305 can be performed before step S306 is performed.
[0085] Step S304, determining whether the number is greater than a preset threshold value to be accessed; if yes, performing the step of determining that the vector point currently accessed by the search progress is the current access vector point, that is, performing step S306; if not, step S305 can be performed.
[0086] Step S305, configuring the search progress to search according to the cluster search strategy. After this step is performed and the nearest neighbor vector point is found, the vector retrieval method of the database of the embodiment ends.
[0087] Step S306, determining that the vector point currently accessed by the search progress is the current access vector point, and determining the neighbor vector points adjacent to the current access vector point.
[0088] Step S307, the distance between the current access vector point and the query vector point is calculated to obtain a query distance value.
[0089] Step S308, a visited maximum distance value is calculated; wherein the visited maximum distance value is the maximum value among the distance values between the query vector point and each vector point visited by the search process; and this step can be executed after the step of calculating the distance between the access vector point and the query vector point, i.e., after step S307 and before step S309.
[0090] Step S309, a decay factor of the set threshold value is calculated according to the query distance value and the visited maximum distance value.
[0091] Step S310, the set threshold value is calculated according to the decay factor and a preset initial static threshold value, wherein the decay factor is configured to increase with the increase of the visited maximum distance value, the decay factor is configured to decrease with the increase of the query distance value, and the set threshold value is configured to decrease with the increase of the decay factor.
[0092] Step S311, neighbor direction vectors and query direction vectors are constructed, which are respectively directed from the current access vector point to the neighbor vector points and the query vector point.
[0093] Step S312, the similarity of the neighbor direction vectors and the query direction vectors is calculated to obtain a consistency degree.
[0094] Step S313, the neighbor vector points with the consistency degree greater than or equal to the set threshold value are included in the candidate set of the most adjacent vector points, and the neighbor vector points with the consistency degree less than the set threshold value are pruned. It should be noted that the step of pruning the neighbor vector points with the consistency degree less than the set threshold value in this embodiment can be executed when the step of including the neighbor vector points with the consistency degree greater than or equal to the set threshold value in the candidate set of the most adjacent vector points is executed, and in some other embodiments, the step of pruning the neighbor vector points with the consistency degree less than the set threshold value can also be executed before or after the step of including the neighbor vector points with the consistency degree greater than or equal to the set threshold value in the candidate set of the most adjacent vector points.
[0095] Step S314, the nearest neighbor vector point to the query vector point in the candidate set is determined, and the search process is configured to access the neighbor vector point; this step can be performed after the step of including the neighbor vector point with the consistency degree greater than or equal to the set threshold in the candidate set of the nearest neighbor vector point, that is, after the step of step S313. And after this step, the step of determining that the vector point currently accessed by the search process is the current access vector point can be returned to execute, that is, step S306 is returned to execute until the nearest neighbor vector point is found, and the vector retrieval method of the database in this embodiment is ended.
[0096] It needs to be understood that steps S311 and S312 can be performed by first constructing the neighbor direction vector and the query direction vector, then calculating the similarity of the neighbor direction vector and the query direction vector, and then calculating the consistency degree, which can further improve the retrieval efficiency and working performance of the database. In addition, in some other embodiments, step S312 can be replaced by calculating the size of the angle between the neighbor direction vector and the query direction vector, and the consistency degree is evaluated by the angle.
[0097] Step S311, when there are multiple neighbor vector points, specifically can be: first select one neighbor vector point, and construct the neighbor direction vector and the query direction vector respectively pointing from the current access vector point to the selected neighbor vector point and the query vector point. And step S313 specifically can be: judging whether the consistency degree is greater than or equal to the set threshold, if yes, including the neighbor vector point in the candidate set of the nearest neighbor vector point, if not, pruning the neighbor vector point; and after the step of including the neighbor vector point in the candidate set of the nearest neighbor vector point and / or the step of pruning the neighbor vector point, and in the case that the current access vector point still has neighbor vector points that have not been judged by the consistency degree (i.e. there are neighbor vector points of the current access vector point that have not been pruned or included in the candidate set), the next neighbor vector point is selected and the step of constructing the neighbor direction vector and the query direction vector respectively pointing from the current access vector point to the selected neighbor vector point and the query vector point is continued to be executed, until all neighbor vector points of the current access vector point have been judged by the consistency degree (i.e. all neighbor vector points of the current access vector point have been pruned or included in the candidate set), then step S314 is executed.
[0098] Steps S307 to S310 can implement the calculation of the set threshold, and wherein the accessed maximum distance value can measure the query distance value at this time, evaluate the size of the query distance at this time, to determine whether to reduce the set threshold, that is, to determine the attenuation factor of the set threshold. Further, the set threshold can be calculated according to the attenuation factor and the preset initial static threshold. Further, steps S307 to S310 can configure the set threshold as a dynamic value that changes according to the change of the query distance value, to ensure the accuracy of the direction of the guidance of the search process while improving the retrieval efficiency and working performance of the database, and ensure the retrieval effect.
[0099] In steps S303 to S305, since the number of vector points to be accessed is small, whether the beam search strategy or the method of directionally guiding the search process in the embodiment or other methods is used, the overall time and computing power consumed are small; while in the case of a large number of vector points to be accessed, the beam search strategy or the greedy strategy will consume a large amount of computing power and retrieval time. Therefore, when the number of vector points to be searched by the search process at the bottom layer is small, that is, less than or equal to the access threshold, the search process can be configured to search according to the beam search strategy; when the number is large, that is, greater than the access threshold, step S306 can be executed to directionally guide the search process, wherein the value range of the access threshold can be greater than or equal to 10, and the access threshold is preferably 20.
[0100] In step S313, the neighbor vector points with a consistency degree less than the set threshold are pruned, and then the subsequent distance calculation of the neighbor vector points with a consistency degree less than the set threshold can be excluded, to achieve the technical effect of improving the retrieval efficiency and working performance of the database.
[0101] Step S314 can configure the search process to continue to access the next vector point to find the most adjacent vector point.
[0102] Reference Figure 4 In the embodiment, the step of calculating the similarity of the neighbor direction vector and the query direction vector can include:
[0103] Step S401, calculate the cosine similarity of the neighbor direction vector and the query direction vector, and use the obtained cosine similarity to evaluate the consistency degree.
[0104] Specifically, the calculation formula of the cosine similarity of the neighbor direction vector and the query direction vector is:
[0105]
[0106] Wherein cosine is the cosine similarity;
[0107] "•" represents vector dot product;
[0108] ||...|| represents L2 norm of vector;
[0109] d uv represents neighbor direction vector;
[0110] d uq represents query direction vector.
[0111] In some other embodiments, the degree of consistency can be evaluated by calculating the dot product of the neighbor direction vector and the query direction vector.
[0112] In the present embodiment, the relationship among the query distance value, the visited maximum distance value and the decay factor can be:
[0113] And,
[0114] The relationship among the decay factor, the initial static threshold and the set threshold can be:
[0115] threshold adjusted = threshold - a(d u ) x delta threshold;
[0116] Wherein, delta threshold is a constant that can be adjusted, which is a positive value, and its value range is from the initial static threshold to 1. For example, 0.1, 0.2, 0.3, 0.4, 0.5;
[0117] D max is the visited maximum distance value.
[0118] d u is the query distance value.
[0119] The threshold is an initial static threshold, and the initial static threshold can range from -1 to 1, preferably from 0 to 1, and preferably has a value of 1. For example, 0, 0.2, 0.5, 1, etc. Moreover, the preset of the initial static threshold can determine the number of neighbor vector points that are included in the candidate set at the beginning of the search for the bottom layer, so as to affect the search efficiency of the search process. When the initial static threshold is preset to -1, it is expected that the search process can include more neighbor vector points in the candidate set at the beginning of the search for the bottom layer, so as to ensure the accuracy of the search method. When the initial static threshold is preset to 1, it is expected that the search process can quickly locate the neighbor vector point between the current access vector point and the query vector point at the beginning of the search for the bottom layer, so as to ensure the search efficiency of the search method. When the initial static threshold is preset to 0, it is expected that the search process can take into account both the accuracy and the search efficiency of the search method at the beginning of the search for the bottom layer.
[0120] threshold adjusted To set the threshold, the smaller the set threshold, the lower the requirement for the cosine similarity between the neighbor direction vector and the query direction vector, that is, the lower the requirement for the consistency between the two directions of the current access vector point pointing to the neighbor vector point and the query vector point. Specifically, when the set threshold is -1, the neighbor vector points added to the candidate set through step S313 are the neighbor vector points whose two directions of the current access vector point pointing to the neighbor vector point and the query vector point are less than or equal to 180°. When the set threshold is 0, the neighbor vector points added to the candidate set through step S313 are the neighbor vector points whose two directions of the current access vector point pointing to the neighbor vector point and the query vector point are less than or equal to 90°. If the set threshold is 1, the threshold will be very strict, and the execution of step S313 will only include the neighbor vector points whose two directions of the current access vector point pointing to the neighbor vector point and the query vector point are completely consistent into the candidate set. Moreover, when the cosine similarity calculated by step S401 is -1, it means that the directions of the neighbor direction vector and the query direction vector are completely opposite, that is, the two directions of the current access vector point pointing to the neighbor vector point and the query vector point are completely opposite. When the cosine similarity calculated by step S401 is 0, it means that the directions of the neighbor direction vector and the query direction vector are perpendicular, that is, the two directions of the current access vector point pointing to the neighbor vector point and the query vector point are perpendicular. When the cosine similarity calculated by step S401 is 1, it means that the directions of the neighbor direction vector and the query direction vector are completely similar, that is, the two directions of the current access vector point pointing to the neighbor vector point and the query vector point are completely consistent.
[0121] Referring to Figure 5In this embodiment, after the step of calculating the maximum visited distance value before or before the step of calculating the set threshold based on the query distance value, the vector retrieval method further includes:
[0122] Step S501: Determine whether the query distance value is greater than or equal to the maximum distance value already visited; if yes, proceed to step S502; if no, proceed to the step of calculating and setting a threshold based on the query distance value or step S309.
[0123] Step S502: Assign the query distance value to the maximum distance value that has been visited.
[0124] It is understandable that during the search process, the query distance value may exceed the maximum distance already visited. Based on the relationship between the query distance value, the maximum distance already visited, and the attenuation factor in the above embodiment, it can be seen that if the query distance value is greater than the maximum distance already visited, the attenuation factor may become negative, which will cause the set threshold to increase as the query distance value increases. Therefore, the configuration of steps S501 and S502 can ensure the stable and normal operation of the vector retrieval method in this embodiment.
[0125] Figure 6 This is one of the flowcharts illustrating a vector retrieval method for a database according to another embodiment of the present invention; Figure 7 This is a second schematic flowchart of a vector retrieval method for a database according to another embodiment of the present invention. (Refer to...) Figure 6 In this embodiment, the vector retrieval method for the database may include:
[0126] Step S601: The search starts from the highest level and proceeds with a greedy descent. The traversal process is as follows: Figure 8 As shown, after reaching the bottom layer, the query starts from the entry point. And points are defined (e.g., ...). Figure 9 As shown): q is the vector of the query point (i.e., the query vector point in the above embodiment), u is the vector of the node currently in the graph search process (i.e., the currently accessed vector point in the above embodiment), and v is the vector representation of a neighboring node of node u (i.e., the neighbor vector point in the above embodiment).
[0127] Step S602: Calculate the distance d between point u and the point q to be queried. u (i.e., the query distance value in the above embodiment), and calculate the maximum distance D between the query point q and each visited vector node. max (i.e., the maximum distance visited in the above embodiments).
[0128] Step S603, determine d u Is it greater than D? max If d u Greater than Dmax then step S604 is executed; if d u is less than or equal to D max , step S605 is executed.
[0129] Step S604, update D max = d u .
[0130] Step S605, determine whether the number of neighbor nodes v not traversed by the node u is greater than 20; if the number of neighbor nodes v not traversed by the node u is ≤20, then step S606 is executed; if the number of neighbor nodes v not traversed by the node u is >20, then step S607 is executed.
[0131] Step S606, search using the original strategy (beam search strategy) based on the HNSW (Hierarchical Navigable Small World) algorithm,
[0132] Step S607, use the direction guiding algorithm to quickly narrow the range. That is, step S701 of the following embodiment is executed.
[0133] Referring to Figure 7 , in this embodiment, the vector retrieval method of the database can further include:
[0134] Step S701, set a direction similarity threshold threshold (i.e., the initial static threshold in the above embodiment, usually threshold ∈ [-1, 1], and preferably [0, 1], and the preferred value is 1, and the common values are, for example, 0, 0.2, 0.5, etc.).
[0135] Step S702, calculate the direction vector d uv = v-u from u to one of the neighbor nodes v, and calculate the direction vector d uq = q-u from u to the query point q.
[0136] Step S703, use the cosine similarity to measure the consistency of the two direction vectors d uv and d uq :
[0137]
[0138] Where “·” represents vector dot product, and ||...|| represents L2 norm of the vector.
[0139] And, calculate threshold adjusted (i.e., the set threshold in the above embodiment):
[0140] An attenuation factor a is introduced, which can be based on the distance of node u to the query point q (i.e. the query distance value d u ) in the above embodiment) to adjust the threshold adjusted When the distance is very close, then the requirement on the direction is relaxed. Specifically, the relationship between the query distance value, D max (i.e. the visited maximum distance value in the above embodiment) and the attenuation factor a can be:
[0141] And,
[0142] The relationship between the attenuation factor a, the threshold and the threshold adjusted can be:
[0143] threshold adjusted = threshold - a(d u ) x At hres hold
[0144] where At hres hold is a constant that can be adjusted, and is positive, and can take a value in the range of threshold to 1, for example 0.1, 0.2, 0.3, 0.4, 0.5.
[0145] Step S704, pruning condition: when the search algorithm explores a neighbor v of node u, before calculating the distance between v and q (e.g. ||v-q||), a direction check is performed. That is, whether cosine(v-u, q-u) is greater than or equal to threshold adjusted is determined. If cosine(v-u, q-u) ≥ threshold adjusted , then it is considered that the direction of neighbor v is roughly consistent with the query direction, and the neighbor is allowed to continue to be processed, and step S705 is executed; if cosine(v-u, q-u) < threshold adjusted , then it is considered that the direction of neighbor v deviates from the query direction far away, and step S706 is executed.
[0146] Step S705, ||v-q|| is calculated and added to the candidate set. After this step, step S707 is executed.
[0147] Step S706, prune the neighbor, and do not perform subsequent processing on it, do not calculate the distance, and do not add it to the candidate set. After this step, step S707 is executed.
[0148] Step S707, determine whether there are more neighbor nodes v of this node u, if yes, select the next neighbor node v, and return to execute step S702; if no, execute subsequent operations, for example, step S314 in the above embodiment can be executed.
[0149] In summary, the lowest-level search process of the vector retrieval method for the database in this embodiment is as follows: Figure 10 As shown, assuming the entry point at the bottom layer is point 2, and point 2's neighboring points are points 1, 3, and 4, the process first determines whether points 1, 3, and 4 are in the same direction as the query vector. If not, they are discarded; if so, distance calculation is performed, and the vector is moved (i.e., the point closest to the query vector among point 2's neighboring points is visited). Specifically, using the dashed line as a boundary, if point 1 is in the opposite direction to the query vector, point 1 is discarded, and points 3 and 4 are retained. The distances between the retained neighboring points of point 2 (points 3 and 4) and the query vector are calculated, and then the closest neighbor is selected to move to (or visit) the next position (e.g., point 3). Furthermore, some steps in the vector retrieval method of the database in this embodiment correspond to the steps in the aforementioned embodiments, and the beneficial technical effects achieved by these steps have been described in detail in the aforementioned embodiments, and will not be repeated here.
[0150] The following is combined Figure 11 The computer program product of this embodiment will be described in detail below. Figure 11 This is a schematic structural block diagram of a computer program product according to an embodiment of the present invention. (Refer to...) Figure 11 In this embodiment, the computer program product 10 includes a computer program 11, which, when executed by a processor, implements the vector retrieval method for the database described in the above embodiment.
[0151] The computer program product 10 of this embodiment can implement the above-described vector retrieval method for databases. Therefore, the computer program product 10 of this embodiment also possesses the beneficial technical effects that the above-described vector retrieval method for databases can achieve.
[0152] The following is combined Figure 12 The computer-readable storage medium of this embodiment will be described in detail below. Figure 12 This is a schematic structural block diagram of a computer-readable storage medium according to an embodiment of the present invention, with reference to... Figure 12 In this embodiment, a computer program 11 is stored on the computer-readable storage medium 20. When the computer program 11 is executed by the processor, it implements the vector retrieval method of the database described in the above embodiment.
[0153] The computer-readable storage medium 20 of this embodiment can implement the vector retrieval method for the database described above. Therefore, the computer-readable storage medium 20 of this embodiment also possesses the beneficial technical effects that can be achieved by the vector retrieval method for the database described above.
[0154] It should be noted that the logic and / or steps represented in the flowchart or otherwise described herein, for example, can be considered as a sequenced list of instructions for implementing logical functions, and can be specifically implemented in any computer-readable storage medium 20 for use by, or in conjunction with, an instruction execution system, apparatus or device (such as a computer-based system, a processor-included system or other system that can fetch and execute instructions from an instruction execution system, apparatus or device).
[0155] For the purposes of this embodiment, the computer-readable storage medium 20 can be any means capable of containing, storing, communicating, propagating, or transmitting a program for use by or in conjunction with an instruction execution system, apparatus, or device. More specific examples (a non-exhaustive list) of computer-readable media include: an electrical connection having one or more wires (electronic device), a portable computer disk drive (magnetic device), random access memory (RAM), read-only memory (ROM), erasable and editable read-only memory (EPROM or flash memory), fiber optic devices, and portable optical disc read-only memory (CDROM). Furthermore, the computer-readable storage medium 20 can even be paper or other suitable media on which a program can be printed, since a program can be obtained electronically, for example, by optically scanning the paper or other medium, followed by editing, interpreting, or otherwise processing as necessary, and then stored in a computer memory.
[0156] It should be understood that various parts of the present invention can be implemented using hardware, software, firmware, or a combination thereof. In the above embodiments, multiple steps or methods can be implemented using software or firmware stored in memory and executed by a suitable instruction execution system.
[0157] The following is combined with Figure 13 The computer device of this embodiment will be described in detail below. Figure 13 This is a structural block diagram of a computer device according to an embodiment of the present invention, with reference to... Figure 13 In this embodiment, the computer device 30 includes a memory 31, a processor 32, and a computer program 11 stored on the memory 31. When the processor 32 executes the computer program 11, it implements the above-described vector retrieval method for the database.
[0158] The computer device 30 of this embodiment can implement the vector retrieval method for the database described in the above embodiment. Therefore, the computer device 30 of this embodiment also possesses the beneficial technical effects that can be achieved by the vector retrieval method for the database described above.
[0159] The computer device 30 can be, for example, a server, a desktop computer, a notebook computer, a tablet computer, or a smart phone. In some examples, the computer device 30 can be a cloud computing node. The computer device 30 can be described in the general context of computer system instructions, such as program modules, being executed by a computer system. Generally, program modules can include routines, programs, objects, components, logic, data structures, and so on, that perform particular tasks or implement particular abstract data types. The computer device 30 can be practiced in distributed cloud computing environments where tasks are performed by remote processing devices that are linked through a communications network. In a distributed cloud computing environment, program modules can be located in local or remote computer system storage media including memory storage devices.
[0160] The computer device 30 can include a processor 32 adapted to execute stored instructions, a memory 31 that provides temporary storage for operations of the instructions during operation. The processor 32 can be a single core processor, a multi-core processor, a computing cluster, or any number of other configurations. The memory 31 can include random access memory (RAM), read only memory, flash memory, or any other suitable storage system.
[0161] The processor 32 can be connected through a system interconnect (e.g., PCI, PCI-Express, etc.) to an I / O interface (input / output interface) adapted to connect the computer device 30 to one or more I / O devices (input / output devices). The I / O devices can include, for example, a keyboard and a pointing device, which can include a touchpad or a touch screen, and so on. The I / O devices can be built-in components of the computer device 30, or can be devices externally connected to the computer device.
[0162] The processor 32 can also be linked through the system interconnect to a display interface adapted to connect the computer device 30 to a display device. The display device can include a display screen that is a built-in component of the computer device 30. The display device can also include a computer monitor, a television, or a projector, and so on, that are externally connected to the computer device 30. In addition, a network interface controller (NIC) can be adapted to connect the computer device 30 to a network through the system interconnect. In some embodiments, the NIC can use any suitable interface or protocol (such as Internet Small Computer System Interface, etc.) to transfer data. The network can be a cellular network, a radio network, a wide area network (WAN), a local area network (LAN), or the Internet, and so on. Remote devices can be connected to the computer device through the network.
[0163] At this point, those skilled in the art will appreciate that although specific exemplary embodiments of the application have been described herein, the present application also encompasses many other variations or modifications in accordance with the principles of the application as set forth above. Accordingly, the scope of the present application should be understood to include all such variations and modifications.
Claims
1. A vector retrieval method for a database, comprising: Obtain the query vector points and the hierarchical navigation small-world graph index of the database; Start the search process and configure the search process to access the vector points on the hierarchical navigation small world graph index from the top to the bottom layer in order to find the nearest neighbor vector point of the query vector point. When the search process reaches the lowest level, the vector point currently accessed by the search process is determined as the currently accessed vector point, and the neighboring vector points adjacent to the currently accessed vector point are determined. Evaluate the degree of consistency between the two directions from the current access vector point to the neighbor vector point and the query vector point, respectively. The neighbor vector points whose consistency level is greater than or equal to a set threshold are included in the candidate set of the nearest neighbor vector points.
2. The vector retrieval method for a database according to claim 1, wherein, The step of evaluating the consistency between the two directions from the current access vector point to the neighbor vector point and the query vector point includes: Construct neighbor direction vectors and query direction vectors that point from the current access vector point to the neighbor vector point and the query vector point, respectively; The similarity between the neighbor direction vector and the query direction vector is calculated to obtain the degree of consistency.
3. The vector retrieval method for a database according to claim 2, wherein, The step of calculating the similarity between the neighbor direction vector and the query direction vector includes: Calculate the cosine similarity between the neighbor direction vector and the query direction vector, and use the obtained cosine similarity to evaluate the degree of consistency.
4. The vector retrieval method for a database according to claim 1, wherein, Before the step of evaluating the consistency between the two directions from the current access vector point to the neighbor vector point and the query vector point, respectively, and after the step of determining the neighbor vector points adjacent to the current access vector point, the vector retrieval method further includes: Calculate the distance between the currently accessed vector point and the query vector point to obtain the query distance value; The set threshold is calculated based on the query distance value, wherein the set threshold is configured to decrease as the query distance value decreases.
5. The vector retrieval method for a database according to claim 4, wherein, Before the step of calculating the set threshold based on the query distance value, and after the step of calculating the distance between the access vector point and the query vector point, the vector retrieval method further includes: Calculate the maximum distance value already visited, where the maximum distance value already visited is the maximum value among the distance values between the query vector point and all vector points already visited by the search process; and, The step of calculating the set threshold based on the query distance value includes: The decay factor of the set threshold is calculated based on the query distance value and the maximum distance value already visited; The set threshold is calculated based on the attenuation factor and a preset initial static threshold, wherein the attenuation factor is configured to increase as the maximum distance value visited increases, the attenuation factor is configured to decrease as the query distance value increases, and the set threshold is configured to decrease as the attenuation factor increases.
6. The vector retrieval method for a database according to claim 5, wherein, The relationship between the query distance value, the maximum distance already visited, and the decay factor is as follows: and, The relationship between the attenuation factor, the initial static threshold, and the set threshold is as follows: threshold adjusted =threshold-α(d u )×Δthreshold; Where Δthreshold is an adjustable constant and is a positive value; D max This is the maximum distance value that has been visited; d u The query distance value; threshold is the initial static threshold; threshold adjusted The set threshold is defined.
7. The vector retrieval method for a database according to claim 5, wherein, Before the step of calculating the set threshold based on the query distance value, and after the step of calculating the maximum visited distance value, the vector retrieval method further includes: Determine whether the query distance value is greater than or equal to the maximum distance value already visited; If so, assign the maximum distance value already visited to the value of the query distance.
8. The vector retrieval method for a database according to claim 1, wherein, When performing the step of including neighbor vector points with a consistency level greater than or equal to a set threshold into the candidate set of the nearest neighbor vector points, the vector retrieval method further includes: Pruning is performed on neighbor vector points whose consistency level is less than the set threshold; and... After the step of including the neighbor vector points with a consistency level greater than or equal to a set threshold into the candidate set of the nearest neighbor vector points, the vector retrieval method further includes: Identify the nearest neighbor vector point in the candidate set to the query vector point, and configure the search process to access that neighbor vector point; Return to the step of determining the vector point currently accessed by the search process as the currently accessed vector point.
9. The vector retrieval method for a database according to claim 1, wherein, Before the step of determining that the vector point currently accessed by the search process is the currently accessed vector point, and if the currently accessed vector point is the entry point of the search process at the lowest level, the vector retrieval method further includes: Obtain the number of vector points at the lowest level that the search process needs to access; Determine whether the number is greater than a preset threshold for access; If so, perform the step of determining that the vector point currently accessed by the search process is the currently accessed vector point; If not, configure the search process to perform the search according to the cluster search strategy.
10. A computer program product comprising a computer program that, when executed by a processor, implements the vector retrieval method for the database according to any one of claims 1 to 9.
Citation Information
Cited By
Data correlation recommendation method based on vectorization information matching
CN122132447A
A database-based fusion index construction and query method, device and medium
CN122387963A
A database-based fusion index construction and query method, device and medium
CN122387963B