Highlights result with longest match

This commit is contained in:
lironhl 2020-04-24 14:21:22 +03:00
parent 1b7a6687c8
commit 16e075983d
3 changed files with 9 additions and 1 deletions

1
Cargo.lock generated
View File

@ -1141,6 +1141,7 @@ dependencies = [
"serde_qs", "serde_qs",
"sha2", "sha2",
"siphasher", "siphasher",
"slice-group-by",
"structopt", "structopt",
"sysinfo", "sysinfo",
"tempdir", "tempdir",

View File

@ -42,6 +42,7 @@ tide = "0.6.0"
ureq = { version = "0.12.0", features = ["tls"], default-features = false } ureq = { version = "0.12.0", features = ["tls"], default-features = false }
walkdir = "2.3.1" walkdir = "2.3.1"
whoami = "0.8.1" whoami = "0.8.1"
slice-group-by = "0.2.6"
[dev-dependencies] [dev-dependencies]
http-service = "0.4.0" http-service = "0.4.0"

View File

@ -17,6 +17,7 @@ use meilisearch_tokenizer::is_cjk;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::Value; use serde_json::Value;
use siphasher::sip::SipHasher; use siphasher::sip::SipHasher;
use slice_group_by::GroupBy;
#[derive(Debug)] #[derive(Debug)]
pub enum Error { pub enum Error {
@ -526,7 +527,12 @@ fn calculate_highlights(
let value: Vec<_> = value.chars().collect(); let value: Vec<_> = value.chars().collect();
let mut highlighted_value = String::new(); let mut highlighted_value = String::new();
let mut index = 0; let mut index = 0;
for m in matches {
let longest_matches = matches
.linear_group_by_key(|m| m.start)
.map(|group| group.last().unwrap());
for m in longest_matches {
if m.start >= index { if m.start >= index {
let before = value.get(index..m.start); let before = value.get(index..m.start);
let highlighted = value.get(m.start..(m.start + m.length)); let highlighted = value.get(m.start..(m.start + m.length));