Skip to content

Instantly share code, notes, and snippets.

@karmi
Last active March 25, 2017 09:57
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save karmi/5594127 to your computer and use it in GitHub Desktop.
Save karmi/5594127 to your computer and use it in GitHub Desktop.
Example of Elasticsearch's attachment handling
{\rtf1\ansi\ansicpg1250\cocoartf1038\cocoasubrtf350
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
{\info
{\title Test RTF document}
{\author John Smith}
{\*\company My Organization}}\paperw11900\paperh16840\margl1440\margr1440\vieww9000\viewh8400\viewkind0
\pard\tx566\tx1133\tx1700\tx2267\tx2834\tx3401\tx3968\tx4535\tx5102\tx5669\tx6236\tx6803\ql\qnatural\pardirnatural
\f0\fs24 \cf0 Test RTF document.\
\
Lorem ipsum dolor.}
# Example of handling attachments in Elasticsearch
# ================================================
#
# 1. Install the plugin: $ plugin -install elasticsearch/elasticsearch-mapper-attachments/1.7.0
# 2. Run the script: $ bash test_attachments.sh
#
# More info: <http://www.elasticsearch.org/guide/reference/mapping/attachment-type/>
curl -X DELETE http://localhost:9200/test_attachments
echo;
curl -X POST http://localhost:9200/test_attachments -d '{
"mappings" : {
"document" : {
"properties" : {
"content" : {
"type" : "attachment",
"fields" : {
"content" : { "store" : "yes" },
"author" : { "store" : "yes" },
"title" : { "store" : "yes", "analyzer" : "english"},
"date" : { "store" : "yes" },
"keywords" : { "store" : "yes", "analyzer" : "keyword" },
"_name" : { "store" : "yes" },
"_content_type" : { "store" : "yes" }
}
}
}
}
}
}'
echo;
echo '>>> Index the document'
curl -i -X PUT http://localhost:9200/test_attachments/document/1 -d "{
\"_name\" : \"test.doc\",
\"content\" : \"$(openssl base64 -in test.doc)\"
}"
echo;
curl -X POST http://localhost:9200/test_attachments/_refresh
echo; echo ">>> Search for author 'John'"
curl "http://localhost:9200/test_attachments/_search?pretty=true&q=content.author:john&fields=content.title,content.author"
@spacetrack
Copy link

... thanks for this example; some notes:

  1. _content_type should be content_type (line 25)

  2. it might make sense to add

    "content_length" : { "store" : "yes" },
    

    before line 25

  3. it might make sense to replace fields=content.title,content.author by fields=*, so you can see all the fields that have been filled by the attachment plugin

@Tamanna-Sharma
Copy link

Hi, I am new to ElasticSearch and mapper-attachment.
I am trying to index a pdf file into ES using your code but after deleting test_attachments index, its giving me following error:

{"error":{"root_cause":[{"type":"mapper_parsing_exception","reason":"No handler for type [attachment] declared on field [content]"}],"type":"mapper_parsing_exception","reason":"Failed to parse mapping [document]: No handler for type [attachment] declared on field [content]","caused_by":{"type":"mapper_parsing_exception","reason":"No handler for type [attachment] declared on field [content]"}},"status":400}

Please let me know what I have missed in order to get this code worked.

Thanks in advance..

@bytemybit
Copy link

Tamanna-Sharma, If you haven't already figured this out you need to restart elasticsearch for the plugin to be recognized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment