Project

General

Profile

« Previous | Next » 

Revision 7604

Added by Jing Tao over 11 years ago

Add code to get all doc ids in the solr server.

View differences:

metacat-index/src/test/java/edu/ucsb/nceas/metacat/index/SolrIndexIT.java
66 66
       InputStream emlInputStream = new FileInputStream(new File(EMLFILEPATH));    
67 67
       solrIndex.insert(id, systemMetadata, emlInputStream);
68 68
       String result = doQuery(solrIndex.getSolrServer());
69
       List<String> ids = solrIndex.getSolrIds();
70
       assertTrue(ids.size() == 1);
71
       for(String identifiers :ids) {
72
           assertTrue(id.equals(identifiers));
73
       }
69 74
       assertTrue(result.contains("version1"));
70 75
    	
71 76
    }
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SolrIndex.java
47 47
import org.apache.commons.io.output.ByteArrayOutputStream;
48 48
import org.apache.commons.logging.Log;
49 49
import org.apache.commons.logging.LogFactory;
50
import org.apache.solr.client.solrj.SolrQuery;
50 51
import org.apache.solr.client.solrj.SolrServer;
51 52
import org.apache.solr.client.solrj.SolrServerException;
53
import org.apache.solr.client.solrj.response.QueryResponse;
52 54
import org.apache.solr.client.solrj.response.UpdateResponse;
55
import org.apache.solr.common.SolrDocument;
56
import org.apache.solr.common.SolrDocumentList;
53 57
import org.apache.solr.common.SolrInputDocument;
58
import org.apache.solr.common.util.NamedList;
54 59
import org.dataone.cn.indexer.XMLNamespaceConfig;
55 60
import org.dataone.cn.indexer.parser.IDocumentSubprocessor;
56 61
import org.dataone.cn.indexer.parser.SolrField;
......
61 66
import org.dataone.service.util.TypeMarshaller;
62 67
import org.jibx.runtime.JiBXException;
63 68
import org.w3c.dom.Document;
69
import org.w3c.dom.NameList;
64 70
import org.xml.sax.SAXException;
65 71

  
66 72
/**
......
70 76
 */
71 77
public class SolrIndex {
72 78
            
79
    public static final String ID = "id";
80
    private static final String IDQUERY = ID+":*";
73 81
    private List<IDocumentSubprocessor> subprocessors = null;
74 82
    private SolrServer solrServer = null;
75 83
    private XMLNamespaceConfig xmlNamespaceConfig = null;
......
372 380
     * Get the solrServer
373 381
     * @return
374 382
     */
375
    SolrServer getSolrServer() {
383
    public SolrServer getSolrServer() {
376 384
        return solrServer;
377 385
    }
378 386

  
379 387
    /**
380
     * Set the solrServer. This method is only for setting a test solr server in the junit test.
388
     * Set the solrServer. 
381 389
     * @param solrServer
382 390
     */
383
    void setSolrServer(SolrServer solrServer) {
391
    public void setSolrServer(SolrServer solrServer) {
384 392
        this.solrServer = solrServer;
385 393
    }
394
    
395
    /**
396
     * Get all indexed ids in the solr server.
397
     * @return
398
     * @throws SolrServerException
399
     */
400
    public List<String> getSolrIds() throws SolrServerException {
401
        List<String> list = new ArrayList<String>();
402
        SolrQuery query = new SolrQuery(IDQUERY); 
403
        query.setRows(Integer.MAX_VALUE); 
404
        query.setFields(ID); 
405
        QueryResponse response = solrServer.query(query); 
406
        SolrDocumentList docs = response.getResults();
407
        if(docs != null) {
408
            for(SolrDocument doc :docs) {
409
                String identifier = (String)doc.getFieldValue(ID);
410
                //System.out.println("======================== "+identifier);
411
                list.add(identifier);
412
            }
413
        }
414
        return list;
415
    }
386 416
}
metacat-index/src/main/java/edu/ucsb/nceas/metacat/index/SystemMetadataEventListener.java
187 187
        }
188 188
        return obsoletes;
189 189
    }
190
    
191
    
192
    /**
193
     * Get an InputStream as the data object for the specific pid.
194
     * @param pid
195
     * @return
196
     * @throws FileNotFoundException
197
     */
198
    public InputStream getDataObject(String pid) throws FileNotFoundException {
199
        String objectPath = objectPathMap.get(pid);
200
        InputStream data = null;
201
        data = new FileInputStream(objectPath);
202
        return data;
190 203

  
204
    }
205

  
191 206
	public void entryAdded(EntryEvent<Identifier, SystemMetadata> entryEvent) {
192 207
		// use the same implementation for insert/update for now
193 208
		this.entryUpdated(entryEvent);

Also available in: Unified diff