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

Code before:
                Occur::Must,
                Box::new(ExistsQuery::new_exists_query(
                    FIELD_FAILED_CHUNKS_COUNT.into(),
                )),
            ),
            // Exclude chunk documents
            (
                Occur::MustNot,
                Box::new(ExistsQuery::new_exists_query(FIELD_CHUNK_ID.into())),
            ),
        ])
    }

    /// Build a query to check if the document has specific attribute field.
    pub fn doc_has_attribute_field(&self, corpus: &str, doc_id: &str, field: &str) -> impl Query {
        let doc_id_query = TermQuery::new(
            Term::from_field_text(self.field_id, doc_id),
            tantivy::schema::IndexRecordOption::Basic,
        );

        BooleanQuery::new(vec![
            // Must match the corpus
            (Occur::Must, self.corpus_query(corpus)),
            // Must match the doc id
            (Occur::Must, Box::new(doc_id_query)),
            // Must has the failed_chunks_count field
            (
                Occur::Must,
                Box::new(ExistsQuery::new_exists_query(format!(
                    "{}.{}",
                    FIELD_ATTRIBUTES, field
                ))),
            ),
            // Exclude chunk documents
            (
                Occur::MustNot,
                Box::new(ExistsQuery::new_exists_query(FIELD_CHUNK_ID.into())),
            ),
        ])
    }

    /// Build a query to find the document with the given `doc_id`, include chunks.
    pub fn doc_query_with_chunks(&self, corpus: &str, doc_id: &str) -> impl Query {
        let doc_id_query = TermQuery::new(
            Term::from_field_text(self.field_id, doc_id),
            tantivy::schema::IndexRecordOption::Basic,
        );

        BooleanQuery::new(vec![
            // Must match the corpus
            (Occur::Must, self.corpus_query(corpus)),

Review comment (suggestion):
```suggestion
            // Must has the field_attributes field.
```

Code after:
            (
                Occur::Must,
                Box::new(ExistsQuery::new_exists_query(
                    FIELD_FAILED_CHUNKS_COUNT.into(),
                )),
            ),
            // Exclude chunk documents
            (
                Occur::MustNot,
                Box::new(ExistsQuery::new_exists_query(FIELD_CHUNK_ID.into())),
            ),
        ])
    }

    /// Build a query to check if the document has specific attribute field.
    pub fn doc_has_attribute_field(&self, corpus: &str, doc_id: &str, field: &str) -> impl Query {
        let doc_id_query = TermQuery::new(
            Term::from_field_text(self.field_id, doc_id),
            tantivy::schema::IndexRecordOption::Basic,
        );

        BooleanQuery::new(vec![
            // Must match the corpus
            (Occur::Must, self.corpus_query(corpus)),
            // Must match the doc id
            (Occur::Must, Box::new(doc_id_query)),
            // Must has the attributes.field field
            (
                Occur::Must,
                Box::new(ExistsQuery::new_exists_query(format!(
                    "{}.{}",
                    FIELD_ATTRIBUTES, field
                ))),
            ),
        ])
    }

    /// Build a query to find the document with the given `doc_id`, include chunks.
    pub fn doc_query_with_chunks(&self, corpus: &str, doc_id: &str) -> impl Query {
        let doc_id_query = TermQuery::new(
            Term::from_field_text(self.field_id, doc_id),
            tantivy::schema::IndexRecordOption::Basic,
        );

        BooleanQuery::new(vec![
            // Must match the corpus
            (Occur::Must, self.corpus_query(corpus)),
            // Must match the doc id
            (Occur::Must, Box::new(doc_id_query)),
        ])
    }