本教程演示了一种混合搜索,它是针对相同查询条件的全文搜索和语义搜索的聚合。虽然全文搜索对于查询词的精确匹配很有效,但语义搜索还提供了一个额外好处,即使文档不包含精确的查询词,也能识别语义相似的文档。这确保了同义匹配和上下文相似的匹配项也包含在两种搜索方法的组合结果中。
相反,如果数据集中有专有名词或特定关键词的词元,这些词元在数据集中使用的上下文相同,您不希望在培训嵌入模型时考虑这些词元,则向量搜索可能会受益于与全文搜索相结合。
您还可以为每个查询的每种搜索方法设立权重。根据全文或语义搜索结果是否与查询最相关且最合适,您可以增加每个查询的搜索方法的权重。
关于教程
本教程演示了如何在包含电影详细信息的 sample_mflix.embedded_movies
集合上运行结合 Atlas Vector Search 和 Atlas Search 查询的混合搜索,以获得统一的搜索结果。具体来说,本教程将指导您完成以下步骤:
在
plot_embedding_voyage_3_large
字段上创建 Atlas Vector Search 索引。该字段包含表示电影情节摘要的向量嵌入。在
sample_mflix.embedded_movies
集合的title
字段上设置 Atlas Search 索引。此字段包含文本字符串形式的电影名称。运行一个使用互易等级融合的查询,将针对
plot_embedding_voyage_3_large
字段的$vectorSearch
查询结果和针对title
字段的$search
查询结果结合起来。
先决条件
在开始之前,请完成通用前提条件。您必须拥有 Project Data Access Admin
访问项目的权限,才能创建 Atlas Vector Search 和 Atlas Search 索引。
创建 Atlas Vector Search 和 Atlas Search 索引
本部分演示如何在sample_mflix.embedded_movies
collection中的字段上创建以下索引:
plot_embedding_voyage_3_large
字段上的 Atlas Vector Search 索引,用于针对该字段运行向量查询。title
字段上的 Atlas Search 索引,用于对该字段运行全文搜索。
步骤
使用 mongosh
连接到 Atlas 集群。
定义 Atlas Vector Search 索引。
运行以下命令。此索引定义在查询集合时将 plot_embedding_voyage_3_large
字段索引为 Atlas Vector Search 字段。
1 db.embedded_movies.createSearchIndex( 2 "hybrid-vector-search", 3 "vectorSearch", 4 { 5 "fields": [ 6 { 7 "type": "vector", 8 "path": "plot_embedding_voyage_3_large", 9 "numDimensions": 2048, 10 "similarity": "dotProduct" 11 } 12 ] 13 } 14 );
运行组合语义搜索和全文搜索查询
本节演示了如何通过在 $rankFusion
阶段内使用 $vectorSearch
和 $search
管道阶段,在 plot_embedding_voyage_3_large
和 title
字段中查询 sample_mflix.embedded_movies
star wars 集合中的数据,从而对结果中的文档进行重新排序。$rankFusion
阶段可确保同时出现在两个搜索结果中的文档都出现在组合结果的顶部。
步骤
设置要在查询中使用的嵌入。
创建一个名为
query-embeddings.js
的文件。将以下嵌入复制并粘贴到文件中。
STAR_WARS_EMBEDDING=[-0.025189938,0.014741838,-0.013024342,-0.0197512,0.011235285,0.004651551,0.043509893,0.003112961,0.013310592,-0.033348043,0.037212405,-0.021039322,-0.026048684,0.012809656,0.029483676,0.003578116,-0.044654887,0.032632418,0.014312465,-0.058967352,0.025333062,-0.055246111,0.02189807,-0.017604331,-0.002880384,0.045227386,0.004794675,0.017604331,0.023186192,-0.054673612,-0.011306847,-0.012523406,-0.012380281,0.002540462,0.015958399,-0.042364895,-0.001467028,-0.020180576,-0.058108605,-0.035065539,0.010090288,-0.033348043,0.058394853,-0.013883091,0.002048471,-0.020753073,-0.029769925,0.031916797,-0.014741838,-0.040933646,-0.004096943,0.020753073,-0.002540462,0.028052431,-0.02404494,0.006547953,-0.003578116,0.003757022,0.019178702,-0.037784904,-0.02833868,0.01753277,0.029769925,0.017747456,-0.031344298,0.022899942,0.006333265,0.010376536,-0.024474313,-0.012094032,-0.004651551,0.007764512,0.017962143,0.013811528,0.037212405,-0.03148742,0.000666424,0.024474313,-0.021325571,0.041219898,0.011235285,0.046658635,0.019035578,0.020753073,0.010662786,-0.001726441,-0.012738093,-0.027193682,-0.014598713,-0.013167467,0.013596841,0.001932183,-0.010304974,-0.007478263,0.005689205,0.002987727,0.005724986,0.002325775,0.002415228,-0.003828584,-0.029340552,-0.017318081,-0.070417322,0.003810694,-0.013453716,-0.001628043,-0.027909305,0.014026215,0.009589351,0.004902019,0.028768053,-0.005259831,-0.010448099,0.025189938,0.038357403,0.048662379,0.039788652,0.010448099,0.001574371,0.020323699,0.005510299,0.026907433,0.043223642,-0.001153942,-0.010233412,0.048376128,-0.056104861,0.006691077,0.015672149,-0.015028087,0.036210533,-0.009231539,0.010519661,0.022899942,0.025762435,-0.009052633,-0.0301993,0.032203045,-0.00522405,0.029626802,-0.02433119,-0.025619311,0.016674021,0.02404494,-0.009589351,-0.026334934,-0.04436864,-0.014455589,0.02619181,0.017604331,0.02189807,-0.007728731,-0.021611821,-0.03363429,0.008480135,-0.027479932,0.025046812,-0.006047016,0.020753073,0.01016185,-0.034063663,0.029483676,-0.019035578,0.041506145,0.013453716,-0.009159978,0.007549825,0.025189938,0.005152487,-0.009446226,-0.009016853,0.021325571,0.030771798,-0.046944883,0.001314958,0.021182448,0.047231134,-0.007048889,-0.030771798,-0.025905561,-0.000612752,-0.023186192,0.011378409,0.035065539,0.007979199,0.023901815,-0.004973582,0.005188268,-0.046944883,0.009374664,0.047231134,0.058967352,0.043509893,0.011449971,0.017174957,-0.024188064,-0.025476186,-0.02833868,0.033061791,0.015314337,-0.018749328,0.013382155,0.007048889,0.005975454,0.005295612,-0.013310592,-0.022756819,-0.012523406,-0.03363429,-0.014527151,0.011449971,0.01202247,0.044941138,-0.012594969,0.002862493,0.000572499,0.030628674,-0.0098756,0.020466823,0.059539851,-0.00370335,0.007335138,0.023901815,0.023758691,-0.005903892,0.003918037,0.013310592,0.010090288,-0.012809656,-0.010376536,-0.01109216,-0.008086543,0.012809656,-0.019894326,0.012738093,0.056391109,0.029340552,-0.04436864,-0.001619098,0.042364895,-0.027623056,0.011593096,-0.031916797,-0.0301993,0.032203045,-0.003757022,0.017174957,0.033491168,0.003900147,0.002325775,0.006726858,0.020180576,0.017389644,0.009088415,0.018319955,-0.003631788,0.00586811,-0.006691077,-0.014240902,-0.009052633,0.031630546,0.04436864,-0.022899942,-0.003327648,-0.006691077,0.013310592,-0.035924286,-0.008158104,-0.005116706,-0.040647399,0.002397338,0.014455589,-0.030342424,0.028624929,-0.031773672,0.043509893,-0.001833785,-0.025619311,0.032775544,-0.046944883,0.013739966,-0.030485548,0.018319955,0.016745584,-0.020323699,-0.015815273,-0.020896198,-0.015171212,0.026334934,0.035638034,0.008873728,0.003291867,-0.02647806,0.003649678,0.003613897,0.009804038,-0.013525278,0.005367174,0.007657168,-0.017103394,-0.015815273,-0.000398065,0.013310592,0.014240902,0.003935928,0.001735386,-0.018606203,0.008265448,-0.068127327,0.012165595,-0.007836075,0.02189807,-0.000983982,0.019178702,-0.009589351,-0.013739966,-0.007800293,0.040361151,0.027623056,-0.002540462,-0.03663991,0.011163722,-0.016316209,-0.006333265,-0.010877473,-0.023329318,-0.021468697,0.013596841,0.032059919,0.007442481,0.02433119,-0.003613897,-0.013596841,0.010448099,0.010877473,-0.0098756,0.033920541,-0.006691077,-0.039502401,-0.010877473,-0.016960271,0.014097777,-0.008122323,0.007478263,0.010018725,-0.030485548,-0.011020597,0.000317558,0.00461577,0.020466823,0.070703574,-0.024617439,0.002111088,-0.024617439,-0.004204286,-0.048662379,-0.006834202,0.027766181,-0.002504681,0.025189938,0.033920541,-0.02833868,-0.000773768,-0.03578116,0.015958399,0.006369046,0.033204917,-0.006762639,0.02003745,-0.020180576,0.015886836,-0.015385899,-0.029340552,-0.009446226,0.015529023,-0.010376536,-0.012881218,-0.000715623,0.014312465,-0.029197427,-0.000684315,0.000360048,0.015815273,-0.027050557,0.006655296,0.018892452,-0.021182448,0.031201173,0.014240902,-0.022756819,0.004365302,-0.020609949,0.008515916,-0.016244646,0.001162888,0.000084421,0.003273976,-0.017819017,0.000576971,0.020753073,-0.004794675,0.018105267,-0.013095905,-0.028052431,0.004114834,0.02833868,-0.027193682,-0.010877473,-0.002576244,0.011879345,-0.017819017,0.006726858,-0.021754947,-0.031773672,-0.013382155,0.024903689,0.013167467,0.000033964,0.034063663,0.022613693,-0.038357403,-0.010018725,-0.017174957,-0.004418973,0.02189807,-0.003166633,-0.009589351,0.009303101,-0.036496785,-0.005760767,-0.006583733,-0.003596007,0.014026215,-0.003828584,-0.02833868,-0.020896198,0.001449137,0.039502401,0.012881218,0.025476186,0.000961619,-0.025762435,0.002808821,0.034922414,0.004687332,-0.046658635,0.030914923,-0.036067411,0.008659041,-0.004025381,-0.0301993,-0.026048684,0.024760563,0.036496785,-0.029913051,0.015672149,0.007764512,0.01509965,0.010304974,-0.004490536,-0.007585606,-0.019464951,0.016602458,-0.007048889,-0.005510299,0.011163722,0.013739966,-0.034636162,0.020609949,-0.004418973,0.034636162,0.040933646,0.031773672,0.023758691,0.031344298,-0.006798421,0.026048684,-0.011521534,0.020753073,0.014384027,0.026334934,-0.034206789,-0.036067411,0.014598713,0.023758691,-0.039216153,0.003363429,0.002880384,-0.006726858,-0.000916892,-0.001395465,-0.009660914,0.032059919,0.008086543,0.029054303,-0.011593096,0.065551087,0.031058047,-0.041219898,-0.014097777,-0.017103394,0.016244646,-0.028911177,0.044654887,-0.030771798,0.024760563,0.02833868,0.018248392,0.026907433,-0.002227377,0.034063663,0.000167724,0.021039322,-0.018892452,0.012738093,-0.001395465,0.005760767,-0.024760563,-0.002683587,0.000230341,-0.0197512,0.009088415,-0.00400749,-0.026764309,-0.012881218,0.016101522,-0.009303101,0.015529023,-0.016817145,0.014312465,-0.030914923,-0.018463079,0.020323699,-0.023472441,-0.023758691,-0.005009362,0.018176829,0.012738093,0.009374664,-0.031916797,0.016387772,0.027479932,0.015529023,-0.021325571,0.020323699,-0.025476186,0.008515916,-0.039788652,-0.007979199,-0.009947163,-0.006869983,0.004758894,0.022613693,-0.013668403,-0.015171212,0.035351787,-0.022327444,0.019178702,0.000404774,-0.003524444,-0.012094032,0.023901815,-0.0400749,-0.004579989,0.00245101,0.013024342,0.015958399,0.009517789,0.034779288,0.021468697,0.00062617,0.007728731,-0.028195554,0.0301993,-0.002504681,0.008909509,0.004651551,-0.007013108,0.03148742,0.019608077,0.002540462,0.043509893,-0.006190141,0.024903689,0.010519661,0.018319955,0.010519661,0.009660914,0.000966091,-0.004454754,0.000299667,0.007907636,-0.018463079,0.004758894,-0.001851675,-0.002415228,0.010233412,-0.024617439,-0.030771798,0.018749328,0.003023508,0.005474518,-0.011521534,-0.008551697,0.007979199,0.03363429,0.000275068,0.007800293,0.0039896,0.00522405,-0.035924286,-0.01416934,0.02619181,-0.025476186,-0.033777416,0.021325571,-0.02218432,0.001833785,0.027766181,-0.006118578,0.032059919,0.038929902,0.003613897,0.031344298,-0.002737259,0.057536107,0.009732476,0.020753073,0.005402955,-0.047803629,-0.040933646,0.009052633,-0.030485548,0.018319955,0.025046812,-0.002361557,0.045513637,0.008766385,-0.031058047,0.014312465,0.002737259,-0.004186396,0.032059919,0.024617439,-0.012666531,0.006798421,0.02619181,-0.012523406,0.009947163,0.005617642,0.039216153,0.008766385,0.009517789,0.042651143,-0.012881218,0.007263576,-0.000514354,0.016817145,-0.048948627,0.018176829,0.034922414,0.005331393,0.000391356,-0.017604331,0.026048684,-0.011807784,0.017461207,0.012809656,0.029483676,-0.017174957,0.023472441,0.005188268,0.007585606,-0.034922414,0.069558576,0.023472441,-0.010304974,0.020180576,0.025046812,0.016459335,0.000317558,-0.018606203,0.066696085,0.011664659,0.025762435,-0.016888708,0.015314337,-0.009231539,0.016459335,-0.021325571,0.009303101,0.000840857,-0.014455589,0.00170855,0.014741838,-0.004168505,-0.009088415,-0.0074067,-0.004472645,0.002665696,0.023615567,0.038929902,-0.016960271,-0.027193682,0.03663991,-0.016530896,0.003256086,0.015171212,0.036926158,0.02433119,0.047231134,-0.049234878,0.009947163,-0.01109216,-0.014097777,-0.007585606,0.00338132,-0.008086543,0.018176829,-0.014527151,-0.000205742,-0.041219898,0.012666531,0.046086136,0.004025381,-0.0074067,0.033348043,-0.020896198,-0.000514354,0.033491168,0.004257958,0.02404494,-0.008372792,-0.021754947,0.037784904,0.013453716,0.013024342,-0.026334934,0.023758691,0.012094032,0.006297485,0.045227386,0.021039322,-0.020323699,0.005975454,0.008802165,0.00370335,0.006941545,-0.029340552,-0.008551697,-0.004454754,0.003488663,0.010662786,0.00801498,0.010090288,0.015600586,0.018105267,-0.020180576,-0.00307718,0.031630546,0.000644061,0.011950907,-0.023472441,0.01509965,-0.035924286,0.016459335,-0.027766181,-0.014598713,-0.021611821,-0.013310592,-0.021039322,-0.02189807,0.018606203,-0.007979199,0.018176829,0.022041194,-0.002916165,0.009088415,-0.00522405,-0.018176829,-0.031916797,-0.017318081,-0.025476186,-0.014527151,-0.017675893,-0.026621183,0.000362284,0.02619181,0.016101522,-0.013310592,0.021325571,0.027909305,0.016316209,0.006011235,0.008551697,0.030914923,-0.070703574,0.004794675,-0.019321827,-0.011163722,-0.014598713,-0.0197512,-0.005438737,-0.025189938,-0.037212405,0.004168505,-0.021754947,0.018033706,0.035065539,0.022756819,0.005581861,-0.007764512,-0.003005618,-0.003524444,0.006655296,-0.00170855,-0.046086136,-0.009374664,0.001744332,0.030056175,0.016674021,0.014312465,0.029054303,-0.009052633,0.005832329,-0.029197427,-0.004723113,0.032489292,0.022899942,-0.044941138,0.014026215,-0.007227794,-0.035494912,0.001261286,0.079004802,0.008122323,0.022041194,0.016602458,0.046658635,-0.016888708,-0.006547953,-0.016316209,0.002021636,-0.016745584,0.003792803,0.005116706,-0.037784904,-0.028481804,-0.014670276,-0.005259831,0.018892452,0.001252341,-0.068699829,-0.021611821,-0.015242774,-0.027050557,-0.032059919,0.026048684,-0.014240902,-0.007013108,0.014598713,-0.005474518,-0.007192013,-0.016817145,0.00400749,0.010519661,0.007657168,0.005295612,0.009124196,0.024474313,-0.019894326,-0.044941138,-0.022756819,-0.022327444,-0.041792396,0.027479932,-0.013668403,-0.036210533,0.001225505,0.009947163,-0.044654887,-0.02003745,0.031344298,-0.004186396,-0.009517789,0.000720096,-0.023901815,0.000670897,0.022899942,0.006619515,0.006512171,0.022327444,0.021468697,0.021611821,0.039216153,-0.019608077,0.028052431,-0.020466823,-0.0197512,0.004454754,0.026048684,-0.024617439,-0.000333212,0.002200541,-0.002629915,0.021611821,0.009374664,0.00894529,-0.057822354,-0.009660914,-0.002844602,0.020323699,0.000603807,0.018033706,-0.027050557,-0.004186396,-0.019608077,-0.021754947,0.009732476,0.01602996,-0.016960271,-0.001520699,-0.023615567,0.004383192,0.000925838,0.023043068,0.032775544,0.006404828,-0.010304974,0.019321827,0.017604331,-0.01230872,0.007657168,0.005402955,-0.03148742,-0.000550135,-0.002111088,-0.029626802,0.01323903,-0.033777416,0.006655296,0.035065539,-0.003256086,0.000907947,0.004025381,0.011020597,-0.04808988,0.02619181,0.015171212,0.023758691,0.014741838,-0.001359684,-0.041506145,-0.009088415,-0.012738093,0.000176669,0.033777416,0.024188064,-0.002307885,0.023901815,0.00034663,-0.024474313,-0.031773672,-0.023758691,-0.024474313,-0.011163722,0.000447265,0.005080925,-0.00123445,0.006297485,-0.031058047,-0.012738093,-0.003059289,-0.026907433,-0.015672149,-0.005760767,0.023043068,0.023043068,-0.015028087,0.017747456,0.013883091,-0.011807784,0.038357403,-0.016817145,0.014884963,0.017389644,-0.000599334,0.016602458,0.008086543,-0.039502401,0.050379876,-0.024474313,0.035351787,-0.023758691,0.002039526,0.004061162,-0.012165595,-0.020180576,0.001636988,-0.013883091,0.017389644,0.006225922,-0.03578116,-0.016817145,-0.001332848,-0.005617642,-0.008730603,-0.039216153,0.02433119,0.028052431,0.02833868,0.039502401,0.010233412,-0.006869983,0.021468697,0.002039526,-0.0197512,0.020753073,0.027050557,0.009517789,0.011449971,0.038929902,0.008873728,0.009374664,0.007871855,-0.006082797,-0.007156232,-0.014670276,-0.000447265,0.046944883,-0.015242774,-0.019894326,0.008158104,0.016173085,-0.018463079,0.034922414,-0.005009362,-0.000092248,0.005760767,0.006190141,-0.022613693,0.034206789,0.012523406,0.000992927,0.038071156,-0.048376128,-0.017747456,0.014384027,0.000751404,0.015314337,-0.010519661,0.058681104,0.013954652,0.022899942,-0.003757022,0.01416934,-0.000469628,-0.008337011,-0.001153942,0.02189807,-0.024760563,0.01416934,-0.012738093,-0.025046812,0.030771798,-0.046658635,-0.002137924,0.053242367,0.010090288,-0.046086136,0.016316209,-0.005295612,0.023043068,-0.023043068,0.010233412,-0.018319955,-0.017389644,0.030485548,0.009660914,0.017174957,0.050379876,-0.010304974,0.017819017,-0.000364521,0.011163722,0.001753277,0.010448099,0.013095905,0.008372792,-0.01109216,0.036926158,-0.015672149,-0.014598713,0.008981071,-0.011879345,-0.036926158,-0.01789058,-0.008694822,-0.028911177,-0.017103394,-0.028768053,-0.030914923,0.001033181,0.00431163,-0.024474313,-0.031058047,0.010018725,0.00647639,-0.027336806,0.025046812,0.003148742,-0.010018725,0.03663991,0.033348043,-0.001162888,-0.01016185,0.010376536,0.010519661,0.019178702,0.016101522,0.007943418,-0.013739966,-0.013525278,-0.027193682,0.006655296,0.027050557,-0.017389644,-0.027479932,0.041792396,0.045513637,-0.014741838,0.012451844,0.018319955,-0.00153859,0.010519661,0.017962143,-0.012594969,0.018606203,0.023472441,-0.034063663,-0.004061162,0.015600586,0.019178702,0.002361557,-0.025619311,-0.00586811,-0.02003745,0.013739966,0.017675893,-0.025189938,-0.002415228,0.001547535,0.019608077,0.039502401,-0.00184273,0.025189938,0.00277304,0.020323699,0.007227794,0.012165595,-0.00370335,0.02433119,-0.00586811,0.016459335,0.034206789,0.001051072,-0.010519661,-0.004096943,0.00153859,0.01574371,0.01230872,-0.007692949,-0.019464951,0.005116706,0.017389644,0.024903689,0.046658635,-0.010519661,0.020609949,0.060684849,-0.045227386,-0.008551697,-0.004293739,-0.001502809,-0.015815273,-0.005975454,0.000290722,0.027050557,-0.002245268,-0.016888708,-0.027193682,-0.001288122,-0.021182448,-0.041219898,0.031916797,0.030628674,-0.03234617,0.006655296,0.008372792,-0.009016853,-0.033348043,0.010877473,-0.05238362,-0.004490536,-0.028911177,0.006905764,-0.003506554,0.039788652,-0.036496785,-0.015886836,0.015314337,-0.015672149,0.006225922,-0.021182448,-0.034349915,-0.043223642,-0.025476186,0.002558353,0.007048889,-0.037784904,-0.014026215,-0.044654887,-0.036926158,0.008229667,0.007728731,0.039216153,-0.00027954,0.026907433,0.027193682,-0.017174957,-0.011378409,0.017174957,-0.015815273,0.009446226,0.033777416,-0.014384027,0.003721241,-0.024760563,-0.000229223,0.008802165,-0.000377939,-0.007692949,0.016173085,0.018248392,-0.02218432,-0.003202414,-0.033204917,-0.002969836,-0.023186192,0.030056175,-0.015457462,-0.015314337,-0.008981071,0.022613693,-0.014026215,-0.025476186,0.025333062,0.034206789,0.010662786,0.016387772,0.030342424,-0.008086543,0.007120451,0.000221396,0.025619311,0.01789058,-0.008086543,0.011807784,-0.016101522,0.002719368,0.005653423,0.017962143,-0.001941128,0.06297484,0.016244646,0.003524444,0.018749328,-0.001815894,0.006082797,0.069558576,-0.011020597,0.018033706,0.026621183,0.007692949,-0.008694822,0.035924286,-0.001878511,-0.005975454,-0.028911177,-0.007871855,0.014741838,0.008587479,0.026621183,0.005259831,-0.023186192,0.000368993,-0.01323903,0.002683587,-0.011306847,0.007478263,-0.000621698,0.022756819,-0.018892452,-0.013167467,0.007764512,-0.010877473,-0.017461207,-0.013525278,0.014240902,-0.019464951,-0.009016853,0.016173085,-0.027479932,0.002934055,0.042937394,-0.004830457,-0.031201173,-0.024188064,0.00245101,0.017318081,0.001824839,-0.022756819,0.021325571,-0.027479932,-0.008659041,0.008337011,-0.016674021,-0.003452882,0.000711151,-0.045799885,0.013739966,-0.029340552,-0.035208661,0.000554608,-0.007800293,0.018892452,0.028481804,-0.011593096,-0.00894529,0.024474313,-0.008050761,0.025476186,-0.001306012,-0.00214687,-0.008050761,-0.018606203,-0.006047016,-0.028768053,0.029197427,-0.021468697,0.005402955,-0.021039322,-0.014312465,-0.002325775,-0.020896198,0.000706678,0.035638034,-0.008480135,-0.035351787,0.014312465,0.012523406,0.011807784,0.003041399,-0.010018725,-0.022613693,-0.021182448,-0.005474518,-0.007120451,0.025476186,0.036926158,-0.007907636,-0.014097777,0.010519661,0.005617642,0.014670276,-0.011664659,0.011879345,-0.012738093,-0.007871855,-0.012666531,0.032918669,-0.010018725,0.007943418,0.024188064,0.005760767,-0.003918037,0.022613693,0.017318081,0.036496785,0.037784904,0.008694822,0.016674021,0.000106225,0.003685459,-0.031201173,-0.01016185,-0.02404494,-0.019035578,0.031773672,0.000975037,-0.032489292,-0.033920541,0.015385899,-0.023186192,0.012523406,-0.011163722,-0.005116706,0.015314337,-0.006834202,-0.038357403,-0.023472441,0.015529023,0.017747456,0.005653423,-0.002325775,0.009088415,-0.022899942,0.02433119,0.000849803,-0.042078644,-0.004257958,0.018176829,-0.0049378,-0.002415228,-0.016173085,0.012594969,0.013596841,-0.017031832,-0.001806949,-0.003810694,0.003005618,0.046944883,-0.025476186,0.012738093,0.009016853,-0.009660914,0.012666531,0.014026215,-0.010519661,0.022327444,0.00400749,-0.007478263,-0.03578116,0.009804038,-0.028911177,-0.018248392,0.004758894,0.005939673,0.033920541,-0.011378409,-0.005474518,0.012451844,0.018176829,0.033920541,0.025905561,-0.014670276,0.001798003,0.031344298,0.00431163,0.027193682,-0.006798421,0.011449971,0.00894529,0.013883091,0.023758691,-0.007692949,0.031344298,-0.014384027,-0.007514044,-0.026621183,-0.001645933,-0.010090288,-0.009589351,0.008193886,-0.007871855,-0.031773672,0.013525278,0.006583733,0.010949035,-0.004794675,-0.004061162,-0.021468697,0.011020597,-0.020609949,0.010018725,0.010662786,0.008158104,0.00370335,0.007800293,-0.029626802,0.029197427,-0.017174957,-0.00586811,-0.003112961,-0.018749328,-0.042364895,0.027050557,0.028624929,0.008837947,0.026334934,0.018248392,-0.004758894,0.009446226,-0.033061791,-0.022041194,-0.021039322,0.008086543,-0.009088415,-0.031630546,0.020896198,-0.018749328,0.022613693,-0.014097777,0.004347411,0.014813401,-0.013525278,-0.001726441,0.010448099,0.036067411,-0.026048684,-0.020753073,-0.005045144,0.033348043,-0.002576244,0.02404494,0.022041194,0.017031832,-0.000297431,-0.003184523,-0.012738093,0.022756819,-0.016888708,0.021039322,-0.000313085,0.032203045,-0.011235285,0.001583316,-0.00461577,-0.026334934,-0.002934055,-0.005939673,0.001458082,0.007156232,-0.005689205,-0.042937394,0.011378409,0.007156232,0.00554608,0.026621183,-0.014455589,-0.017604331,-0.02647806,-0.03578116,-0.009804038,0.006905764,-0.0197512,-0.035494912,-0.027623056,-0.00461577,0.011521534,0.006905764,0.009517789,0.003112961,0.033920541,-0.004687332,-0.005045144,-0.009231539,0.000126911,0.008694822,-0.029340552,0.011163722,0.047517382,-0.039788652,-0.033348043,-0.003041399,-0.007657168,-0.035065539,-0.012165595,-0.024617439,0.0049378,0.017461207,0.029626802,-0.004723113,-0.000366757,-0.041792396,0.041219898,-0.009446226,0.016960271,-0.017747456,-0.036067411,0.025046812,0.027336806,0.025905561,0.049234878,0.016602458,-0.005796548,0.021325571,-0.003524444,-0.000872166,0.014670276,0.020323699,0.002737259,0.009660914,-0.006440609,-0.062402345,-0.062688597,-0.008158104,-0.018749328,0.005438737,-0.002066362,0.000205742,-0.018749328,0.017103394,-0.03578116,0.000265004,-0.03663991,-0.024760563,0.054101113,-0.00647639,-0.026334934,0.034779288,0.022756819,-0.033348043,-0.02433119,-0.041792396,-0.014455589,-0.023329318,-0.004687332,-0.013453716,-0.018319955,-0.014455589,0.043509893,0.013453716,-0.014813401,0.009052633,-0.034349915,0.052097369,-0.003774913,0.016316209,-0.027050557,0.028195554,-0.007549825,-0.020466823,0.014598713,-0.005939673,0.003273976,-0.052097369,0.002701478,-0.02404494,-0.005903892,0.009947163,-0.00043161,0.02189807,0.042078644,-0.020609949,0.007836075,0.018892452,0.001207614,0.009159978,-0.029197427,-0.023329318,0.004347411,-0.019464951,0.020180576,0.019894326,0.00615436,0.021468697,-0.017461207,-0.011879345,-0.002307885,0.016101522,-0.029626802,-0.014455589,0.020323699,0.020753073,0.018176829,-0.006082797,0.027766181,0.016530896,-0.008444354,0.001806949,0.029626802,-0.012451844,0.023186192,0.006869983,-0.034063663,-0.012094032,-0.021611821,-0.008050761,0.05581861,-0.004257958,-0.025333062,-0.004526317,0.018033706,0.027479932,0.025476186,0.008802165,-0.009589351,0.012809656,0.012523406,0.0400749,0.018033706,0.011020597,0.014956526,0.013883091,-0.006655296,0.019608077,-0.03234617,0.007585606,-0.036067411,-0.024617439,-0.001628043,0.022041194,0.026764309,-0.038643654,-0.009124196,-0.020323699,-0.013883091,0.02404494,0.002361557,-0.03234617,-0.0049378,0.018606203,0.022327444,0.043509893,-0.030485548,-0.003560225,0.007442481,0.00370335,0.011449971,0.023472441,0.000197915,-0.011950907,0.023329318,-0.009804038,-0.025619311,0.0074067,-0.003953818,0.014312465,0.048662379,-0.027193682,0.019894326,-0.042364895,0.003327648,0.042937394,-0.040933646,-0.020466823,0.056104861,0.020896198,-0.014527151,-0.036210533,-0.022470569,0.035924286,-0.013167467,-0.055532362,-0.006190141,-0.027909305,0.012881218,0.003005618,0.005689205,0.01109216,-0.016387772,-0.021468697,0.018176829,-0.021611821,0.032918669,0.002951946,-0.008837947,-0.031773672,-0.029913051,0.017318081,0.017461207,-0.02833868,-0.027479932,-0.023615567,0.037212405,-0.023615567,-0.02404494,0.029626802,0.002951946,0.004043271,-0.001377575,0.007800293,0.024188064,-0.027336806,-0.002361557,-0.00307718,-0.025189938,0.007192013,-0.000207978,0.040933646,0.010877473,-0.000720096,-0.006941545,0.003059289,0.032775544,-0.007657168,0.019321827,-0.012094032,0.042078644,-0.010591224,0.011807784,-0.016173085,-0.015529023,-0.023615567,0.016173085,-0.018606203,0.007192013,-0.008587479,0.000257177,0.002245268,-0.011879345,0.044082388,-0.005939673,-0.003059289,-0.013954652,-0.026621183,0.039502401,-0.006440609,0.014384027,0.012094032,-0.034779288,-0.002379447,0.023329318,0.029483676,-0.002325775,-0.001565426,0.025619311,0.018606203,-0.016101522,-0.0301993,0.002576244,0.035208661,0.002325775,-0.011664659,-0.022470569,-0.047231134,-0.016888708,-0.017747456,0.05152487,0.009374664,-0.010591224,0.009303101,-0.005009362,-0.029197427,0.001726441,-0.023758691,-0.002934055,-0.017461207,0.018606203,-0.002665696,-0.003578116,-0.018606203,-0.004651551,-0.021325571,0.005367174,-0.009124196,0.016888708,-0.01295278,0.002048471,-0.011593096,0.017604331,-0.008515916,0.006082797,-0.036067411,-0.027336806,-0.010018725,-0.001306012,0.026334934,-0.019321827,-0.030914923,-0.026764309,-0.0024689,-0.005581861,-0.002755149,0.019464951,-0.01295278,0.011163722,0.000639588,0.025476186,0.029483676,0.042651143,-0.011306847,0.002737259,0.010090288,-0.015600586,0.038357403,0.029197427,-0.008193886,0.044082388,0.032489292,-0.00123445,-0.008372792,0.009124196,-0.00309507,-0.041792396,0.007764512,-0.009947163,0.044941138,0.030056175,-0.013024342,-0.019321827,0.019321827,0.008551697,-0.053814866,0.038071156,-0.037784904,0.012165595,-0.008444354,-0.029626802,-0.006905764,-0.028195554,-0.01080591,-0.0098756,-0.032203045,0.040361151,-0.033920541,0.004454754,-0.036067411,0.011879345,-0.011235285,0.020609949,-0.004579989,0.0074067,-0.017103394,0.01789058,0.011449971,-0.007836075,0.000427138,-0.007120451,0.008337011,0.00123445,-0.003273976,-0.007263576,-0.011449971,0.022756819,0.002576244,-0.013310592,-0.013382155,-0.011664659,-0.013453716,-0.005152487,-0.050379876,-0.034636162,-0.011306847,0.015028087,0.008480135,-0.047517382,-0.00586811,-0.008301229,-0.01416934,0.000876638,0.000326503,0.01080591,0.004150615,0.005259831,-0.008837947,0.017031832,-0.016316209,-0.036210533,-0.013883091,-0.000295195,0.014240902,0.020753073,-0.022899942,-0.038929902,0.00586811,-0.008050761,0.00016437,-0.001932183,0.001789058,0.017031832,0.045227386,-0.016745584,-0.005689205,0.008372792,0.008301229,-0.016674021,-0.004222177,0.005832329,0.016173085,-0.009374664,-0.006869983,-0.052669868,0.035065539,0.000123557,0.007478263,0.033491168,0.054387365,0.002254213,-0.007836075,-0.020323699,-0.019178702,-0.010376536,-0.039788652,-0.032632418,-0.012380281,-0.002540462,-0.016602458,-0.022756819,0.019608077,0.001887456,0.032918669,0.008050761,-0.013739966,0.006547953,-0.016674021,0.001574371,0.019321827,-0.016960271,-0.031201173,0.027766181,0.006655296,0.020753073,0.009804038,0.024903689,-0.043509893,0.015815273,-0.017962143,-0.043223642,0.033920541,-0.007764512,-0.006762639,0.017819017,-0.026621183,0.046658635,-0.004490536,0.024188064,-0.010304974,0.004651551,0.047231134,-0.038071156,0.009589351,-0.033920541,-0.027050557,0.050093625,0.008515916,-0.025189938,-0.008050761,0.011950907,0.004973582,0.014527151,-0.009374664,-0.003721241,0.019608077,0.049807377,0.016602458,0.02404494]; 保存并关闭文件。
使用 mongosh
连接到集群。
使用 sample_mflix
数据库。
在 mongosh
提示符下运行以下命令:
use sample_mflix
switched to db sample_mflix
对 embedded_movies
集合运行以下 Atlas Search 查询。
1 db.embedded_movies.aggregate([ 2 { 3 $rankFusion: { 4 input: { 5 pipelines: { 6 vectorPipeline: [ 7 { 8 "$vectorSearch": { 9 "index": "hybrid-vector-search", 10 "path": "plot_embedding_voyage_3_large", 11 "queryVector": STAR_WARS_EMBEDDING, 12 "numCandidates": 100, 13 "limit": 20 14 } 15 } 16 ], 17 fullTextPipeline: [ 18 { 19 "$search": { 20 "index": "hybrid-full-text-search", 21 "phrase": { 22 "query": "star wars", 23 "path": "title" 24 } 25 } 26 }, 27 { "$limit": 20 } 28 ] 29 } 30 }, 31 combination: { 32 weights: { 33 vectorPipeline: 0.5, 34 fullTextPipeline: 0.5 35 } 36 }, 37 "scoreDetails": true 38 } 39 }, 40 { 41 "$project": { 42 _id: 1, 43 title: 1, 44 plot: 1, 45 scoreDetails: {"$meta": "scoreDetails"} 46 } 47 }, 48 { 49 "$limit": 10 50 } 51 ]);
[ { _id: ObjectId('573a1397f29313caabce68f6'), plot: "Luke Skywalker joins forces with a Jedi Knight, a cocky pilot, a wookiee and two droids to save the universe from the Empire's world-destroying battle-station, while also attempting to rescue Princess Leia from the evil Darth Vader.", title: 'Star Wars: Episode IV - A New Hope', scoreDetails: { value: 0.01626123744050767, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 2, weight: 0.5, value: 2.9571781158447266, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 1, weight: 0.5, value: 0.7987099885940552, details: [] } ] } }, { _id: ObjectId('573a139af29313caabcf0f5f'), plot: 'Two Jedi Knights escape a hostile blockade to find allies and come across a young boy who may bring balance to the Force, but the long dormant Sith resurface to reclaim their old glory.', title: 'Star Wars: Episode I - The Phantom Menace', scoreDetails: { value: 0.015527202696196438, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 2, weight: 0.5, value: 2.9571781158447266, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 7, weight: 0.5, value: 0.7771433591842651, details: [] } ] } }, { _id: ObjectId('573a1397f29313caabce77d9'), plot: 'After the rebels have been brutally overpowered by the Empire on their newly established base, Luke Skywalker takes advanced Jedi training with Master Yoda, while his friends are pursued by Darth Vader as part of his plan to capture Luke.', title: 'Star Wars: Episode V - The Empire Strikes Back', scoreDetails: { value: 0.015504807692307693, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 4, weight: 0.5, value: 2.712062358856201, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 5, weight: 0.5, value: 0.7810181975364685, details: [] } ] } }, { _id: ObjectId('573a1397f29313caabce8cdb'), plot: 'After rescuing Han Solo from the palace of Jabba the Hutt, the rebels attempt to destroy the second Death Star, while Luke struggles to make Vader return from the dark side of the Force.', title: 'Star Wars: Episode VI - Return of the Jedi', scoreDetails: { value: 0.015388257575757576, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 4, weight: 0.5, value: 2.712062358856201, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 6, weight: 0.5, value: 0.7782549858093262, details: [] } ] } }, { _id: ObjectId('573a13c0f29313caabd62f62'), plot: 'Anakin Skywalker and Ahsoka Tano must rescue the kidnapped son of Jabba the Hutt, but political intrigue complicates their mission.', title: 'Star Wars: The Clone Wars', scoreDetails: { value: 0.015141165755919854, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 1, weight: 0.5, value: 3.609658718109131, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 12, weight: 0.5, value: 0.7561323642730713, details: [] } ] } }, { _id: ObjectId('573a139af29313caabcf124d'), plot: 'As the Clone Wars near an end, the Sith Lord Darth Sidious steps out of the shadows, at which time Anakin succumbs to his emotions, becoming Darth Vader and putting his relationships with Obi-Wan and Padme at risk.', title: 'Star Wars: Episode III - Revenge of the Sith', scoreDetails: { value: 0.014854753521126762, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 4, weight: 0.5, value: 2.712062358856201, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 11, weight: 0.5, value: 0.756283700466156, details: [] } ] } }, { _id: ObjectId('573a139af29313caabcf1258'), plot: 'Ten years after initially meeting, Anakin Skywalker shares a forbidden romance with Padmè, while Obi-Wan investigates an assassination attempt on the Senator and discovers a secret clone army crafted for the Jedi.', title: 'Star Wars: Episode II - Attack of the Clones', scoreDetails: { value: 0.014391447368421052, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 4, weight: 0.5, value: 2.712062358856201, details: [] }, { inputPipelineName: 'vectorPipeline', rank: 16, weight: 0.5, value: 0.7400004267692566, details: [] } ] } }, { _id: ObjectId('573a1398f29313caabce9091'), plot: "A Duke's son leads desert warriors against the galactic emperor and his father's evil nemesis when they assassinate his father and free their desert world from the emperor's rule.", title: 'Dune', scoreDetails: { value: 0.008064516129032258, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 0, weight: 0.5 }, { inputPipelineName: 'vectorPipeline', rank: 2, weight: 0.5, value: 0.7947750091552734, details: [] } ] } }, { _id: ObjectId('573a1397f29313caabce6f53'), plot: 'In this Star Wars take-off, the peaceful planet of Jillucia has been nearly wiped out by the Gavanas, whose leader takes orders from his mother (played a comic actor in drag) rather than ...', title: 'Message from Space', scoreDetails: { value: 0.007936507936507936, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 0, weight: 0.5 }, { inputPipelineName: 'vectorPipeline', rank: 3, weight: 0.5, value: 0.7913960218429565, details: [] } ] } }, { _id: ObjectId('573a139df29313caabcfa90b'), plot: 'In this Star Wars take-off, the peaceful planet of Jillucia has been nearly wiped out by the Gavanas, whose leader takes orders from his mother (played a comic actor in drag) rather than ...', title: 'Message from Space', scoreDetails: { value: 0.007936507936507936, description: 'value output by reciprocal rank fusion algorithm, computed as sum of (weight * (1 / (60 + rank))) across input pipelines from which this document is output, from:', details: [ { inputPipelineName: 'fullTextPipeline', rank: 0, weight: 0.5 }, { inputPipelineName: 'vectorPipeline', rank: 3, weight: 0.5, value: 0.7913960218429565, details: [] } ] } } ]
关于查询
示例查询从语义搜索和全文搜索中检索排序后的搜索结果,并根据结果数组中的位置为结果中的文档分配倒数排名分数。倒数排名分数的计算公式如下:
1.0/{document position in the results + constant value}
然后,查询将每个文档的两次搜索得分相加,根据综合得分对文档进行排名,并对文档进行排序以返回单一结果。常量的值始终为60
。
查询权重
示例查询定义了以下管道的权重,以影响这些管道对最终得分的排名贡献:
vectorPipeline
= 0.5fullTextPipeline
= 0.5
您可以调整权重,使某种搜索方法更受重视。请注意,数字越小,权重越高。
加权倒数排名分数的计算公式如下:
weight x reciprocal rank
该 scoreDetails.value
显示管道的原始分数,然后使用倒数排名融合对其进行加权和组合。scoreDetails.details.value
显示加权倒数排名分数。
查询阶段
示例查询使用$rankFusion
阶段来独立执行语义查询和全文查询,然后对输入查询结果进行去重并合并成一个最终排序的结果集。它根据输入管道中出现的排名和管道权重,返回一个按排名排列的文档集合。具体而言,此阶段接收以下输入管道:
| 此管道包含 |
|
该示例查询使用以下阶段来合并语义搜索和文本搜索的结果,并在结果中返回一个排序文档列表: