Пример #1
0
 /** 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());
   }
 }
Пример #2
0
 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());
   }
 }
Пример #3
0
  public JSONArray readJsonFile() {

    BufferedReader br = null;
    JSONParser parser = new JSONParser();
    JSONArray array = new JSONArray();
    try {

      String sCurrentLine;
      br = getBufferedReaderForCompressedFile(jsonFilePath);
      //            br = new BufferedReader(new FileReader(jsonFilePath));

      while ((sCurrentLine = br.readLine()) != null) {
        //                System.out.println("Record:\t" + sCurrentLine);

        Object obj;
        try {
          obj = parser.parse(sCurrentLine);
          JSONObject jsonObject = (JSONObject) obj;
          array.add(jsonObject);

        } catch (ParseException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

    } catch (IOException e) {
      e.printStackTrace();
    } catch (CompressorException ex) {
      ex.printStackTrace();
    } finally {
      try {
        if (br != null) br.close();
      } catch (IOException ex) {
        ex.printStackTrace();
      }
    }
    return array;
  }
Пример #4
0
 /**
  * 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());
     }
   }
 }