# How to use the elasticsearch Attachment Processor with node.js

**URL:** <https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609>\
**Category:** Elasticsearch\
**Created:** [March 23, 2019, 5:36pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609 "2019-03-23T17:36:28Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 23, 2019, 5:36pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/1 "2019-03-23T17:36:28Z")

</div>

I'm making an application to search for words or phrases in .PDF files and I made the following code (Found on the internet):

```
    const elasticsearch = require('elasticsearch');
const fse = require('fs-extra')

let client = new elasticsearch.Client({
    host: "localhost:9200",
    log: ["error", "warning"]
});

client.indices.create({index: 'files'})
.then(() => {
    // create a mapping for the attachment
    return client.indices.putMapping({
        index: 'files',
        type: 'document',
        body: {
            document: {
                properties: {
                    file: {
                        type: 'attachment',
                        fields: {
                            content: {
                                type: 'string',
                                term_vector: 'with_positions_offsets',
                                store: true
                            }
                        }
                    }
                }
            }
        }
    });
});

const fileContents = fse.readFileSync('C:\\Users\\JoaoDJunior\\Downloads\\João D. Junior - rc.pdf');
const fileBase64 = new Buffer(fileContents).toString('base64');
//console.log(fileBase64);
client.create({
    index: 'files',
    type: 'document',
    id: 'somefileid',
    body: {
        file_id: 'somefileid',
        file: {
            _content: fileBase64
        }
    }
})
.catch((err) => {
    console.error('Error while creating elasticsearch record', err);
});

client.search({
    q: 'java',
    index: 'files'
}, (error, result) => {
    if (error) return console.log(error);
    console.log(result.hits);
});

```

The problem is that I can not get the words inside the document. Is there any error in my code, could anyone help?

---

<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:** [March 23, 2019, 6:28pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/2 "2019-03-23T18:28:55Z")

</div>

You are using here the `mapper-attachments` plugin which has been removed in 5.x.

Read [https://www.elastic.co/guide/en/elasticsearch/plugins/6.6/ingest-attachment.html](https://www.elastic.co/guide/en/elasticsearch/plugins/6.6/ingest-attachment.html) about `ingest-attachment` feature and how to use it.

---

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 23, 2019, 6:41pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/3 "2019-03-23T18:41:09Z")

</div>

Through the command line, I get results, but I can not do with the nodejs application. Is it possible to use the library?

---

<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:** [March 23, 2019, 6:55pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/4 "2019-03-23T18:55:10Z")

</div>

What did you run on the command line? Vs what is your code then? Did you change it?

---

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 23, 2019, 7:13pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/5 "2019-03-23T19:13:57Z")

</div>

I run via the curl this code:  
`PUT _ingest / pipeline / attachment { "description": "Extract attachment information", "processors": [{ "attachment": { "field": "date" } }] } PUT my_index / _doc / my_id? Pipeline = attachment { "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0 =" } GET my_index / _doc / my_id`  
but I do not know how to send my base64 pdf file to the body.

---

<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:** [March 23, 2019, 9:24pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/6 "2019-03-23T21:24:02Z")

</div>

Is your question "how to generate a BASE64 from a binary file in python?"

---

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 25, 2019, 2:06pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/7 "2019-03-25T14:06:27Z")

</div>

with python I got it, however I have to mount with node.js, and use the elasticsearch client, and I'm not getting results. I saw that fsclawler might be a solution, however I wanted to make sure it could solve everything just with the elasticsearch

---

<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:** [March 25, 2019, 2:10pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/8 "2019-03-25T14:10:43Z")

</div>

Hmmm. Weird. Not sure what happened but I think this answer:

> [@dadoonet](#):
>
> Is your question "how to generate a BASE64 from a binary file in python?"

Was not related to your question but to another one... 🥴

Anyway I'm not sure I understand what your problem is. The code you shared initially is not about ingest-attachment but mapper-attachments.

You need to write using JS an equivalent code to:

> [@How to use the elasticsearch Attachment Processor with node.js](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/5):
>
> I run via the curl this code: PUT \_ingest / pipeline / attachment { "description": "Extract attachment information", "processors": [{ "attachment": { "field": "date" } }] } PUT my\_index / \_doc / my\_id? Pipeline = attachment { "data": "e1xydGYxXGFuc2kNCkxvcmVtIGlwc3VtIGRvbG9yIHNpdCBhbWV0DQpccGFyIH0 =" } GET my\_index / \_doc / my\_id but I do not know how to send my base64 pdf file to the body.

---

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 25, 2019, 2:25pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/9 "2019-03-25T14:25:53Z")

</div>

The code posted was the only one I found by example and apparently would do what I like: Read the contents of a pdf file and make it available for search.

But it does not fulfill what you would like, so I would like an example equivalent to the code made available in the documentation for the elasticsearch plugin.

---

<div class="post-metadata">

**Author:** ![Joao\_D\_Junior](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/joao_d_junior/32/42635_2.png) [@Joao\_D\_Junior](https://discuss.elastic.co/u/Joao_D_Junior)\
**Post date:** [March 25, 2019, 3:27pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/10 "2019-03-25T15:27:56Z")

</div>

I got the result with fscrawler.

---

<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:** [April 22, 2019, 3:28pm UTC](https://discuss.elastic.co/t/how-to-use-the-elasticsearch-attachment-processor-with-node-js/173609/11 "2019-04-22T15:28:07Z")

</div>

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