Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Facet returns wrong root #2494

Open
PSeitz opened this issue Sep 11, 2024 · 0 comments
Open

Facet returns wrong root #2494

PSeitz opened this issue Sep 11, 2024 · 0 comments
Labels

Comments

@PSeitz
Copy link
Contributor

PSeitz commented Sep 11, 2024

As reported in discord:

use tantivy::collector::FacetCollector;
use tantivy::query::{BooleanQuery, Occur, Query, TermQuery};
use tantivy::schema::*;
use tantivy::{doc, Index, IndexWriter};


fn main() -> tantivy::Result<()> {
    let mut schema_builder = Schema::builder();

    let id = schema_builder.add_text_field("id", TEXT | STORED);
    let title = schema_builder.add_text_field("title", TEXT | STORED);
    let category = schema_builder.add_facet_field("category", FacetOptions::default());

    let schema = schema_builder.build();
    let index = Index::create_in_ram(schema);

    let mut index_writer: IndexWriter = index.writer(30_000_000)?;

    index_writer.add_document(doc!(
        id => "1",
        title => "The Quantum Paradox",
        category => Facet::from("/science-fiction/space")
    ))?;
    index_writer.add_document(doc!(
        id => "2",
        title => "The Alien Invasion",
        category => Facet::from("/science-fiction/alien-invasion")
    ))?;
    index_writer.add_document(doc!(
        id => "3",
        title => "Terraforming Mars",
        category => Facet::from("/science-fiction/space-colonization")
    ))?;
    index_writer.add_document(doc!(
        id => "4",
        title => "The Dragon's Oath",
        category => Facet::from("/fantasy/epic-fantasy")
    ))?;
    index_writer.add_document(doc!(
        id => "5",
        title => "The Forgotten Kingdom",
        category => Facet::from("/fantasy/epic-fantasy/martin")
    ))?;
    index_writer.add_document(doc!(
        id => "6",
        title => "The Last Elf",
        category => Facet::from("/fantasy/epic-fantasy/tolkien")
    ))?;
    index_writer.add_document(doc!(
        id => "7",
        title => "Cybernetic Rebellion",
        category => Facet::from("/science-fiction/cyberpunk")
    ))?;
    index_writer.commit()?;

    let reader = index.reader()?;
    let searcher = reader.searcher();
    let mut facet_collector = FacetCollector::for_field("category");
    facet_collector.add_facet("/fantasy/epic-fantasy");

    let facet = Facet::from("/fantasy/epic-fantasy");
    let facet_term = Term::from_facet(category, &facet);

    let occur = Occur::Must;
    let query: Box<dyn Query> = Box::new(TermQuery::new(facet_term, IndexRecordOption::Basic));
    let mut sub_queries: Vec<(Occur, Box<dyn Query>)> = vec![];
    sub_queries.push((occur, query));
    let facet_term_query = BooleanQuery::new(sub_queries);
    let facet_counts = searcher.search(&facet_term_query, &facet_collector)?;

    let counts: Vec<(&Facet, u64)> = facet_counts.get("/").collect();
    println!("{:?}", counts);

    Ok(())
}

if you run main you should see the same weird behavior that I am seeing, [(Facet(/fantasy/epic-fantasy/martin), 1), (Facet(/fantasy/epic-fantasy/tolkien), 1), (Facet(/science-fiction), 1)]

@PSeitz PSeitz added the bug label Oct 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant