Is it possible to change the analyzer specified in

2019-01-24 16:01发布

In Solr, if we have a field in the schema with stored="true", and we change the analyzer associated with that field, is it possible to update just this field without reindexing all the documents? Could this be done using the "stored" values of the field with the new analyzer without going back to the original data source?

3条回答
混吃等死
2楼-- · 2019-01-24 16:46

I found a way using SolrJ.

        SolrQuery query = new SolrQuery();

        query.setQuery( "whatever_by_id" );

        QueryResponse rsp;

        rsp = server.query(query);

        Iterator<SolrDocument> iter = rsp.getResults().iterator();

        while (iter.hasNext()) {
            SolrDocument resultDoc = iter.next();
            String id = (String) resultDoc.getFieldValue("oid"); //id is the uniqueKey field

            SolrInputDocument inputdoc = new SolrInputDocument() ;
            for( Map.Entry<String, Object> f : resultDoc.entrySet()) {
                inputdoc.setField(f.getKey(), f.getValue()) ;
            }

            server.deleteById(id) ;
            server.commit() ;

            Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
            docs.add(inputdoc) ;
            server.add(docs) ;

            server.commit() ;
        }

When we add the "new" inputdoc (a copy of the old resultDoc), it uses the new analyzer we have changed in the schema to index. It´s not very elegant, but it works.

查看更多
姐就是有狂的资本
3楼-- · 2019-01-24 16:50
Explosion°爆炸
4楼-- · 2019-01-24 16:53

Guy, I optimized your code.

    ...
    while (iter.hasNext()) {
        ...
        //server.deleteById(id) ;
        //server.commit() ;

        Collection<SolrInputDocument> docs = new ArrayList<SolrInputDocument>();
        docs.add(inputdoc) ;
        server.add(docs) ;
        // server.commit() ;
    }
    server.commit() ;
查看更多
登录 后发表回答