mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-04 20:37:15 +02:00
adapt meilisearch-http to the new schemaless option
This commit is contained in:
parent
21d122a870
commit
4f0ead625b
13 changed files with 143 additions and 359 deletions
|
@ -1,3 +1,4 @@
|
|||
|
||||
use std::collections::{BTreeSet, HashSet};
|
||||
|
||||
use http::StatusCode;
|
||||
|
@ -7,6 +8,7 @@ use serde_json::Value;
|
|||
use tide::querystring::ContextExt as QSContextExt;
|
||||
use tide::response::IntoResponse;
|
||||
use tide::{Context, Response};
|
||||
use meilisearch_core::settings::Settings;
|
||||
|
||||
use crate::error::{ResponseError, SResult};
|
||||
use crate::helpers::tide::ContextExt;
|
||||
|
@ -117,27 +119,13 @@ pub async fn get_all_documents(ctx: Context<Data>) -> SResult<Response> {
|
|||
Ok(tide::response::json(response_body))
|
||||
}
|
||||
|
||||
fn infered_schema(document: &IndexMap<String, Value>, identifier: Option<String>) -> Option<meilisearch_schema::Schema> {
|
||||
use meilisearch_schema::{SchemaBuilder, DISPLAYED, INDEXED};
|
||||
|
||||
let mut identifier = identifier;
|
||||
fn find_identifier(document: &IndexMap<String, Value>) -> Option<String> {
|
||||
for key in document.keys() {
|
||||
if identifier.is_none() && key.to_lowercase().contains("id") {
|
||||
identifier = Some(key.to_string());
|
||||
break;
|
||||
if key.to_lowercase().contains("id") {
|
||||
return Some(key.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
match identifier {
|
||||
Some(identifier) => {
|
||||
let mut builder = SchemaBuilder::with_identifier(identifier);
|
||||
for key in document.keys() {
|
||||
builder.new_attribute(key, DISPLAYED | INDEXED);
|
||||
}
|
||||
Some(builder.build())
|
||||
}
|
||||
None => None,
|
||||
}
|
||||
return None
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
|
@ -165,14 +153,22 @@ async fn update_multiple_documents(mut ctx: Context<Data>, is_partial: bool) ->
|
|||
.schema(&reader)
|
||||
.map_err(ResponseError::internal)?;
|
||||
if current_schema.is_none() {
|
||||
match data.first().and_then(|docs| infered_schema(docs, query.identifier)) {
|
||||
Some(schema) => {
|
||||
index
|
||||
.schema_update(&mut update_writer, schema)
|
||||
.map_err(ResponseError::internal)?;
|
||||
let id = match query.identifier {
|
||||
Some(id) => id,
|
||||
None => {
|
||||
match data.first().and_then(|docs| find_identifier(docs)) {
|
||||
Some(id) => id,
|
||||
None => return Err(ResponseError::bad_request("Could not infer a schema")),
|
||||
}
|
||||
}
|
||||
None => return Err(ResponseError::bad_request("Could not infer a schema")),
|
||||
}
|
||||
};
|
||||
let settings = Settings {
|
||||
attribute_identifier: Some(id),
|
||||
..Settings::default()
|
||||
};
|
||||
index
|
||||
.settings_update(&mut update_writer, settings.into())
|
||||
.map_err(ResponseError::internal)?;
|
||||
}
|
||||
|
||||
let mut document_addition = if is_partial {
|
||||
|
|
|
@ -2,19 +2,16 @@ use chrono::{DateTime, Utc};
|
|||
use http::StatusCode;
|
||||
use log::error;
|
||||
use meilisearch_core::ProcessedUpdateResult;
|
||||
use meilisearch_schema::{Schema, SchemaBuilder};
|
||||
// use meilisearch_schema::Schema;
|
||||
use rand::seq::SliceRandom;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::json;
|
||||
use tide::querystring::ContextExt as QSContextExt;
|
||||
use tide::response::IntoResponse;
|
||||
use tide::{Context, Response};
|
||||
|
||||
use crate::error::{ResponseError, SResult};
|
||||
use crate::helpers::tide::ContextExt;
|
||||
use crate::models::schema::SchemaBody;
|
||||
use crate::models::token::ACL::*;
|
||||
use crate::routes::document::IndexUpdateResponse;
|
||||
use crate::Data;
|
||||
|
||||
fn generate_uid() -> String {
|
||||
|
@ -124,7 +121,7 @@ pub async fn get_index(ctx: Context<Data>) -> SResult<Response> {
|
|||
struct IndexCreateRequest {
|
||||
name: String,
|
||||
uid: Option<String>,
|
||||
schema: Option<SchemaBody>,
|
||||
// schema: Option<SchemaBody>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize)]
|
||||
|
@ -132,9 +129,9 @@ struct IndexCreateRequest {
|
|||
struct IndexCreateResponse {
|
||||
name: String,
|
||||
uid: String,
|
||||
schema: Option<SchemaBody>,
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
update_id: Option<u64>,
|
||||
// schema: Option<SchemaBody>,
|
||||
// #[serde(skip_serializing_if = "Option::is_none")]
|
||||
// update_id: Option<u64>,
|
||||
created_at: DateTime<Utc>,
|
||||
updated_at: DateTime<Utc>,
|
||||
}
|
||||
|
@ -165,30 +162,29 @@ pub async fn create_index(mut ctx: Context<Data>) -> SResult<Response> {
|
|||
};
|
||||
|
||||
let mut writer = db.main_write_txn().map_err(ResponseError::internal)?;
|
||||
let mut update_writer = db.update_write_txn().map_err(ResponseError::internal)?;
|
||||
|
||||
created_index
|
||||
.main
|
||||
.put_name(&mut writer, &body.name)
|
||||
.map_err(ResponseError::internal)?;
|
||||
|
||||
let schema: Option<Schema> = body.schema.clone().map(Into::into);
|
||||
let mut response_update_id = None;
|
||||
if let Some(schema) = schema {
|
||||
let update_id = created_index
|
||||
.schema_update(&mut update_writer, schema)
|
||||
.map_err(ResponseError::internal)?;
|
||||
response_update_id = Some(update_id)
|
||||
}
|
||||
// let schema: Option<Schema> = body.schema.clone().map(Into::into);
|
||||
// let mut response_update_id = None;
|
||||
// if let Some(schema) = schema {
|
||||
// let update_id = created_index
|
||||
// .schema_update(&mut update_writer, schema)
|
||||
// .map_err(ResponseError::internal)?;
|
||||
// response_update_id = Some(update_id)
|
||||
// }
|
||||
|
||||
writer.commit().map_err(ResponseError::internal)?;
|
||||
update_writer.commit().map_err(ResponseError::internal)?;
|
||||
// writer.commit().map_err(ResponseError::internal)?;
|
||||
// update_writer.commit().map_err(ResponseError::internal)?;
|
||||
|
||||
let response_body = IndexCreateResponse {
|
||||
name: body.name,
|
||||
uid,
|
||||
schema: body.schema,
|
||||
update_id: response_update_id,
|
||||
// schema: body.schema,
|
||||
// update_id: update_id,
|
||||
created_at: Utc::now(),
|
||||
updated_at: Utc::now(),
|
||||
};
|
||||
|
@ -263,78 +259,6 @@ pub async fn update_index(mut ctx: Context<Data>) -> SResult<Response> {
|
|||
.into_response())
|
||||
}
|
||||
|
||||
#[derive(Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase", deny_unknown_fields)]
|
||||
struct SchemaParams {
|
||||
raw: bool,
|
||||
}
|
||||
|
||||
pub async fn get_index_schema(ctx: Context<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(IndexesRead)?;
|
||||
|
||||
let index = ctx.index()?;
|
||||
|
||||
// Tide doesn't support "no query param"
|
||||
let params: SchemaParams = ctx.url_query().unwrap_or_default();
|
||||
|
||||
let db = &ctx.state().db;
|
||||
let reader = db.main_read_txn().map_err(ResponseError::internal)?;
|
||||
|
||||
let schema = index
|
||||
.main
|
||||
.schema(&reader)
|
||||
.map_err(ResponseError::open_index)?;
|
||||
|
||||
match schema {
|
||||
Some(schema) => {
|
||||
if params.raw {
|
||||
Ok(tide::response::json(schema))
|
||||
} else {
|
||||
Ok(tide::response::json(SchemaBody::from(schema)))
|
||||
}
|
||||
}
|
||||
None => Err(ResponseError::not_found("missing index schema")),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn update_schema(mut ctx: Context<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(IndexesWrite)?;
|
||||
|
||||
let index_uid = ctx.url_param("index")?;
|
||||
|
||||
let params: SchemaParams = ctx.url_query().unwrap_or_default();
|
||||
|
||||
let schema = if params.raw {
|
||||
ctx.body_json::<SchemaBuilder>()
|
||||
.await
|
||||
.map_err(ResponseError::bad_request)?
|
||||
.build()
|
||||
} else {
|
||||
ctx.body_json::<SchemaBody>()
|
||||
.await
|
||||
.map_err(ResponseError::bad_request)?
|
||||
.into()
|
||||
};
|
||||
|
||||
let db = &ctx.state().db;
|
||||
let mut writer = db.update_write_txn().map_err(ResponseError::internal)?;
|
||||
|
||||
let index = db
|
||||
.open_index(&index_uid)
|
||||
.ok_or(ResponseError::index_not_found(index_uid))?;
|
||||
|
||||
let update_id = index
|
||||
.schema_update(&mut writer, schema.clone())
|
||||
.map_err(ResponseError::internal)?;
|
||||
|
||||
writer.commit().map_err(ResponseError::internal)?;
|
||||
|
||||
let response_body = IndexUpdateResponse { update_id };
|
||||
Ok(tide::response::json(response_body)
|
||||
.with_status(StatusCode::ACCEPTED)
|
||||
.into_response())
|
||||
}
|
||||
|
||||
pub async fn get_update_status(ctx: Context<Data>) -> SResult<Response> {
|
||||
ctx.is_allowed(IndexesRead)?;
|
||||
|
||||
|
|
|
@ -51,10 +51,10 @@ pub fn load_routes(app: &mut tide::App<Data>) {
|
|||
.put(index::update_index)
|
||||
.delete(index::delete_index);
|
||||
|
||||
router
|
||||
.at("/schema")
|
||||
.get(index::get_index_schema)
|
||||
.put(index::update_schema);
|
||||
// router
|
||||
// .at("/schema")
|
||||
// .get(index::get_index_schema)
|
||||
// .put(index::update_schema);
|
||||
|
||||
router.at("/documents").nest(|router| {
|
||||
router
|
||||
|
|
|
@ -64,8 +64,9 @@ pub async fn search_with_url_query(ctx: Context<Data>) -> SResult<Response> {
|
|||
let crop_length = query.crop_length.unwrap_or(200);
|
||||
if attributes_to_crop == "*" {
|
||||
let attributes_to_crop = schema
|
||||
.get_displayed_name()
|
||||
.iter()
|
||||
.map(|(attr, ..)| (attr.to_string(), crop_length))
|
||||
.map(|attr| (attr.to_string(), crop_length))
|
||||
.collect();
|
||||
search_builder.attributes_to_crop(attributes_to_crop);
|
||||
} else {
|
||||
|
@ -79,7 +80,7 @@ pub async fn search_with_url_query(ctx: Context<Data>) -> SResult<Response> {
|
|||
|
||||
if let Some(attributes_to_highlight) = query.attributes_to_highlight {
|
||||
let attributes_to_highlight = if attributes_to_highlight == "*" {
|
||||
schema.iter().map(|(attr, ..)| attr.to_string()).collect()
|
||||
schema.get_displayed_name()
|
||||
} else {
|
||||
attributes_to_highlight
|
||||
.split(',')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue