set new attributes indexed if needed

This commit is contained in:
Quentin de Quelen 2020-01-27 08:52:36 +01:00 committed by qdequele
parent b1528f9466
commit 585bba43a0
No known key found for this signature in database
GPG key ID: B3F0A000EBF11745
10 changed files with 100 additions and 648 deletions

View file

@ -57,6 +57,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
let attribute_identifier = schema.clone().map(|s| s.identifier());
let attributes_searchable = schema.clone().map(|s| s.get_indexed_name());
let attributes_displayed = schema.clone().map(|s| s.get_displayed_name());
let index_new_fields = schema.map(|s| s.must_index_new_fields());
let settings = Settings {
ranking_rules,
@ -66,6 +67,7 @@ pub async fn get_all(ctx: Request<Data>) -> SResult<Response> {
attributes_displayed,
stop_words,
synonyms,
index_new_fields,
};
Ok(tide::Response::new(200).body_json(&settings).unwrap())
@ -99,6 +101,7 @@ pub async fn delete_all(ctx: Request<Data>) -> SResult<Response> {
attributes_displayed: UpdateState::Clear,
stop_words: UpdateState::Clear,
synonyms: UpdateState::Clear,
index_new_fields: UpdateState::Clear,
};
let update_id = index.settings_update(&mut writer, settings)?;

View file

@ -1,8 +1,11 @@
#![allow(dead_code)]
use serde_json::Value;
use std::error::Error;
use std::time::Duration;
use async_std::task::{block_on, sleep};
use async_std::io::prelude::*;
use assert_json_diff::assert_json_eq;
use http_service::Body;
use http_service_mock::{make_server, TestBackend};
use meilisearch_http::data::Data;
@ -41,9 +44,7 @@ pub fn enrich_server_with_movies_index(
let req = http::Request::post("/indexes")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
println!("enrich_server_with_movies_index: {:?}", res.status());
let _res = server.simulate(req).unwrap();
Ok(())
}
@ -96,9 +97,7 @@ pub fn enrich_server_with_movies_settings(
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
println!("enrich_server_with_movies_settings: {:?}", res.status());
let _res = server.simulate(req).unwrap();
block_on(sleep(Duration::from_secs(5)));
@ -113,9 +112,7 @@ pub fn enrich_server_with_movies_documents(
let req = http::Request::post("/indexes/movies/documents")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
println!("enrich_server_with_movies_documents: {:?}", res.status());
let _res = server.simulate(req).unwrap();
block_on(sleep(Duration::from_secs(5)));
@ -132,17 +129,17 @@ pub fn search(server: &mut TestBackend<Service<Data>>, query: &str, expect: Valu
block_on(res.into_body().read_to_end(&mut buf)).unwrap();
let response: Value = serde_json::from_slice(&buf).unwrap();
assert_json_eq!(expect, response, ordered: false)
assert_json_eq!(expect, response["hits"].clone(), ordered: false)
}
pub fn update_config(server: &mut TestBackend<Service<Data>>, config: Value) {
let body = config.to_string().into_bytes();
let req = http::Request::post("/indexes")
let req = http::Request::post("/indexes/movies/settings")
.body(Body::from(body))
.unwrap();
let res = server.simulate(req).unwrap();
assert_eq!(res.status(), 201);
assert_eq!(res.status(), 202);
block_on(sleep(Duration::from_secs(5)));
}

View file

@ -1,15 +1,6 @@
use std::convert::Into;
use std::time::Duration;
use assert_json_diff::assert_json_eq;
use async_std::io::prelude::*;
use async_std::task::{block_on, sleep};
use http_service::Body;
use http_service_mock::TestBackend;
use meilisearch_http::data::Data;
use serde_json::json;
use serde_json::Value;
use tide::server::Service;
mod common;
@ -628,15 +619,11 @@ fn basic_search() {
}
#[test]
fn search_with_settings_change() {
fn search_with_settings_basic() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
common::enrich_server_with_movies_settings(&mut server).unwrap();
common::enrich_server_with_movies_documents(&mut server).unwrap();
// Basic
let config = json!({
"rankingRules": [
"_typo",
@ -735,9 +722,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Set with stop words
#[test]
fn search_with_settings_stop_words() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -780,6 +770,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=the%20avangers&limit=3";
let response = json!([
@ -837,9 +828,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Set with synonyms
#[test]
fn search_with_settings_synonyms() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -887,6 +881,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3";
let response = json!([
@ -944,9 +939,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Set asc(vote_average) in ranking rules
#[test]
fn search_with_settings_ranking_rules() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -989,6 +987,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3";
let response = json!([
@ -1046,9 +1045,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Remove Title from attributesSearchable
#[test]
fn search_with_settings_attributes_searchable() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -1090,6 +1092,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3";
let response = json!([
@ -1147,9 +1150,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Remove Attributes displayed
#[test]
fn search_with_settings_attributes_displayed() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -1186,6 +1192,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3";
let response = json!([
@ -1213,9 +1220,12 @@ fn search_with_settings_change() {
]);
common::search(&mut server, query, response);
}
////////////////////////////////////////////////////////////////////////////////////////////////
// Reoder attributesSearchable
#[test]
fn search_with_settings_attributes_searchable_2() {
let mut server = common::setup_server().unwrap();
common::enrich_server_with_movies_index(&mut server).unwrap();
let config = json!({
"rankingRules": [
@ -1252,6 +1262,7 @@ fn search_with_settings_change() {
});
common::update_config(&mut server, config);
common::enrich_server_with_movies_documents(&mut server).unwrap();
let query = "q=avangers&limit=3";
let response = json!([

View file

@ -132,6 +132,7 @@ fn write_all_and_delete() {
"attributesDisplayed": null,
"stopWords": null,
"synonyms": null,
"indexNewFields": true,
});
assert_json_eq!(json, res_value, ordered: false);
@ -312,7 +313,8 @@ fn write_all_and_update() {
"synonyms": {
"wolverine": ["xmen", "logan"],
"logan": ["wolverine", "xmen"],
}
},
"indexNewFields": true
});
assert_json_eq!(res_expected, res_value, ordered: false);