/** Write the document to the index and close it */ public void finish() { try { indexWriter.commit(); indexWriter.close(); } catch (IOException ex) { System.err.println("We had a problem closing the index: " + ex.getMessage()); } }
public void addDocument(JSONObject object) { Document doc = new Document(); String body = (String) object.get("body"); String id = (String) object.get("id"); long idLong = Long.valueOf(id, 36); // System.out.println(body); doc.add(new TextField("body", body, Field.Store.YES)); doc.add(new LongField("id", idLong, Field.Store.YES)); // doc.add(new StoredField("body_store",CompressionTools.compressString(body))); try { indexWriter.addDocument(doc); } catch (IOException ex) { System.err.println("Error adding documents to the index. " + ex.getMessage()); } catch (Exception ex) { System.err.println("Error adding documents to the index. " + ex.getMessage()); } }
/** * Add documents to the index * * @param jsonObjects */ public void addDocuments(JSONArray jsonObjects) { for (JSONObject object : (List<JSONObject>) jsonObjects) { Document doc = new Document(); // for(String field : (Set<String>) object.keySet()){ // if(object==null || object.get(field)==null) // continue; // Class type = object.get(field).getClass(); // if(type==null) // continue; // if(type.equals(String.class)){ // doc.add(new StringField(field, (String)object.get(field), // Field.Store.NO)); // }else if(type.equals(Long.class)){ // doc.add(new LongField(field, (long)object.get(field), Field.Store.YES)); // }else if(type.equals(Double.class)){ // doc.add(new DoubleField(field, (double)object.get(field), // Field.Store.YES)); // }else if(type.equals(Boolean.class)){ // doc.add(new StringField(field, object.get(field).toString(), // Field.Store.YES)); // } // } String body = (String) object.get("body"); String id = (String) object.get("id"); long idLong = Long.valueOf(id, 36); // System.out.println(body); doc.add(new TextField("body", body, Field.Store.YES)); doc.add(new LongField("id", idLong, Field.Store.YES)); try { indexWriter.addDocument(doc); } catch (IOException ex) { System.err.println("Error adding documents to the index. " + ex.getMessage()); } catch (Exception ex) { System.err.println("Error adding documents to the index. " + ex.getMessage()); } } }