Indexing Attachment file to elastic search

后端 未结 4 781
暖寄归人
暖寄归人 2021-01-06 15:03

I have typed this command to index a document in Elasticsearch

create an index

curl -X PUT \"localhost:9200/test_idx_1x\"

4条回答
  •  难免孤独
    2021-01-06 15:13

    First, you don't specify whether you have the attachment plugin installed. If not, you can do so with:

    ./bin/plugin -install mapper-attachments
    

    You will need to restart ElasticSearch for it to load the plugin.

    Then, as you do above, you map a field to have type attachment:

    curl -XPUT 'http://127.0.0.1:9200/foo/?pretty=1'  -d '
    {
       "mappings" : {
          "doc" : {
             "properties" : {
                "file" : {
                   "type" : "attachment"
                }
             }
          }
       }
    }
    '
    

    When you try to index a document, you need to encode the contents of your file in Base64. You could do this on the command line using the base64 command line utility. However, to be legal JSON, you also need to encode new lines, which you can do by piping the output from base64 through Perl:

    curl -XPOST 'http://127.0.0.1:9200/foo/doc?pretty=1'  -d '
    {
       "file" : '`base64 /path/to/file | perl -pe 's/\n/\\n/g'`'
    }
    '
    

    Now you can search your file:

    curl -XGET 'http://127.0.0.1:9200/foo/doc/_search?pretty=1'  -d '
    {
       "query" : {
          "text" : {
             "file" : "text to look for"
          }
       }
    }
    '
    

    See ElasticSearch attachment type for more.

提交回复
热议问题