Metadata-Version: 2.3
Name: procrustus-indexer
Version: 2.1.0
Summary: Python library for indexing HuC datasets.
Author: Rob Zeeman
Author-email: rob.zeeman@di.huc.knaw.nl
Requires-Python: >=3.13
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.13
Requires-Dist: elasticsearch (>=8.17.2,<9.0.0)
Requires-Dist: jmespath (>=1.0.1,<2.0.0)
Requires-Dist: jsonpath-ng (>=1.7.0,<2.0.0)
Requires-Dist: pylint (>=3.3.5,<4.0.0)
Requires-Dist: pytest (>=8.3.5,<9.0.0)
Requires-Dist: rdflib (>=7.1.4)
Requires-Dist: saxonche (>=12.7.0)
Description-Content-Type: text/markdown

# Procrustus Indexer
Python library for indexing HuC datasets.

This is a first experimental version, for indexing files based on a TOML configuration.
For now, input files need to be json.

Modules will be added for:
* XML
* Relational databases (MySQL, Postgres)
* Linked data (SPARQL, RDF)
* Tabular data (CSV, MS-Excel)

## Installation

In order to install this library, you can use pip:

```
pip install procrustus-indexer
```

## Usage
In order to use the indexer you need to configure it first. Use the `build_indexer`
function to create an indexer. It requires the configuration TOML file, the name of
the index to use and an Elasticsearch client.

For this example, assume we have the following file structure:

```
json-files/
    a.json
    b.json
config.toml
```

First we create the indexer:

```python
from elasticsearch import Elasticsearch
from procrustus_indexer import build_indexer

indexer = build_indexer('config.toml', 'index-name', Elasticsearch())
```

### Create index and mapping:
We can use the indexer to generate a mapping for the Elasticsearch index.

```python
indexer.create_mapping(overwrite=True)
```
The `overwrite` parameter can be used to re-create the index if it already exists.

### Index json files
Now we can import our json files in Elasticsearch.

```python
indexer.import_folder("json-files")
```

or

```python
indexer.import_files(["json-files/a.json", "json-files/b.json"])
```


## TOML Configuration
In order to use this indexer, you need to have a TOML file describing the
required ES index fields, and where in the source files to find these.
Here we will see how to configure this TOML file, and what options there are.

Let's first consider this example configueration:

```toml
[index]
full_text="yes"
record="yes"

[index.input]
format="json"

[index.id]
path="jmes:id"
type="keyword"

[index.facet.title]
path="jmes:title"
type="text"

[index.facet.tag]
path="jmes:tag"
type="text"

[index.facet.the_answer]
path="jmes:value"
type="number"

[index.facet.nested_value]
path="jmes:nested.object.value"
type="number"
```

This will turn the following input file:

```json
{
  "id": 0,
  "title": "This is a test",
  "tag": "label",
  "value": 42,
  "nested": {
    "object": {
      "value": 7
    }
  }
}
```

Into this output file (for inserting in ES):
```json
{
  "id": 0,
  "title": "This is a test",
  "tag": "label",
  "the_answer": 42,
  "nested_value": 7
}
```

We will now go over each field and see what it means.

#### `[index]`
This part is not currently processed yet. It contains information about
full-text search processing.

#### `[index.input]`
Here, thie input format is set (along with format-specific configuration in the future). For now,
only `json` is supported.

#### `[index.id]`
Here, the ID used for documents in the Elasticsearch index is specified. The `path` determines
where in the input files to look for the ID. Paths follow the following format: `{type}:{path}`.
Available types depend on the input format. For now, the only input format is JSON and the only
supported path type is `jmes`, which will use [JMESPath](https://jmespath.org).

#### `[index.facet.{name}]`
Additional fields, other than the ID, can be specified in here. You can add any number you like.
These consist of three components: the `{name}`, which specifies the name of the property in the
ES index, the `path`, which works like ID and determines where in the input file to find the value,
and the `type`. This determines how the property will be indexed in ES. Supported types are `text`,
`number` and `keyword`. Text is indexed as a full-text field, keyword as a string used for `terms`
queries, e.g.

