# Elastic with pfd, excel and word documents

**URL:** https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284
**Category:** Elastic Search
**Tags:** elastic-app-search
**Created:** [February 14, 2024, 1:38pm UTC](https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284 "2024-02-14T13:38:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![DAVID\_MARIN\_ALVAREZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_marin_alvarez/32/126492_2.png) [@DAVID\_MARIN\_ALVAREZ](https://discuss.elastic.co/u/DAVID_MARIN_ALVAREZ)
#### Post date: [February 14, 2024, 1:38pm UTC](https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284/1 "2024-02-14T13:38:38Z")

</div>

Hello, I need help, I am creating a project with which I need to index files of different types to perform searches on them using elastic, these files are extracted directly from an Oracle database in 12c, I would like their content to be indexed regardless of whether they are excel, word, pdf, etc. and that it is not necessary to encode them to upload them as plain text, is there a way to achieve this?

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [February 14, 2024, 2:09pm UTC](https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284/2 "2024-02-14T14:09:39Z")

</div>

If you have a directory containing the files You can use [FSCrawler](https://fscrawler.readthedocs.io). There's [a tutorial](https://fscrawler.readthedocs.io/en/latest/user/tutorial.html) to help you getting started.

You can use the [ingest attachment plugin](https://www.elastic.co/guide/en/elasticsearch/plugins/current/ingest-attachment.html).

There an example here: [https://www.elastic.co/guide/en/elasticsearch/plugins/current/using-ingest-attachment.html](https://www.elastic.co/guide/en/elasticsearch/plugins/current/using-ingest-attachment.html)

```auto
PUT _ingest/pipeline/attachment
{
  "description" : "Extract attachment information",
  "processors" : [
    {
      "attachment" : {
        "field" : "data"
      }
    }
  ]
}
PUT my_index/_doc/my_id?pipeline=attachment
{
  "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0="
}
GET my_index/_doc/my_id

```

The `data` field is basically the BASE64 representation of your binary file.

---

<div class="post-metadata">

### Author: ![DAVID\_MARIN\_ALVAREZ](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/david_marin_alvarez/32/126492_2.png) [@DAVID\_MARIN\_ALVAREZ](https://discuss.elastic.co/u/DAVID_MARIN_ALVAREZ)
#### Post date: [February 14, 2024, 2:17pm UTC](https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284/3 "2024-02-14T14:17:58Z")

</div>

The documents are not hosted in any directory, they are consulted through a model within the project and loaded through a function. At no time are they hosted locally because the amount of data is too high to be stored locally.

```auto
class AdmDocumentos(models.Model):
    id = models.BigIntegerField(primary_key=True, blank=True)
    nombre = models.CharField(max_length=100)
    archivo = models.TextField() # Cambiado a TextField para almacenar texto

    class Meta:
        managed = False
        db_table = '"' + DATABASE_SCHEMA + '"."adm_documentos"'
        db_table_comment = 'Documentos'
        verbose_name = 'Documento'
        verbose_name_plural = 'Documentos'
        ordering = ['nombre']
        
    @set_sql_for_field('id', 'SELECT ' + DATABASE_SCHEMA + '.id_seq.NEXTVAL FROM dual')
    def save(self, *args, **kwargs):
        super().save(*args, **kwargs)

    def __str__ (self):
        return self.nombre

# Conexión a Elasticsearch
connections.create_connection(hosts=['http://localhost:9200'], http_auth=(' *******', '**********'))

# Definición del índice en Elasticsearch
class DocumentoIndex(Document):
    nombre = Text()
    archivo = Binary() 

    class Index:
        name = 'documento_index'

        # Definición del mapeo
        mappings = {
            "properties": {
                "nombre": {
                    "type": "text"
                },
                "archivo": {
                    "type": "binary",
                }
            }
        }

# Función para indexar documentos
@receiver(post_save, sender=AdmDocumentos)
def indexar_documento(sender, instance, created, **kwargs):
    if created:
        # Si se crea un nuevo documento, lo indexamos en Elasticsearch
        documento_index = DocumentoIndex(
            meta={'id': instance.id},
            nombre=instance.nombre,
        )
        try:
            # Verificar si el archivo es un objeto de tipo bytes
            if isinstance(instance.archivo, bytes):
                # Convertir el archivo a una cadena de texto antes de indexarlo
                contenido_decodificado = instance.archivo.decode('utf-8')
                documento_index.archivo = contenido_decodificado
            else:
                # Si el archivo ya es una cadena de texto, lo asignamos directamente
                documento_index.archivo = instance.archivo

            # Guardar el documento indexado en Elasticsearch
            documento_index.save()
        except Exception as e:
            print(f"Error al indexar el documento: {e}")

```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [March 13, 2024, 2:18pm UTC](https://discuss.elastic.co/t/elastic-with-pfd-excel-and-word-documents/353284/4 "2024-03-13T14:18:16Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
