Implementing mapper attachment in elasticsearch

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

Mapping attachement type
[code lang=”bash”]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" }
} } } } }'[/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($_)’`
json="{\"file\":\"${coded}\"}"
echo "$json" &gt; json.file
curl -X POST "192.168.0.37:9200/test/attachment/" -d @json.file
[/code]
Query  (Search esults will be highlighted)
[code lang=”bash”]curl "192.168.0.37:9200/_search?pretty=true" -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.