通过elasticsearch-mapper attachment插件实现文件建立索引

作者: paopao   发布时间:2016-01-06
1.安装elasticsearch-mapper attachment

bin/plugin install elasticsearch/elasticsearch-mapper-attachments/3.1.1
 2.按照插件官方文档来测试
3.插件需要手动把文档内容转化为base64编码然后建立索引,代码如下
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import org.apache.tika.Tika;
import org.apache.tika.config.TikaConfig;
import org.apache.tika.metadata.Metadata;
import org.apache.tika.parser.AutoDetectParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.pdf.PDFParser;
import org.apache.tika.sax.BodyContentHandler;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.Client;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.ImmutableSettings;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.xml.sax.ContentHandler;

import com.spatial4j.core.io.ParseUtils;

import static org.elasticsearch.common.xcontent.XContentFactory.*;
public class sysfiles {
    public static void main(String[] args) throws Exception{
        sys();
}

    private static void sys() throws IOException {
        // TODO Auto-generated method stub
        String idxName = "test";
        String idxType = "attachments";
        Settings settings =ImmutableSettings.settingsBuilder().put("cluster.name","az_bsms_elasticsearch").build();
        Client client=new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("127.0.0.1", 9300));
       String data64=org.elasticsearch.common.Base64.encodeFromFile(filepath);
        XContentBuilder source = jsonBuilder().startObject()
            .field("file", data64)
                .field("text", data64)
                .endObject();

        String id = "file"+11;
        IndexResponse idxResp = client.prepareIndex().setIndex(idxName).setType(idxType).setId(id)
                .setSource(source).setRefresh(true).execute().actionGet();
        System.out.println(idxResp);
        client.close();
    }
4.按官方文档正常的搜索就可以了

5 个评论

请问如何在windows系统下安装插件?
您好,请问您用的是什么版本的jar包?我的没有这个方法encodeFromFile(filepath)
我也没有这个方法. 请问你解决了吗.
你好,请问你这个怎么解决的。谢谢
请给个DEMO参考一下,十分感谢

要回复文章请先登录注册