mirror of
https://github.com/meilisearch/MeiliSearch
synced 2025-07-03 03:47:02 +02:00
use distinct on search
This commit is contained in:
parent
72450c765d
commit
a2f0f95337
6 changed files with 44 additions and 9 deletions
|
@ -3,6 +3,7 @@ use std::collections::{HashMap, HashSet};
|
|||
use std::convert::From;
|
||||
use std::error;
|
||||
use std::fmt;
|
||||
use std::hash::{Hash, Hasher};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use indexmap::IndexMap;
|
||||
|
@ -13,6 +14,7 @@ use meilisearch_core::{Highlight, Index, MainT, RankedMap};
|
|||
use meilisearch_schema::{FieldId, Schema};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
use siphasher::sip::SipHasher;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
|
@ -205,6 +207,21 @@ impl<'a> SearchBuilder<'a> {
|
|||
|
||||
query_builder.with_fetch_timeout(self.timeout);
|
||||
|
||||
if let Some(field) = self.index.main.distinct_attribute(reader)? {
|
||||
if let Some(field_id) = schema.id(&field) {
|
||||
query_builder.with_distinct(1, move |id| {
|
||||
match self.index.document_attribute_bytes(reader, id, field_id) {
|
||||
Ok(Some(bytes)) => {
|
||||
let mut s = SipHasher::new();
|
||||
bytes.hash(&mut s);
|
||||
Some(s.finish())
|
||||
}
|
||||
_ => None,
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
let start = Instant::now();
|
||||
let docs =
|
||||
query_builder.query(reader, &self.query, self.offset..(self.offset + self.limit));
|
||||
|
|
|
@ -96,12 +96,12 @@ fn write_all_and_delete() {
|
|||
let res_value: Value = serde_json::from_slice(&buf).unwrap();
|
||||
|
||||
let json = json!([
|
||||
"typo",
|
||||
"words",
|
||||
"proximity",
|
||||
"attribute",
|
||||
"wordsPosition",
|
||||
"exactness"
|
||||
"typo",
|
||||
"words",
|
||||
"proximity",
|
||||
"attribute",
|
||||
"wordsPosition",
|
||||
"exactness"
|
||||
]);
|
||||
|
||||
assert_json_eq!(json, res_value, ordered: false);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue