Elasticsearch

Batch process all your records using unstructured-ingest to store structured outputs locally on your filesystem and upload those local files to an Elasticsearch index.

First you’ll need to install Elasticsearch dependencies as shown here.

pip install "unstructured[elasticsearch]"

Run Locally

The upstream connector can be any of the ones supported, but for convenience here, showing a sample command using the upstream local connector.

#!/usr/bin/env bash

EMBEDDING_PROVIDER=${EMBEDDING_PROVIDER:-"langchain-huggingface"}

unstructured-ingest \
  local \
  --input-path example-docs/book-war-and-peace-1225p.txt \
  --output-dir local-output-to-elasticsearch \
  --strategy fast \
  --chunk-elements \
  --embedding-provider "$EMBEDDING_PROVIDER" \
  --num-processes 4 \
  --verbose \
  elasticsearch \
  --hosts "$ELASTICSEARCH_HOSTS" \
  --username "$ELASTICSEARCH_USERNAME" \
  --password "$ELASTICSEARCH_PASSWORD" \
  --index-name "$ELASTICSEARCH_INDEX_NAME" \
  --num-processes 2

For a full list of the options the CLI accepts check unstructured-ingest <upstream connector> elasticsearch --help.

NOTE: Keep in mind that you will need to have all the appropriate extras and dependencies for the file types of the documents contained in your data storage platform if you’re running this locally. You can find more information about this in the installation guide.

Vector Search Sample Mapping

To make sure the schema of the index matches the data being written to it, a sample mapping json can be used.

Object description
  1{
  2   "properties": {
  3      "element_id": {
  4         "type": "keyword"
  5      },
  6      "text": {
  7         "type": "text",
  8         "analyzer": "english"
  9      },
 10      "type": {
 11         "type": "text"
 12      },
 13      "embeddings": {
 14         "type": "dense_vector",
 15         "dims": 384
 16      },
 17      "metadata": {
 18         "type": "object",
 19         "properties": {
 20            "category_depth": {
 21               "type": "integer"
 22            },
 23            "parent_id": {
 24               "type": "keyword"
 25            },
 26            "attached_to_filename": {
 27               "type": "keyword"
 28            },
 29            "filetype": {
 30               "type": "keyword"
 31            },
 32            "last_modified": {
 33               "type": "date"
 34            },
 35            "file_directory": {
 36               "type": "keyword"
 37            },
 38            "filename": {
 39               "type": "keyword"
 40            },
 41            "data_source": {
 42               "type": "object",
 43               "properties": {
 44                  "url": {
 45                     "type": "text",
 46                     "analyzer": "standard"
 47                  },
 48                  "version": {
 49                     "type": "keyword"
 50                  },
 51                  "date_created": {
 52                     "type": "date"
 53                  },
 54                  "date_modified": {
 55                     "type": "date"
 56                  },
 57                  "date_processed": {
 58                     "type": "date"
 59                  },
 60                  "record_locator": {
 61                     "type": "keyword"
 62                  },
 63                  "permissions_data": {
 64                     "type": "object"
 65                  }
 66               }
 67            },
 68            "coordinates": {
 69               "type": "object",
 70               "properties": {
 71                  "system": {
 72                     "type": "keyword"
 73                  },
 74                  "layout_width": {
 75                     "type": "float"
 76                  },
 77                  "layout_height": {
 78                     "type": "float"
 79                  },
 80                  "points": {
 81                     "type": "float"
 82                  }
 83               }
 84            },
 85            "languages": {
 86               "type": "keyword"
 87            },
 88            "page_number": {
 89               "type": "integer"
 90            },
 91            "page_name": {
 92               "type": "keyword"
 93            },
 94            "url": {
 95               "type": "text",
 96               "analyzer": "standard"
 97            },
 98            "links": {
 99               "type": "object"
100            },
101            "link_urls": {
102               "type": "text"
103            },
104            "link_texts": {
105               "type": "text"
106            },
107            "sent_from": {
108               "type": "text",
109               "analyzer": "standard"
110            },
111            "sent_to": {
112               "type": "text",
113               "analyzer": "standard"
114            },
115            "subject": {
116               "type": "text",
117               "analyzer": "standard"
118            },
119            "section": {
120               "type": "text",
121               "analyzer": "standard"
122            },
123            "header_footer_type": {
124               "type": "keyword"
125            },
126            "emphasized_text_contents": {
127               "type": "text"
128            },
129            "emphasized_text_tags": {
130               "type": "keyword"
131            },
132            "text_as_html": {
133               "type": "text",
134               "analyzer": "standard"
135            },
136            "regex_metadata": {
137               "type": "object"
138            },
139            "detection_class_prob": {
140               "type": "float"
141            }
142         }
143      }
144   }
145}