2024-07-08 19:43:21 +02:00
use actix_web ::http ::header ::ACCEPT_ENCODING ;
2022-10-13 00:56:57 +02:00
use actix_web ::test ;
2023-05-02 20:03:45 +02:00
use meili_snap ::* ;
2022-10-13 00:56:57 +02:00
use urlencoding ::encode as urlencode ;
2021-03-15 10:36:12 +01:00
2022-10-20 18:00:07 +02:00
use crate ::common ::encoder ::Encoder ;
2024-11-06 13:54:20 +01:00
use crate ::common ::{
shared_does_not_exists_index , shared_empty_index , shared_index_with_test_set ,
GetAllDocumentsOptions , Server , Value ,
} ;
2023-09-11 16:50:53 +02:00
use crate ::json ;
2022-10-20 18:00:07 +02:00
2021-02-19 19:43:32 +01:00
// TODO: partial test since we are testing error, amd error is not yet fully implemented in
// transplant
#[ actix_rt::test ]
async fn get_unexisting_index_single_document ( ) {
2024-11-04 21:26:50 +01:00
let ( _response , code ) = shared_does_not_exists_index ( ) . await . get_document ( 1 , None ) . await ;
2021-06-15 22:21:52 +02:00
assert_eq! ( code , 404 ) ;
2021-02-19 19:43:32 +01:00
}
2021-02-22 14:32:48 +01:00
#[ actix_rt::test ]
2021-10-21 14:42:01 +02:00
async fn error_get_unexisting_document ( ) {
2024-10-29 22:49:06 +01:00
let server = Server ::new_shared ( ) ;
let index = server . unique_index ( ) ;
let ( task , _code ) = index . create ( None ) . await ;
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2021-10-21 14:42:01 +02:00
let ( response , code ) = index . get_document ( 1 , None ) . await ;
let expected_response = json! ( {
2021-10-26 19:36:48 +02:00
" message " : " Document `1` not found. " ,
2021-10-21 14:42:01 +02:00
" code " : " document_not_found " ,
" type " : " invalid_request " ,
2023-01-19 16:10:05 +01:00
" link " : " https://docs.meilisearch.com/errors#document_not_found "
2021-10-21 14:42:01 +02:00
} ) ;
assert_eq! ( response , expected_response ) ;
2021-06-15 22:21:52 +02:00
assert_eq! ( code , 404 ) ;
2021-02-22 14:32:48 +01:00
}
#[ actix_rt::test ]
async fn get_document ( ) {
2024-10-29 22:49:06 +01:00
let server = Server ::new_shared ( ) ;
let index = server . unique_index ( ) ;
let ( task , _code ) = index . create ( None ) . await ;
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2023-09-11 16:50:53 +02:00
let documents = json! ( [
2021-02-22 14:32:48 +01:00
{
" id " : 0 ,
2022-05-25 11:51:26 +02:00
" nested " : { " content " : " foobar " } ,
2021-02-22 14:32:48 +01:00
}
] ) ;
2024-10-29 22:49:06 +01:00
let ( task , code ) = index . add_documents ( documents , None ) . await ;
2021-03-17 13:54:17 +01:00
assert_eq! ( code , 202 ) ;
2024-10-29 22:49:06 +01:00
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2021-03-15 18:11:10 +01:00
let ( response , code ) = index . get_document ( 0 , None ) . await ;
2021-02-22 14:32:48 +01:00
assert_eq! ( code , 200 ) ;
2021-03-15 18:11:10 +01:00
assert_eq! (
response ,
2023-09-11 16:50:53 +02:00
json! ( {
2021-03-15 18:11:10 +01:00
" id " : 0 ,
2022-05-25 11:51:26 +02:00
" nested " : { " content " : " foobar " } ,
2021-03-15 18:11:10 +01:00
} )
) ;
2022-05-25 11:51:26 +02:00
2024-06-05 23:40:29 +02:00
let ( response , code ) = index . get_document ( 0 , Some ( json! ( { " fields " : [ " id " ] } ) ) ) . await ;
2022-05-25 11:51:26 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! (
response ,
2023-09-11 16:50:53 +02:00
json! ( {
2022-05-25 11:51:26 +02:00
" id " : 0 ,
} )
) ;
2024-06-05 23:40:29 +02:00
let ( response , code ) =
index . get_document ( 0 , Some ( json! ( { " fields " : [ " nested.content " ] } ) ) ) . await ;
2022-05-25 11:51:26 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! (
response ,
2023-09-11 16:50:53 +02:00
json! ( {
2022-05-25 11:51:26 +02:00
" nested " : { " content " : " foobar " } ,
} )
) ;
2021-02-22 14:32:48 +01:00
}
2021-02-19 19:43:32 +01:00
#[ actix_rt::test ]
2021-10-21 14:42:01 +02:00
async fn error_get_unexisting_index_all_documents ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_does_not_exists_index ( ) . await ;
2024-10-29 22:49:06 +01:00
let ( response , code ) = index . get_all_documents ( GetAllDocumentsOptions ::default ( ) ) . await ;
2021-10-21 14:42:01 +02:00
let expected_response = json! ( {
2024-11-04 21:26:50 +01:00
" message " : " Index `DOES_NOT_EXISTS` not found. " ,
2021-10-21 14:42:01 +02:00
" code " : " index_not_found " ,
" type " : " invalid_request " ,
2023-01-19 16:10:05 +01:00
" link " : " https://docs.meilisearch.com/errors#index_not_found "
2021-10-21 14:42:01 +02:00
} ) ;
assert_eq! ( response , expected_response ) ;
2021-06-15 22:21:52 +02:00
assert_eq! ( code , 404 ) ;
2021-02-19 19:43:32 +01:00
}
2021-02-19 19:46:45 +01:00
#[ actix_rt::test ]
2021-12-02 16:03:26 +01:00
async fn get_no_document ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_empty_index ( ) . await ;
2021-02-19 19:46:45 +01:00
2022-10-20 18:00:07 +02:00
let ( response , code ) = index . get_all_documents ( GetAllDocumentsOptions ::default ( ) ) . await ;
2021-02-19 19:46:45 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert! ( response [ " results " ] . as_array ( ) . unwrap ( ) . is_empty ( ) ) ;
2021-02-19 19:46:45 +01:00
}
2021-02-19 19:55:00 +01:00
#[ actix_rt::test ]
async fn get_all_documents_no_options ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_index_with_test_set ( ) . await ;
2021-02-19 19:55:00 +01:00
2022-10-20 18:00:07 +02:00
let ( response , code ) = index . get_all_documents ( GetAllDocumentsOptions ::default ( ) ) . await ;
2021-02-19 19:55:00 +01:00
assert_eq! ( code , 200 ) ;
2024-11-02 22:25:33 +01:00
let results = response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( results . len ( ) , 20 ) ;
2023-09-11 16:50:53 +02:00
let first = json! ( {
2021-02-22 14:23:17 +01:00
" id " :0 ,
" isActive " :false ,
" balance " :" $2,668.55 " ,
" picture " :" http://placehold.it/32x32 " ,
" age " :36 ,
" color " :" Green " ,
" name " :" Lucas Hess " ,
" gender " :" male " ,
" email " :" lucashess@chorizon.com " ,
" phone " :" +1 (998) 478-2597 " ,
" address " :" 412 Losee Terrace, Blairstown, Georgia, 2825 " ,
" about " :" Mollit ad in exercitation quis. Anim est ut consequat fugiat duis magna aliquip velit nisi. Commodo eiusmod est consequat proident consectetur aliqua enim fugiat. Aliqua adipisicing laboris elit proident enim veniam laboris mollit. Incididunt fugiat minim ad nostrud deserunt tempor in. Id irure officia labore qui est labore nulla nisi. Magna sit quis tempor esse consectetur amet labore duis aliqua consequat. \r \n " ,
" registered " :" 2016-06-21T09:30:25 -02:00 " ,
" latitude " :- 44.174957 ,
" longitude " :- 145.725388 ,
" tags " :[ " bug "
, " bug " ] } ) ;
2024-11-02 22:25:33 +01:00
assert_eq! ( first , results [ 0 ] ) ;
2021-02-22 14:23:17 +01:00
}
2022-10-13 00:56:57 +02:00
#[ actix_rt::test ]
async fn get_all_documents_no_options_with_response_compression ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_index_with_test_set ( ) . await ;
2022-10-13 00:56:57 +02:00
2024-11-04 21:26:50 +01:00
let app = Server ::new_shared ( ) . init_web_app ( ) . await ;
2022-10-13 00:56:57 +02:00
let req = test ::TestRequest ::get ( )
2024-10-29 22:49:06 +01:00
. uri ( & format! ( " /indexes/ {} /documents? " , urlencode ( & index . uid ) ) )
2022-10-13 00:56:57 +02:00
. insert_header ( ( ACCEPT_ENCODING , " gzip " ) )
. to_request ( ) ;
let res = test ::call_service ( & app , req ) . await ;
assert_eq! ( res . status ( ) , 200 ) ;
let bytes = test ::read_body ( res ) . await ;
let decoded = Encoder ::Gzip . decode ( bytes ) ;
let parsed_response =
2022-10-13 15:35:31 +02:00
serde_json ::from_slice ::< Value > ( decoded . into ( ) . as_ref ( ) ) . expect ( " Expecting valid json " ) ;
2022-10-13 00:56:57 +02:00
let arr = parsed_response [ " results " ] . as_array ( ) . unwrap ( ) ;
assert_eq! ( arr . len ( ) , 20 ) ;
}
2021-02-22 14:23:17 +01:00
#[ actix_rt::test ]
async fn test_get_all_documents_limit ( ) {
2024-11-06 13:54:20 +01:00
let index = shared_index_with_test_set ( ) . await ;
2021-02-22 14:23:17 +01:00
2021-03-15 18:11:10 +01:00
let ( response , code ) = index
2022-10-20 18:00:07 +02:00
. get_all_documents ( GetAllDocumentsOptions { limit : Some ( 5 ) , .. Default ::default ( ) } )
2021-03-15 18:11:10 +01:00
. await ;
2021-02-22 14:23:17 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 5 ) ;
assert_eq! ( response [ " results " ] [ 0 ] [ " id " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 5 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-02-22 14:23:17 +01:00
}
#[ actix_rt::test ]
async fn test_get_all_documents_offset ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_index_with_test_set ( ) . await ;
2021-02-22 14:23:17 +01:00
2021-03-15 18:11:10 +01:00
let ( response , code ) = index
2022-10-20 18:00:07 +02:00
. get_all_documents ( GetAllDocumentsOptions { offset : Some ( 5 ) , .. Default ::default ( ) } )
2021-03-15 18:11:10 +01:00
. await ;
2021-02-22 14:23:17 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
assert_eq! ( response [ " results " ] [ 0 ] [ " id " ] , json! ( 5 ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 5 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-02-22 14:23:17 +01:00
}
#[ actix_rt::test ]
async fn test_get_all_documents_attributes_to_retrieve ( ) {
2024-11-04 21:26:50 +01:00
let index = shared_index_with_test_set ( ) . await ;
2021-03-15 18:11:10 +01:00
let ( response , code ) = index
. get_all_documents ( GetAllDocumentsOptions {
2024-06-05 23:40:29 +02:00
fields : Some ( vec! [ " name " ] ) ,
2021-03-15 18:11:10 +01:00
.. Default ::default ( )
} )
. await ;
2021-02-22 14:23:17 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 1 ) ;
assert! ( results [ " name " ] ! = json! ( null ) ) ;
}
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-02-22 14:23:17 +01:00
2024-06-06 16:31:07 +02:00
let ( response , code ) = index . get_all_documents_raw ( " ?fields= " ) . await ;
2021-02-22 14:23:17 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 0 ) ;
}
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-02-22 14:23:17 +01:00
2021-04-17 17:33:36 +02:00
let ( response , code ) = index
. get_all_documents ( GetAllDocumentsOptions {
2024-06-05 23:40:29 +02:00
fields : Some ( vec! [ " wrong " ] ) ,
2021-04-17 17:33:36 +02:00
.. Default ::default ( )
} )
. await ;
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 0 ) ;
}
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-04-17 17:33:36 +02:00
2021-03-15 18:11:10 +01:00
let ( response , code ) = index
. get_all_documents ( GetAllDocumentsOptions {
2024-06-05 23:40:29 +02:00
fields : Some ( vec! [ " name " , " tags " ] ) ,
2021-03-15 18:11:10 +01:00
.. Default ::default ( )
} )
. await ;
2021-02-22 14:23:17 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 2 ) ;
assert! ( results [ " name " ] ! = json! ( null ) ) ;
assert! ( results [ " tags " ] ! = json! ( null ) ) ;
}
2021-04-17 17:33:36 +02:00
let ( response , code ) = index
2024-06-05 23:40:29 +02:00
. get_all_documents ( GetAllDocumentsOptions { fields : Some ( vec! [ " * " ] ) , .. Default ::default ( ) } )
2021-04-17 17:33:36 +02:00
. await ;
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 16 ) ;
}
2021-04-17 17:33:36 +02:00
let ( response , code ) = index
. get_all_documents ( GetAllDocumentsOptions {
2024-06-05 23:40:29 +02:00
fields : Some ( vec! [ " * " , " wrong " ] ) ,
2021-04-17 17:33:36 +02:00
.. Default ::default ( )
} )
. await ;
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
for results in response [ " results " ] . as_array ( ) . unwrap ( ) {
assert_eq! ( results . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 16 ) ;
}
2021-02-19 19:55:00 +01:00
}
2021-03-15 10:36:12 +01:00
2022-05-25 19:40:43 +02:00
#[ actix_rt::test ]
async fn get_document_s_nested_attributes_to_retrieve ( ) {
2024-10-29 22:49:06 +01:00
let server = Server ::new_shared ( ) ;
let index = server . unique_index ( ) ;
let ( task , _code ) = index . create ( None ) . await ;
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2022-05-25 19:40:43 +02:00
let documents = json! ( [
{
" id " : 0 ,
" content.truc " : " foobar " ,
} ,
{
" id " : 1 ,
" content " : {
" truc " : " foobar " ,
" machin " : " bidule " ,
} ,
} ,
] ) ;
2024-10-29 22:49:06 +01:00
let ( task , code ) = index . add_documents ( documents , None ) . await ;
2022-05-25 19:40:43 +02:00
assert_eq! ( code , 202 ) ;
2024-10-29 22:49:06 +01:00
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2022-05-25 19:40:43 +02:00
2024-06-05 23:40:29 +02:00
let ( response , code ) = index . get_document ( 0 , Some ( json! ( { " fields " : [ " content " ] } ) ) ) . await ;
2022-05-25 19:40:43 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! ( response , json! ( { } ) ) ;
2024-06-05 23:40:29 +02:00
let ( response , code ) = index . get_document ( 1 , Some ( json! ( { " fields " : [ " content " ] } ) ) ) . await ;
2022-05-25 19:40:43 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! (
response ,
json! ( {
" content " : {
" truc " : " foobar " ,
" machin " : " bidule " ,
} ,
} )
) ;
2024-06-05 23:40:29 +02:00
let ( response , code ) = index . get_document ( 0 , Some ( json! ( { " fields " : [ " content.truc " ] } ) ) ) . await ;
2022-05-25 19:40:43 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! (
response ,
json! ( {
" content.truc " : " foobar " ,
} )
) ;
2024-06-05 23:40:29 +02:00
let ( response , code ) = index . get_document ( 1 , Some ( json! ( { " fields " : [ " content.truc " ] } ) ) ) . await ;
2022-05-25 19:40:43 +02:00
assert_eq! ( code , 200 ) ;
assert_eq! (
response ,
json! ( {
" content " : {
" truc " : " foobar " ,
} ,
} )
) ;
}
2021-03-15 10:36:12 +01:00
#[ actix_rt::test ]
2022-05-25 11:51:26 +02:00
async fn get_documents_displayed_attributes_is_ignored ( ) {
2024-10-29 22:49:06 +01:00
let server = Server ::new_shared ( ) ;
let index = server . unique_index ( ) ;
2021-03-15 10:36:12 +01:00
index . load_test_set ( ) . await ;
2024-11-04 21:26:50 +01:00
index . update_settings ( json! ( { " displayedAttributes " : [ " gender " ] } ) ) . await ;
2021-03-15 10:36:12 +01:00
2022-10-20 18:00:07 +02:00
let ( response , code ) = index . get_all_documents ( GetAllDocumentsOptions ::default ( ) ) . await ;
2021-03-15 10:36:12 +01:00
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response [ " results " ] . as_array ( ) . unwrap ( ) . len ( ) , 20 ) ;
2022-10-20 18:00:07 +02:00
assert_eq! ( response [ " results " ] [ 0 ] . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 16 ) ;
2022-05-25 11:51:26 +02:00
assert! ( response [ " results " ] [ 0 ] [ " gender " ] ! = json! ( null ) ) ;
assert_eq! ( response [ " offset " ] , json! ( 0 ) ) ;
assert_eq! ( response [ " limit " ] , json! ( 20 ) ) ;
assert_eq! ( response [ " total " ] , json! ( 77 ) ) ;
2021-03-15 10:36:12 +01:00
let ( response , code ) = index . get_document ( 0 , None ) . await ;
assert_eq! ( code , 200 ) ;
2022-05-25 11:51:26 +02:00
assert_eq! ( response . as_object ( ) . unwrap ( ) . keys ( ) . count ( ) , 16 ) ;
2021-03-15 10:36:12 +01:00
assert! ( response . as_object ( ) . unwrap ( ) . get ( " gender " ) . is_some ( ) ) ;
}
2023-05-02 20:03:45 +02:00
#[ actix_rt::test ]
2023-05-03 19:43:23 +02:00
async fn get_document_by_filter ( ) {
2024-10-29 22:49:06 +01:00
let server = Server ::new_shared ( ) ;
let index = server . unique_index ( ) ;
2023-05-02 20:03:45 +02:00
index . update_settings_filterable_attributes ( json! ( [ " color " ] ) ) . await ;
2024-10-29 22:49:06 +01:00
let ( task , _code ) = i ndex
2023-05-02 20:03:45 +02:00
. add_documents (
json! ( [
{ " id " : 0 , " color " : " red " } ,
{ " id " : 1 , " color " : " blue " } ,
{ " id " : 2 , " color " : " blue " } ,
{ " id " : 3 } ,
] ) ,
Some ( " id " ) ,
)
. await ;
2024-10-29 22:49:06 +01:00
index . wait_task ( task . uid ( ) ) . await . succeeded ( ) ;
2023-05-02 20:03:45 +02:00
let ( response , code ) = index . get_document_by_filter ( json! ( { } ) ) . await ;
2023-05-03 19:43:23 +02:00
let ( response2 , code2 ) = index . get_all_documents_raw ( " " ) . await ;
2023-05-02 20:03:45 +02:00
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" id " : 0 ,
" color " : " red "
} ,
{
" id " : 1 ,
" color " : " blue "
} ,
{
" id " : 2 ,
" color " : " blue "
} ,
{
" id " : 3
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 4
}
" ###);
2023-05-03 19:43:23 +02:00
assert_eq! ( code , code2 ) ;
assert_eq! ( response , response2 ) ;
2023-05-02 20:03:45 +02:00
let ( response , code ) = index . get_document_by_filter ( json! ( { " filter " : " color = blue " } ) ) . await ;
2023-05-03 19:43:23 +02:00
let ( response2 , code2 ) = index . get_all_documents_raw ( " ?filter=color=blue " ) . await ;
2023-05-02 20:03:45 +02:00
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" id " : 1 ,
" color " : " blue "
} ,
{
" id " : 2 ,
" color " : " blue "
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 2
}
" ###);
2023-05-03 19:43:23 +02:00
assert_eq! ( code , code2 ) ;
assert_eq! ( response , response2 ) ;
2023-05-02 20:03:45 +02:00
let ( response , code ) = index
. get_document_by_filter ( json! ( { " offset " : 1 , " limit " : 1 , " filter " : " color != blue " } ) )
. await ;
2023-05-03 19:43:23 +02:00
let ( response2 , code2 ) =
index . get_all_documents_raw ( " ?filter=color!=blue&offset=1&limit=1 " ) . await ;
2023-05-02 20:03:45 +02:00
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" id " : 3
}
] ,
" offset " : 1 ,
" limit " : 1 ,
" total " : 2
}
" ###);
2023-05-03 19:43:23 +02:00
assert_eq! ( code , code2 ) ;
assert_eq! ( response , response2 ) ;
2023-05-02 20:03:45 +02:00
let ( response , code ) = index
2023-05-03 20:43:57 +02:00
. get_document_by_filter (
json! ( { " limit " : 1 , " filter " : " color != blue " , " fields " : [ " color " ] } ) ,
)
2023-05-02 20:03:45 +02:00
. await ;
2023-05-03 19:43:23 +02:00
let ( response2 , code2 ) =
index . get_all_documents_raw ( " ?limit=1&filter=color!=blue&fields=color " ) . await ;
2023-05-02 20:03:45 +02:00
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" color " : " red "
}
] ,
" offset " : 0 ,
" limit " : 1 ,
" total " : 2
}
" ###);
2023-05-03 19:43:23 +02:00
assert_eq! ( code , code2 ) ;
assert_eq! ( response , response2 ) ;
// Now testing more complex filter that the get route can't represent
let ( response , code ) =
index . get_document_by_filter ( json! ( { " filter " : [ [ " color = blue " , " color = red " ] ] } ) ) . await ;
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" id " : 0 ,
" color " : " red "
} ,
{
" id " : 1 ,
" color " : " blue "
} ,
{
" id " : 2 ,
" color " : " blue "
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 3
}
" ###);
let ( response , code ) = index
. get_document_by_filter ( json! ( { " filter " : [ [ " color != blue " ] , " color EXISTS " ] } ) )
. await ;
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( json_string! ( response , { " .enqueuedAt " = > " [date] " } ) , @ r ###"
{
" results " : [
{
" id " : 0 ,
" color " : " red "
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 1
}
" ###);
2023-05-02 20:03:45 +02:00
}
2024-06-05 23:40:29 +02:00
#[ actix_rt::test ]
async fn get_document_with_vectors ( ) {
let server = Server ::new ( ) . await ;
let index = server . index ( " doggo " ) ;
let ( value , code ) = server . set_features ( json! ( { " vectorStore " : true } ) ) . await ;
snapshot! ( code , @ " 200 OK " ) ;
snapshot! ( value , @ r ###"
{
" vectorStore " : true ,
" metrics " : false ,
2024-07-09 11:15:57 +02:00
" logsRoute " : false ,
2024-07-17 11:13:37 +02:00
" editDocumentsByFunction " : false ,
" containsFilter " : false
2024-06-05 23:40:29 +02:00
}
" ###);
let ( response , code ) = index
. update_settings ( json! ( {
" embedders " : {
" manual " : {
" source " : " userProvided " ,
" dimensions " : 3 ,
}
} ,
} ) )
. await ;
snapshot! ( code , @ " 202 Accepted " ) ;
2024-10-29 22:49:06 +01:00
server . wait_task ( response . uid ( ) ) . await . succeeded ( ) ;
2024-06-05 23:40:29 +02:00
let documents = json! ( [
{ " id " : 0 , " name " : " kefir " , " _vectors " : { " manual " : [ 0 , 0 , 0 ] } } ,
{ " id " : 1 , " name " : " echo " , " _vectors " : { " manual " : null } } ,
] ) ;
let ( value , code ) = index . add_documents ( documents , None ) . await ;
snapshot! ( code , @ " 202 Accepted " ) ;
2024-10-29 22:49:06 +01:00
index . wait_task ( value . uid ( ) ) . await . succeeded ( ) ;
2024-06-05 23:40:29 +02:00
// by default you shouldn't see the `_vectors` object
let ( documents , _code ) = index . get_all_documents ( Default ::default ( ) ) . await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" results " : [
{
" id " : 0 ,
" name " : " kefir "
} ,
{
" id " : 1 ,
" name " : " echo "
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 2
}
" ###);
let ( documents , _code ) = index . get_document ( 0 , None ) . await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" id " : 0 ,
" name " : " kefir "
}
" ###);
// if we try to retrieve the vectors with the `fields` parameter they
// still shouldn't be displayed
let ( documents , _code ) = index
. get_all_documents ( GetAllDocumentsOptions {
fields : Some ( vec! [ " name " , " _vectors " ] ) ,
.. Default ::default ( )
} )
. await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" results " : [
{
" name " : " kefir "
} ,
{
" name " : " echo "
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 2
}
" ###);
let ( documents , _code ) =
index . get_document ( 0 , Some ( json! ( { " fields " : [ " name " , " _vectors " ] } ) ) ) . await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" name " : " kefir "
}
" ###);
// If we specify the retrieve vectors boolean and nothing else we should get the vectors
let ( documents , _code ) = index
. get_all_documents ( GetAllDocumentsOptions { retrieve_vectors : true , .. Default ::default ( ) } )
. await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" results " : [
{
" id " : 0 ,
" name " : " kefir " ,
" _vectors " : {
" manual " : {
" embeddings " : [
[
0.0 ,
0.0 ,
0.0
]
] ,
2024-06-12 18:13:34 +02:00
" regenerate " : false
2024-06-05 23:40:29 +02:00
}
}
} ,
{
" id " : 1 ,
" name " : " echo " ,
2024-07-16 10:44:23 +02:00
" _vectors " : {
" manual " : {
" embeddings " : [ ] ,
" regenerate " : false
}
}
2024-06-05 23:40:29 +02:00
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 2
}
" ###);
let ( documents , _code ) = index . get_document ( 0 , Some ( json! ( { " retrieveVectors " : true } ) ) ) . await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" id " : 0 ,
" name " : " kefir " ,
" _vectors " : {
" manual " : {
" embeddings " : [
[
0.0 ,
0.0 ,
0.0
]
] ,
2024-06-12 18:13:34 +02:00
" regenerate " : false
2024-06-05 23:40:29 +02:00
}
}
}
" ###);
// If we specify the retrieve vectors boolean and exclude vectors form the `fields` we should still get the vectors
let ( documents , _code ) = index
. get_all_documents ( GetAllDocumentsOptions {
retrieve_vectors : true ,
fields : Some ( vec! [ " name " ] ) ,
.. Default ::default ( )
} )
. await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" results " : [
{
" name " : " kefir " ,
" _vectors " : {
" manual " : {
" embeddings " : [
[
0.0 ,
0.0 ,
0.0
]
] ,
2024-06-12 18:13:34 +02:00
" regenerate " : false
2024-06-05 23:40:29 +02:00
}
}
} ,
{
" name " : " echo " ,
2024-07-16 10:44:23 +02:00
" _vectors " : {
" manual " : {
" embeddings " : [ ] ,
" regenerate " : false
}
}
2024-06-05 23:40:29 +02:00
}
] ,
" offset " : 0 ,
" limit " : 20 ,
" total " : 2
}
" ###);
let ( documents , _code ) =
index . get_document ( 0 , Some ( json! ( { " retrieveVectors " : true , " fields " : [ " name " ] } ) ) ) . await ;
snapshot! ( json_string! ( documents ) , @ r ###"
{
" name " : " kefir " ,
" _vectors " : {
" manual " : {
" embeddings " : [
[
0.0 ,
0.0 ,
0.0
]
] ,
2024-06-12 18:13:34 +02:00
" regenerate " : false
2024-06-05 23:40:29 +02:00
}
}
}
" ###);
}