Implementing mapper attachment in elasticsearch

Create a new index
[code lang=”bash”]curl -X PUT "" -d ‘{
"settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 0 }}

Mapping attachement type
[code lang=”bash”]curl -X PUT "" -d ‘{
"attachment" : {
"properties" : {
"file" : {
"type" : "attachment",
"fields" : {
"title" : { "store" : "yes" },
"file" : { "term_vector":"with_positions_offsets", "store":"yes" }
} } } } }'[/code]

Shell script to convert content to base64 encoding and index
[code lang=”bash”]#!/bin/sh</code>

coded=`cat TestPDF.pdf | perl -MMIME::Base64 -ne ‘print encode_base64($_)’`
echo "$json" &gt; json.file
curl -X POST "" -d @json.file
Query  (Search esults will be highlighted)
[code lang=”bash”]curl "" -d ‘{
"fields" : ["title"],
"query" : {
"query_string" : {
"query" : "Cycling tips"
"highlight" : {
"fields" : {
"file" : {}
} } }'[/code]


If you want to explore more check Full Code on GitHub

2 Replies to “Implementing mapper attachment in elasticsearch”

  1. Firstly great post. I worked like a charm. I have been looking around the internet for quite some time before stumble into this page.

    By any chance, do you know how to do the same with Java API. I managed to index the file(I hope I did, atleast indexed document appear to be similar to this method) but the search query are not returning anything from that index.

    Any help would be helpful.

  2. Hi Pradeep.. thanks for the post.
    By any chance, do you know how to do the same with NEST API. Lately I have been searching a lot on Google for a good example using NEST. It would be a great help if you have one like that.

    TIA 🙂

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.