Browse Source

Develop

develop
Paul Masurel 8 years ago
parent
commit
05764fd3a9
2 changed files with 5 additions and 11 deletions
  1. +1
    -2
      src/commands/new.rs
  2. +4
    -9
      src/commands/serve.rs

+ 1
- 2
src/commands/new.rs View File

@@ -143,8 +143,7 @@ fn run_new(directory: PathBuf) -> tantivy::Result<()> {
let schema = schema_builder.build(); let schema = schema_builder.build();
let schema_json = format!("{}", json::as_pretty_json(&schema)); let schema_json = format!("{}", json::as_pretty_json(&schema));
println!("\n{}\n", Style::new().fg(Green).paint(schema_json)); println!("\n{}\n", Style::new().fg(Green).paint(schema_json));
let mut index = try!(Index::create(&directory, schema));
// index.save_metas()
Index::create(&directory, schema)?;
Ok(()) Ok(())
} }



+ 4
- 9
src/commands/serve.rs View File

@@ -4,14 +4,13 @@
/// and it takes the following query string argument /// and it takes the following query string argument
/// ///
/// - `q=` : your query /// - `q=` : your query
// - `nhits`: the number of hits that should be returned. (default to 10)
/// - `explain=` : if true returns some information about the score.
/// - `nhits`: the number of hits that should be returned. (default to 10)
/// ///
/// ///
/// For instance, the following call should return the 20 most relevant /// For instance, the following call should return the 20 most relevant
/// hits for fulmicoton. /// hits for fulmicoton.
/// ///
/// http://localhost:3000/api/?q=fulmicoton&explain=false&nhits=20
/// http://localhost:3000/api/?q=fulmicoton&&nhits=20
/// ///




@@ -106,7 +105,7 @@ impl IndexServer {
} }
} }
fn search(&self, q: String, num_hits: usize, explain: bool) -> tantivy::Result<Serp> {
fn search(&self, q: String, num_hits: usize) -> tantivy::Result<Serp> {
let query = self.query_parser.parse_query(&q).expect("Parsing the query failed"); let query = self.query_parser.parse_query(&q).expect("Parsing the query failed");
let searcher = self.index.searcher(); let searcher = self.index.searcher();
let mut count_collector = CountCollector::default(); let mut count_collector = CountCollector::default();
@@ -164,14 +163,10 @@ fn search(req: &mut Request) -> IronResult<Response> {
.get("nhits") .get("nhits")
.and_then(|nhits_str| usize::from_str(&nhits_str[0]).ok()) .and_then(|nhits_str| usize::from_str(&nhits_str[0]).ok())
.unwrap_or(10); .unwrap_or(10);
let explain: bool = qs_map
.get("explain")
.map(|s| &s[0] == &"true")
.unwrap_or(false);
let query = try!(qs_map let query = try!(qs_map
.get("q") .get("q")
.ok_or_else(|| IronError::new(StringError(String::from("Parameter q is missing from the query")), status::BadRequest)))[0].clone(); .ok_or_else(|| IronError::new(StringError(String::from("Parameter q is missing from the query")), status::BadRequest)))[0].clone();
let serp = index_server.search(query, num_hits, explain).unwrap();
let serp = index_server.search(query, num_hits).unwrap();
let resp_json = as_pretty_json(&serp).indent(4); let resp_json = as_pretty_json(&serp).indent(4);
let content_type = "application/json".parse::<Mime>().unwrap(); let content_type = "application/json".parse::<Mime>().unwrap();
Ok(Response::with((content_type, status::Ok, format!("{}", resp_json)))) Ok(Response::with((content_type, status::Ok, format!("{}", resp_json))))


Loading…
Cancel
Save