## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(collapse = TRUE, comment = "#>")

## ----setup--------------------------------------------------------------------
library(tantivyr)

## -----------------------------------------------------------------------------
pt_news

## -----------------------------------------------------------------------------
plain <- tnt_index_df(
  pt_news,
  text    = c(title, body),
  filters = c(section, date)
)

idx <- tnt_index_df(
  pt_news,
  text      = c(title, body),
  filters   = c(section, date),
  stemmer   = "portuguese",
  stopwords = TRUE
)

## -----------------------------------------------------------------------------
queries <- c("vacinas", "queimada", "pesquisa", "pesquisar")

data.frame(
  query   = queries,
  plain   = vapply(queries, \(q) tnt_count(plain, q), numeric(1)),
  stemmed = vapply(queries, \(q) tnt_count(idx, q), numeric(1)),
  row.names = NULL
)

## -----------------------------------------------------------------------------
tnt_search(idx, "pesquisar")[, c("score", "section", "title")]

## -----------------------------------------------------------------------------
tnt_count(plain, "de")
tnt_count(idx, "de")

## -----------------------------------------------------------------------------
tnt_search(idx, "a redução dos juros", limit = 3)[, c("score", "title")]

## -----------------------------------------------------------------------------
tnt_count(idx, "orçamento")
tnt_count(idx, "orcamento")

## -----------------------------------------------------------------------------
folded <- tnt_index_df(
  pt_news,
  text         = c(title, body),
  filters      = c(section, date),
  stemmer      = "portuguese",
  stopwords    = TRUE,
  fold_accents = TRUE
)

tnt_count(folded, "orcamento")
tnt_search(folded, "saude agua", limit = 3)[, c("score", "title")]
tnt_search(folded, "orcamento", highlight = title)$title_snippet

## -----------------------------------------------------------------------------
tnt_count(folded, "vacinação")
tnt_count(folded, "vacinacao")

## -----------------------------------------------------------------------------
# exact phrase
tnt_search(idx, '"banco central"')[, c("score", "title")]

# either word
tnt_search(idx, "enchentes OR queimadas")[, c("section", "title")]

# required and excluded words
tnt_search(idx, "+juros -inflação")[, "title"]

# restrict a word to one field
tnt_search(idx, "title:vacina")[, "title"]

## -----------------------------------------------------------------------------
tnt_search(idx, "água", filter = date >= as.Date("2025-01-01"))[, c("date", "title")]

tnt_search(idx, "", filter = section == "esporte", order_by = date, limit = 3)[
  , c("date", "title")
]

## -----------------------------------------------------------------------------
hits <- tnt_search(idx, "vacinas", highlight = c(title, body))
hits$title_snippet
hits$body_snippet

## -----------------------------------------------------------------------------
tnt_stemmers()

