quickwit-oss / tantivy

Tantivy is a full-text search engine library inspired by Apache Lucene and written in Rust
MIT License
11.82k stars 657 forks source link

Highligh feature not work? #2385

Closed qq253498229 closed 4 months ago

qq253498229 commented 4 months ago

Describe the bug

I'm not sure where I went wrong, as I've been unable to successfully implement full-text search with highlighting.

Which version of tantivy are you using?

To Reproduce

#[test]
fn test3() -> anyhow::Result<()> {
    let index_path = TempDir::new()?;
    let mut schema_builder = Schema::builder();
    let title = schema_builder.add_text_field("title", TEXT | STORED);
    let body = schema_builder.add_text_field("body", TEXT);
    let schema = schema_builder.build();
    let index = Index::create_in_dir(&index_path, schema.clone())?;

    let mut index_writer = index.writer(100_000_000)?;
    index_writer.add_document(doc!(
        title => "The Old Man and the Sea",
        body => "He was an old man who fished alone in a skiff in \
                the Gulf Stream and he had gone eighty-four days \
                now without taking a fish."
    ))?;
    index_writer.commit()?;

    let reader = index.reader()?;

    let searcher = reader.searcher();

    let query_parser = QueryParser::for_index(&index, vec![title, body]);

    let query = query_parser.parse_query("Stream")?;

    let top_docs: Vec<(Score, DocAddress)> =
        searcher.search(&query, &TopDocs::with_limit(10))?;

    for (_score, doc_address) in top_docs {
        let retrieved_doc = searcher.doc::<TantivyDocument>(doc_address)?;
        dbg!(&retrieved_doc);

        let mut snippet_generator = SnippetGenerator::create(&searcher, &*query, body)?;
        snippet_generator.set_max_num_chars(100);
        let snippet = snippet_generator.snippet_from_doc(&retrieved_doc);
        dbg!(&snippet);
        let snippet_html: String = snippet.to_html();
        dbg!(&snippet_html);
    }

    Ok(())
}

The console output is as follows:

[tests\test_tanvity.rs:124:9] &retrieved_doc = TantivyDocument {
    field_values: [
        FieldValue {
            field: Field(
                0,
            ),
            value: Str(
                "The Old Man and the Sea",
            ),
        },
    ],
}
[tests\test_tanvity.rs:129:9] &snippet = Snippet {
    fragment: "",
    highlighted: [],
    snippet_prefix: "",
    snippet_postfix: "",
}
[tests\test_tanvity.rs:131:9] &snippet_html = ""
qq253498229 commented 4 months ago

The problem was caused by me.

I forgot to set the body to STORE.