Implementing mapper attachment in elasticsearch

Create a new index

curl -X PUT "192.168.0.37:9200/test" -d '{
"settings" : { "index" : { "number_of_shards" : 1, "number_of_replicas" : 0 }}
}'

Mapping attachement type

curl -X PUT "192.168.0.37:9200/test/attachment/_mapping" -d '{
"attachment" : {
"properties" : {
"file" : {
"type" : "attachment",
"fields" : {
"title" : { "store" : "yes" },
"file" : { "term_vector":"with_positions_offsets", "store":"yes" }
} } } } }'

Shell script to convert content to base64 encoding and index

#!/bin/sh</code>

coded=`cat TestPDF.pdf | perl -MMIME::Base64 -ne 'print encode_base64($_)'`
json="{\"file\":\"${coded}\"}"
echo "$json" &gt; json.file
curl -X POST "192.168.0.37:9200/test/attachment/" -d @json.file


Query  (Search esults will be highlighted)
curl "192.168.0.37:9200/_search?pretty=true" -d '{
"fields" : ["title"],
"query" : {
"query_string" : {
"query" : "Cycling tips"
}
},
"highlight" : {
"fields" : {
"file" : {}
} } }'

***********************************************************************

If you want to explore more check Full Code on GitHub

Author: Pradeep Pant

Software Engineer

2 thoughts on “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 *