elasticsearch索引文档-attachment

对不同类型的文档,pdf xml等内容进行索引是一个常用的功能,es对此提供了支持。

1:attachment type

同其他的core type一样,attachment type用来支持对文档的索引(base64编码)。attachment type是用插件的方式提供的。

注意:目前attachment还在实验阶段。不过还是可用的。

attachment的使用是非常简单的,mapping中设置类型为attachment即可。

{

    "person" : {

                       "properties" : { "my_attachment" : { "type" : "attachment"} }

                    }

}

index过程: 

{ "my_attachment" : "......base64 encoded attachment......"}

也可以通过更加详尽的设置:

{

     "my_attachment" : {

                                       "_content_type" : "application/pdf",

                                       "_name" : "resource/name/of/my.pdf"

                                       "content" : "......base64 encoded attachment......"

                                   }

}

attachment type仅仅索引doc的内容,而且自动添加元数据信息(前提是可用)。元数据信息支持:date,title,author 和 keyword。对元数据信息也提供了查询,采用如下格式my_attachment.author。

无论是meta data还是 content都是简单个core type(string , date....)。所以,都可以在mapping中配置。比如

{

   "person" : {

                     "properties" : {

                           "file" : {

                                   "type" : "attachment",

                                   "fields" : {

                                         "file" : { "index" :  "no" },

                                         "date" : { "store" : true },

                                         "author" : { "analyzer" : "myAnalyzer"}

                                    }

                            }

                     }

    }

}

上边这个mapping,实际doc的内容索引到字段file中,但是我们并不存储它,因此只能通过_all访问。

2:attachment plugin

es对attachment的支持是通过elasticsearch-mapper-attachment来实现的,内部使用了apache tika。

具体介绍可以参考github上的链接:https://github.com/elasticsearch/elasticsearch-mapper-attachments

因此,如果需要设置attachment类型就需要安装plugin。网络上很多介绍安装的方式,基本都是采用

bin/plugin -install的方式安装。这也是es插件通用的安装方式,比如head bigdesk marvel等。但是elasticsearch-mapper-attachment的安装经常会报错,可能是dns的问题,也可能是git上是sourcecode,而不是现成的jar包。因此,如果命令不可用,可以直接将jar包和tika的jar包放到lib下,或者放到已经设置过的plugin目录下即可。注意版本问题。

参考链接:http://mvnrepository.com/artifact/org.elasticsearch/elasticsearch-mapper-attachments

http://archive.apache.org/dist/tika/

3:Attachment Type in Action

Mapping:

String idxName = "test";
String idxType = "attachment";
XContentBuilder map = jsonBuilder().startObject()
        .startObject(idxType)
          .startObject("properties")
            .startObject("file")
              .field("type", "attachment")
              .startObject("fields")
                .startObject("title")
                  .field("store", "yes")
                .endObject()
                .startObject("file")
                  .field("term_vector","with_positions_offsets")
                  .field("store","yes")
                .endObject()
              .endObject()
            .endObject()
          .endObject()
     .endObject();

Create index and put mapping:

CreateIndexResponse resp = client.admin().indices().prepareCreate(idxName).setSettings(
            ImmutableSettings.settingsBuilder()
            .put("number_of_shards", 1)
            .put("index.numberOfReplicas", 1))
            .addMapping("attachment", map).execute().actionGet();

assertThat(resp.acknowledged(), equalTo(true));

Index:

String path = "/opt/data/mydata.pdf"
String data64 = org.elasticsearch.common.Base64.encodeFromFile(path);
XContentBuilder source = jsonBuilder().startObject()
        .field("file", data64).endObject();

String id = "pdf001"
IndexResponse idxResp = client.prepareIndex().setIndex(idxName).setType(idxType).setId(id)
        .setSource(source).setRefresh(true).execute().actionGet();


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值