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

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

## -----------------------------------------------------------------------------
news <- data.frame(
  id     = 1:5,
  title  = c(
    "Orçamento público aprovado pelo congresso",
    "Reforma tributária avança no senado",
    "Nova lei de licitações entra em vigor",
    "Congresso debate orçamentos municipais",
    "Tribunal de contas analisa despesas"
  ),
  source = c("A", "B", "A", "C", "B"),
  year   = c(2022L, 2023L, 2024L, 2024L, 2023L)
)

idx <- tnt_index_df(
  news,
  text      = title,         # full-text column(s)
  filters   = c(source, year), # columns to filter / order on
  stemmer   = "portuguese",
  stopwords = TRUE
)
idx

## -----------------------------------------------------------------------------
tnt_search(idx, "orçamento")

## -----------------------------------------------------------------------------
tnt_search(idx, "", filter = year >= 2024)

tnt_search(idx, "congresso", filter = source == "A")

tnt_search(idx, "", filter = year %in% c(2022, 2024), limit = 10)

## -----------------------------------------------------------------------------
tnt_search(idx, "", filter = "year:[2023 TO *] AND source:B")

## -----------------------------------------------------------------------------
tnt_search(idx, "congresso", highlight = title)$title_snippet

tnt_search(idx, "", order_by = year, desc = TRUE)[, c("title", "year")]

## -----------------------------------------------------------------------------
tnt_count(idx, "congresso")
tnt_count(idx, "", filter = year == 2024)

## -----------------------------------------------------------------------------
sch <- tnt_schema(
  id    = tnt_i64(),
  slug  = tnt_text(stemmer = "raw"),                 # exact key for updates
  title = tnt_text(stemmer = "portuguese", stored = TRUE),
  body  = tnt_text(stemmer = "portuguese"),
  date  = tnt_date(fast = TRUE)
)

path <- tempfile()
idx <- tnt_index(path, schema = sch)

## -----------------------------------------------------------------------------
docs <- data.frame(
  id    = 1:2,
  slug  = c("edital-001", "edital-002"),
  title = c("Edital de licitação 001", "Edital de licitação 002"),
  body  = c("Aquisição de equipamentos de informática.",
            "Contratação de serviços de limpeza."),
  date  = as.Date(c("2024-02-01", "2024-03-15"))
)

idx |> tnt_add(docs) |> tnt_commit()
tnt_num_docs(idx)

## -----------------------------------------------------------------------------
idx |>
  tnt_update(
    data.frame(id = 1L, slug = "edital-001",
               title = "Edital de licitação 001 (retificado)",
               body = "Aquisição de notebooks.",
               date = as.Date("2024-02-10")),
    by = slug
  ) |>
  tnt_commit()

tnt_search(idx, "notebooks")[, c("id", "title")]

idx |> tnt_delete(slug == "edital-002") |> tnt_commit()
tnt_num_docs(idx)

## -----------------------------------------------------------------------------
reopened <- tnt_index(path)
tnt_num_docs(reopened)

## ----include = FALSE----------------------------------------------------------
unlink(path, recursive = TRUE)

