Repository: TabbyML/tabby
File: crates/tabby-common/src/index/code/mod.rs (Rust)

Code before:
        _ => language,
    };

    let mut term =
        Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_LANGUAGE, false);
    term.append_type_and_str(language);
    Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}

pub fn body_query(tokens: &[String]) -> Box<dyn Query> {
    let schema = IndexSchema::instance();
    let subqueries: Vec<Box<dyn Query>> = tokens
        .iter()
        .map(|text| {
            let term = Term::from_field_text(schema.field_chunk_tokens, text);
            let term_query: Box<dyn Query> =
                Box::new(TermQuery::new(term, IndexRecordOption::Basic));

            term_query
        })
        .collect();

    Box::new(BooleanQuery::union(subqueries))
}

fn filepath_query(filepath: &str) -> Box<TermQuery> {
    debug!("filepath_query - Input filepath: {}", filepath);
    let schema = IndexSchema::instance();
    let mut term =
        Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_FILEPATH, false);
    // normalize the path base on the platform.
    let filepath = normalize_path(Some(filepath.to_string()))
        .ok()
        .flatten()
        .unwrap_or_else(|| filepath.to_string());
    debug!("filepath_query - Normalized filepath: {}", filepath);

    term.append_type_and_str(&filepath);
    Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}

pub fn code_search_query(
    query: &CodeSearchQuery,
    chunk_tokens_query: Box<dyn Query>,
) -> BooleanQuery {
    let schema = IndexSchema::instance();

    // language / git_url / filepath field shouldn't contribute to the score, mark them to 0.0.
    let mut subqueries = vec![
        (Occur::Must, chunk_tokens_query),
        (Occur::Must, schema.corpus_query(corpus::CODE)),

Review comment (suggestion):
if you invoke normalize_path in this function, you don't have to modify `api/code.rs` nor `code_search_query` function.

Code after:
    };

    let mut term =
        Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_LANGUAGE, false);
    term.append_type_and_str(language);
    Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}

pub fn body_query(tokens: &[String]) -> Box<dyn Query> {
    let schema = IndexSchema::instance();
    let subqueries: Vec<Box<dyn Query>> = tokens
        .iter()
        .map(|text| {
            let term = Term::from_field_text(schema.field_chunk_tokens, text);
            let term_query: Box<dyn Query> =
                Box::new(TermQuery::new(term, IndexRecordOption::Basic));

            term_query
        })
        .collect();

    Box::new(BooleanQuery::union(subqueries))
}

fn filepath_query(filepath: &str) -> Box<TermQuery> {
    let schema = IndexSchema::instance();
    let mut term =
        Term::from_field_json_path(schema.field_chunk_attributes, fields::CHUNK_FILEPATH, false);
    term.append_type_and_str(filepath);
    Box::new(TermQuery::new(term, IndexRecordOption::Basic))
}

pub fn code_search_query(
    query: &CodeSearchQuery,
    chunk_tokens_query: Box<dyn Query>,
) -> BooleanQuery {
    let schema = IndexSchema::instance();

    // language / git_url / filepath field shouldn't contribute to the score, mark them to 0.0.
    let mut subqueries = vec![
        (Occur::Must, chunk_tokens_query),
        (Occur::Must, schema.corpus_query(corpus::CODE)),
        (
            Occur::Must,
            Box::new(ConstScoreQuery::new(
                Box::new(schema.source_id_query(&query.source_id)),
                0.0,
            )),
        ),
    ];