如何在node.js中使用Elasticsearch附件处理器

时间:2019-03-23 18:33:38

标签: node.js pdf elasticsearch

我正在制作一个应用程序来搜索.PDF文件中的单词或短语,并编写了以下代码(可在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);
});

问题是我无法在文档中找到单词。我的代码中有任何错误,有人可以帮忙吗?

0 个答案:

没有答案