Example usage for org.apache.lucene.index IndexWriter commit

List of usage examples for org.apache.lucene.index IndexWriter commit

Introduction

In this page you can find the example usage for org.apache.lucene.index IndexWriter commit.

Prototype

@Override
public final long commit() throws IOException 

Source Link

Document

Commits all pending changes (added and deleted documents, segment merges, added indexes, etc.) to the index, and syncs all referenced index files, such that a reader will see the changes and the index updates will survive an OS or machine crash or power loss.

Usage

From source file:nmsu.cs.TFIDFVector.java

License:Open Source License

/**
 * build Lucene index for the text/* w  w w. j  av  a2s.  co  m*/
 * @param indexDir
 */
public void buildIndex(String indexDir) {

    //      System.out.println(Debugger.getCallerPosition()+" is RawData null "+rawdata==null);

    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_36, new PorterAnalyzer());
    IndexWriter w;
    try {
        w = new IndexWriter(FSDirectory.open(new File(indexDir)), conf);
        w.deleteAll();
        w.commit();

        System.out.println(rawdata.id2Docs.size());

        for (Map.Entry<Integer, Doc> entry : rawdata.id2Docs.entrySet()) {
            Document document = BaseLineMethod.convertDoc2Document(entry.getValue());
            w.addDocument(document);
        }

        w.commit();
        w.close();

    } catch (CorruptIndexException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (LockObtainFailedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:opennlp.addons.geoentitylinker.indexing.GazetteerIndexer.java

License:Apache License

/**
 *
 * @param geonamesData the actual Geonames gazetteer data downloaded from
 * here: http://download.geonames.org/export/dump/ then click on this link
 * 'allCountries.zip'//from w ww .  j a va2s  .  c o  m
 * @param geoNamesCountryInfo the countryinfo lookup table that can be
 * downloaded from here
 * http://download.geonames.org/export/dump/countryInfo.txt You'll need to
 * copy the page into a file or scrape it
 * @param geonamesAdmin1CodesASCII The lookup data for the province names for
 * each place found here:
 * http://download.geonames.org/export/dump/admin1CodesASCII.txt highlight the
 * table view, and copy results into a text file. Make sure the tab delimitted
 * format is maintained.
 * @param usgsDataFile the actual USGS gazetteer downloaded from here:
 * http://geonames.usgs.gov/domestic/download_data.htm click on the
 * national_file####.zip link to get all the most recent features
 *
 * @param usgsGovUnitsFile go to here:
 * http://geonames.usgs.gov/domestic/download_data.htm in the section titled
 * "Topical Gazetteers -- File Format" click on the drop down list and select
 * "Government Units". The downloaded file is what you need for this param.
 * @param outputIndexDir where you want the final index. Must be a directory,
 * not an actual file.
 * @param outputCountryContextFile The output countrycontext file. THis is a
 * very important file used inside the GeoEntityLinker to assist in toponym
 * resolution.
 * @param regionsFile this file contains a list of regions in the following
 * format: tab delimitted text with index 0 as the name of the region, index 1
 * as the longitude, and index 2 as the latitude
 * @throws Exception
 */
public void index(File geonamesData, File geoNamesCountryInfo, File geonamesAdmin1CodesASCII, File usgsDataFile,
        File usgsGovUnitsFile, File outputIndexDir, File outputCountryContextFile, File regionsFile)
        throws Exception {
    if (!outputIndexDir.isDirectory()) {
        throw new IllegalArgumentException("outputIndexDir must be a directory.");
    }
    if (!geonamesData.exists()) {
        throw new FileNotFoundException("geonames data file does not exist");
    }
    if (!geoNamesCountryInfo.exists()) {
        throw new FileNotFoundException("geoNamesCountryCodes data file does not exist");
    }
    if (!geonamesAdmin1CodesASCII.exists()) {
        throw new FileNotFoundException("geonamesAdmin1CodesASCII data file does not exist");
    }

    if (!usgsDataFile.exists()) {
        throw new FileNotFoundException("usgsDataFile data file does not exist");
    }
    if (!usgsGovUnitsFile.exists()) {
        throw new FileNotFoundException("usgsGovUnitsFile data file does not exist");
    }
    if (!outputIndexDir.exists()) {
        throw new FileNotFoundException("outputIndexDir data file does not exist");
    }
    if (!regionsFile.exists()) {
        throw new FileNotFoundException("regionsFile data file does not exist");
    }

    String indexloc = outputIndexDir.getPath() + "/opennlp_geoentitylinker_gazetteer";
    Directory index = new MMapDirectory(Paths.get(indexloc));
    Analyzer a = new StandardAnalyzer(new CharArraySet(new ArrayList(), true));
    Map<String, Analyzer> analyMap = new HashMap<>();

    analyMap.put("countrycode", new KeywordAnalyzer());
    analyMap.put("admincode", new KeywordAnalyzer());
    analyMap.put("loctype", new KeywordAnalyzer());
    analyMap.put("countycode", new KeywordAnalyzer());
    analyMap.put("gazsource", new KeywordAnalyzer());

    PerFieldAnalyzerWrapper aWrapper = new PerFieldAnalyzerWrapper(a, analyMap);

    IndexWriterConfig config = new IndexWriterConfig(aWrapper);

    IndexWriter w = new IndexWriter(index, config);

    //write the column headers for the countryContextFile 
    FileWriter countryContextFileWriter = new FileWriter(outputCountryContextFile, false);
    String colNamesForCountryContextFile = "countrycode\tprovcode\tcountycode\tcountryname\tprovincename\tcountyname\tcountryregex\tprovregex\tcountyregex\n";
    countryContextFileWriter.write(colNamesForCountryContextFile);
    countryContextFileWriter.flush();
    countryContextFileWriter.close();

    USGSProcessor.process(usgsGovUnitsFile, usgsDataFile, outputCountryContextFile, w);

    GeonamesProcessor.process(geoNamesCountryInfo, geonamesAdmin1CodesASCII, geonamesData,
            outputCountryContextFile, w);

    RegionProcessor.process(regionsFile, outputCountryContextFile, w);
    w.commit();
    w.close();
    System.out.println("\nIndexing complete. Be sure to add '" + indexloc + "' and context file '"
            + outputCountryContextFile.getPath() + "' to entitylinker.properties file");
}

From source file:opennlp.addons.geoentitylinker.indexing.GeonamesProcessor.java

License:Apache License

/**
 *
 * @param gazateerInputData the Geonames allCounties.txt file
 * @param type the types of gaz entry, usgs, geonames, or regions
 * @param adms the province info/*from ww  w.j av a2  s  . c o m*/
 * @param countrycodes the country code info
 * @param w the lucene index writer
 * @throws Exception
 */
public static void readFile(File gazateerInputData, GazetteerIndexer.GazType type,
        Map<String, AdminBoundary> adms, Map<String, String> countrycodes, IndexWriter w) throws Exception {

    BufferedReader reader = new BufferedReader(new FileReader(gazateerInputData));
    String[] boosts = "ADM1 ADM1H ADM2 ADM2H ADM3 ADM3H ADM4 ADM4H ADM5 ADMD ADMDH PCLD PCLH PCLI PCLIX TERR PCLIX PPL PPLA PPLA2 PPLA3 PPLA4 PPLC PPLCH PPLF PPLG PPLH PPLL PPLQ PPLR PPLS PPLX STLMT"
            .split(" ");
    Map<String, Float> boostMap = new HashMap<>();
    for (String boost : boosts) {
        boostMap.put(boost.toLowerCase(), 10f);
    }
    String[] fieldStrings = new String[] { "geonameid", "name", "asciiname", "alternatenames", "latitude",
            "longitude", "feature_class", "feature_code", "country code", "cc2", "admin1_code", "admin2_code",
            "admin3_code", "admin4_code", "population", "elevation", "dem ", "timezone", "modification_date" };

    List<String> fields = Arrays.asList(fieldStrings);
    int counter = 0;
    System.out.println("reading gazetteer data from file...........");
    String line = "";
    while ((line = reader.readLine()) != null) {
        String[] values = line.split(type.getSeparator());

        Document doc = new Document();
        String admincode = values[10].toLowerCase();
        String ccode = values[8].toLowerCase();
        if (ccode.contains(",")) {
            String[] codes = ccode.split(",");
            if (codes.length > 0) {
                ccode = codes[0];
            }
        }
        AdminBoundary adm = adms.get(ccode + "." + admincode);

        String placeName = values[2];
        String lat = values[4];
        String lon = values[5];
        String dsg = values[7].toLowerCase();

        String id = values[0];
        String concatIndexEntry = "";
        String countryname = "";
        if (adm != null) {
            concatIndexEntry = adm.getCountryName() + ", " + adm.getProvinceName() + ", " + placeName;
            countryname = adm.getCountryName();
        } else {
            //there is no admin info, but we can still use the countrycode to concat the country name
            String n = countrycodes.get(ccode);
            countryname = n;
            if (n != null) {
                concatIndexEntry = n + ", " + placeName;
            } else {
                ///don't want a single token hierarchy entry.
                concatIndexEntry = "";
            }
        }
        if (ccode == null) {
            System.out.println("naughty country code");
        }
        for (int i = 0; i < fields.size() - 1; i++) {
            doc.add(new TextField(fields.get(i), values[i].trim(), Field.Store.YES));

        }
        if (dsg.equals("pcli")) {
            System.out.println("placename: " + placeName + " RESET TO: " + countryname);
            placeName = countryname;
        }
        /**
         * add standard fields to the index
         */
        doc.add(new TextField("hierarchy", concatIndexEntry, Field.Store.YES));
        doc.add(new TextField("placename", placeName, Field.Store.YES));
        // doc.add(new TextField("countryname", countryname, Field.Store.YES));
        //System.out.println(placeName);

        doc.add(new TextField("latitude", lat, Field.Store.YES));
        doc.add(new TextField("longitude", lon, Field.Store.YES));
        doc.add(new StringField("loctype", dsg, Field.Store.YES));
        doc.add(new StringField("admincode", (ccode + "." + admincode).toLowerCase(), Field.Store.YES));
        doc.add(new StringField("countrycode", ccode.toLowerCase(), Field.Store.YES));
        doc.add(new StringField("countycode", "", Field.Store.YES));
        doc.add(new StringField("locid", id, Field.Store.YES));
        placeName = placeName.replace("republic of", "").replace("federative", "");
        if (id.equals("3175395")) {
            System.out.println(placeName);
        }
        doc.add(new StringField("gazsource", "geonames", Field.Store.YES));

        w.addDocument(doc);

        counter++;
        if (counter % 100000 == 0) {
            w.commit();
            System.out.println(counter + " .........Geonames entries committed to index..............");
        }

    }

    System.out.println("Completed indexing geonames gaz! index name is: " + type.toString());
}

From source file:opennlp.addons.geoentitylinker.indexing.RegionProcessor.java

License:Apache License

public static void readFile(File gazateerInputData, File outputCountryContextfile, IndexWriter w)
        throws Exception {
    List<String> ccfileentries = new ArrayList<>();
    BufferedReader reader = new BufferedReader(new FileReader(gazateerInputData));
    List<String> fields = new ArrayList<>();
    int counter = 0;
    System.out.println("reading gazetteer data from Regions file...........");
    String line = "";
    while ((line = reader.readLine()) != null) {

        String[] values = line.split("\t");
        if (counter == 0) {

        } else {//from   w  w w.ja va2 s  .  co  m
            Document doc = new Document();
            for (int i = 0; i < fields.size() - 1; i++) {
                doc.add(new TextField(fields.get(i), values[i].trim(), Field.Store.YES));
            }
            String placeName = values[0];
            String lat = values[2];
            String lon = values[1];
            String dsg = "region";
            String id = "rg" + counter;

            String hierarchy = placeName;

            doc.add(new TextField("hierarchy", hierarchy, Field.Store.YES));
            doc.add(new TextField("placename", placeName, Field.Store.YES));
            doc.add(new StringField("latitude", lat, Field.Store.YES));
            doc.add(new StringField("longitude", lon, Field.Store.YES));
            doc.add(new StringField("loctype", dsg, Field.Store.YES));
            doc.add(new StringField("admincode", "", Field.Store.YES));
            doc.add(new StringField("countrycode", id, Field.Store.YES));
            doc.add(new StringField("countycode", "", Field.Store.YES));

            doc.add(new StringField("locid", id, Field.Store.YES));
            doc.add(new StringField("gazsource", "region", Field.Store.YES));
            //countrycontext file format
            // US   KY   131   United States   Kentucky   Leslie

            ccfileentries.add(id + "\t" + id + "\t" + id + "\t" + placeName + "\t" + "NO_DATA_FOUND" + "\t"
                    + "NO_DATA_FOUND" + "\t" + "(" + placeName + ")" + "\t" + "NO_DATA_FOUND" + "\t"
                    + "NO_DATA_FOUND" + "\n");
            if (w != null) {
                w.addDocument(doc);
            }
        }
        counter++;

    }
    if (w != null) {
        w.commit();
    }
    FileWriter writer = new FileWriter(outputCountryContextfile, true);
    for (String string : ccfileentries) {
        writer.write(string);
    }
    System.out.println("successfully wrote Region entries to country oontext file");
    writer.close();
    System.out.println("Completed indexing regions!");
}

From source file:opennlp.addons.geoentitylinker.indexing.USGSProcessor.java

License:Apache License

public static void readFile(File gazateerInputData, IndexWriter w, GazetteerIndexer.GazType type,
        Map<String, AdminBoundary> lookupMap) throws Exception {

    Map<String, StateCentroid> states = new HashMap<>();
    BufferedReader reader = new BufferedReader(new FileReader(gazateerInputData));
    List<String> fields = new ArrayList<>();
    int counter = 0;
    System.out.println("reading gazetteer data from USGS file...........");
    String line = "";
    while ((line = reader.readLine()) != null) {

        String[] values = line.split(type.getSeparator());
        if (counter == 0) {
            for (String columnName : values) {
                fields.add(columnName.replace("", "").trim());
            }//from  w  w w .ja v a 2  s  .  c om

        } else {
            Document doc = new Document();
            for (int i = 0; i < fields.size() - 1; i++) {
                doc.add(new TextField(fields.get(i), values[i].trim(), Field.Store.YES));
            }
            String placeName = values[1];
            String lat = values[9];
            String lon = values[10];
            String dsg = values[2];
            String id = values[0];

            String ccode = values[6];
            String admincode = values[3];
            AdminBoundary get = lookupMap.get(admincode + "." + ccode);
            String countyname = "";
            if (get == null) {
                System.out.println("null...continuing to index" + " ccode: " + ccode + " , admincode: "
                        + admincode + " , placename: " + placeName);
                continue;

            }
            String countyCode = get.getCountyCode();

            if (!get.getCountyName().equals("NO_DATA_FOUND_VALUE")) {
                countyname = get.getCountyName();
            }
            if (!get.getCountyCode().equals("NO_DATA_FOUND_VALUE")) {
                countyCode = get.getCountyCode();
            }
            String hierarchy = get.getCountryName() + ", " + get.getProvinceName() + ", " + countyname + ", "
                    + placeName;

            if (states.containsKey(get.getProvinceName())) {
                StateCentroid entry = states.get(get.getProvinceName());
                entry.count++;
                entry.latSum += Double.valueOf(lat);
                entry.longSum += Double.valueOf(lon);
            } else {
                StateCentroid centroid = new StateCentroid();
                centroid.statecode = get.getProvCode();
                centroid.count = 1;
                centroid.latSum = Double.valueOf(lat);
                centroid.longSum = Double.valueOf(lon);
                states.put(get.getProvinceName(), centroid);
            }

            doc.add(new TextField("hierarchy", hierarchy, Field.Store.YES));
            doc.add(new TextField("placename", placeName, Field.Store.YES));
            doc.add(new TextField("latitude", lat, Field.Store.YES));
            doc.add(new TextField("longitude", lon, Field.Store.YES));
            doc.add(new StringField("loctype", dsg, Field.Store.YES));
            doc.add(new StringField("admincode", (get.getCountryCode() + "." + get.getProvCode()).toLowerCase(),
                    Field.Store.YES));
            doc.add(new StringField("countrycode", get.getCountryCode().toLowerCase(), Field.Store.YES));
            doc.add(new StringField("countycode",
                    (get.getCountryCode() + "." + get.getProvCode() + "." + countyCode).toLowerCase(),
                    Field.Store.YES));

            doc.add(new StringField("locid", id, Field.Store.YES));
            doc.add(new StringField("gazsource", "usgs", Field.Store.YES));
            w.addDocument(doc);
        }
        counter++;
        if (counter % 100000 == 0) {
            w.commit();
            System.out.println(counter + " .........USGS entries committed to index..............");
        }

    }

    for (String state : states.keySet()) {
        StateCentroid get = states.get(state);
        Document doc = new Document();
        doc.add(new TextField("hierarchy", "united states, " + state, Field.Store.YES));
        doc.add(new TextField("placename", state, Field.Store.YES));
        //calculate a centroid for all the points that were in the state
        doc.add(new TextField("latitude", (get.latSum / get.count) + "", Field.Store.YES));
        doc.add(new TextField("longitude", (get.longSum / get.count) + "", Field.Store.YES));
        doc.add(new StringField("loctype", "adm1", Field.Store.YES));
        doc.add(new StringField("admincode", get.statecode, Field.Store.YES));
        doc.add(new StringField("countrycode", "us", Field.Store.YES));
        doc.add(new StringField("countycode", "", Field.Store.YES));

        doc.add(new StringField("locid", "us_state:" + state, Field.Store.YES));
        doc.add(new StringField("gazsource", "usgs", Field.Store.YES));
        w.addDocument(doc);

        // System.out.println(get.statecode + "," + (get.latSum / get.count) + "," + (get.longSum / get.count));
    }
    Document doc = new Document();
    doc.add(new TextField("hierarchy", "united states", Field.Store.YES));
    doc.add(new TextField("placename", "united states", Field.Store.YES));
    //calculate a centroid for all the points that were in the state
    doc.add(new TextField("latitude", 39.0 + "", Field.Store.YES));
    doc.add(new TextField("longitude", -103.0 + "", Field.Store.YES));
    doc.add(new StringField("loctype", "pcli", Field.Store.YES));
    doc.add(new StringField("admincode", "", Field.Store.YES));
    doc.add(new StringField("countrycode", "us", Field.Store.YES));
    doc.add(new StringField("countycode", "", Field.Store.YES));

    doc.add(new StringField("locid", "us_centroid" + "unitedstates", Field.Store.YES));
    doc.add(new StringField("gazsource", "usgs", Field.Store.YES));
    //System.out.println("uscentroid," + (sumofLatSums / sumOfCounts) + "," + (sumofLonSums / sumOfCounts));

    w.addDocument(doc);
    w.commit();

    System.out.println("Completed indexing USGS gaz!");
}

From source file:org.aksw.palmetto.corpus.lucene.creation.PositionStoringLuceneIndexCreator.java

License:Open Source License

/**
 * Creates the index./*www  .  j a va  2s .c  om*/
 * 
 * @param indexPath
 *            The path to the director in which the Lucene index will be created
 * @param docIterator
 *            Iterator that iterates over the document texts.
 * @return true if the creation was successful, else false.
 */
public boolean createIndex(File indexPath, Iterator<IndexableDocument> docIterator) {
    LOGGER.info("Starting index creation...");
    IndexWriter writer = null;
    indexPath.mkdirs();
    Analyzer analyzer = new SimpleAnalyzer(true);
    try {
        IndexWriterConfig config = new IndexWriterConfig(version, analyzer);
        config.setOpenMode(OpenMode.CREATE);

        FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);
        fieldType.setIndexed(true);
        fieldType.setStoreTermVectors(true);
        fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS_AND_POSITIONS);
        fieldType.freeze();

        FieldType docLengthFieldType = new FieldType(IntField.TYPE_STORED);
        docLengthFieldType.setIndexed(false);
        docLengthFieldType.freeze();

        writer = new IndexWriter(FSDirectory.open(indexPath), config);
        int count = 0;
        Document indexDocument;
        IndexableDocument currentDocument;
        while (docIterator.hasNext()) {
            currentDocument = docIterator.next();
            if (currentDocument.getText().length() > 0) {
                indexDocument = toLuceneDocument(analyzer, currentDocument.getText(), fieldType);
                addDocumentLength(indexDocument, docLengthFieldName, docLengthFieldType,
                        currentDocument.getNumberOfTokens());
                writer.addDocument(indexDocument);
                ++count;
                if (count >= commitInterval) {
                    writer.commit();
                    System.gc();
                    count = 0;
                }
            }
        }
        LOGGER.info("Finished index creation.");
    } catch (IOException e) {
        LOGGER.error("Error while creating Index. Aborting.", e);
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
            }
        }
    }
    return true;
}

From source file:org.aksw.palmetto.corpus.lucene.creation.SimpleLuceneIndexCreator.java

License:Open Source License

/**
 * Creates the index./*from   ww  w  .  j av a 2 s.c  om*/
 * 
 * @param indexPath
 *            The path to the director in which the Lucene index will be created
 * @param docIterator
 *            Iterator that iterates over the document texts.
 * @return true if the creation was successful, else false.
 */
public boolean createIndex(File indexPath, Iterator<String> docIterator) {
    LOGGER.info("Starting index creation...");
    IndexWriter writer = null;
    indexPath.mkdirs();
    Analyzer analyzer = new SimpleAnalyzer(true);
    try {
        IndexWriterConfig config = new IndexWriterConfig(version, analyzer);
        config.setOpenMode(OpenMode.CREATE);

        FieldType fieldType = new FieldType(TextField.TYPE_NOT_STORED);
        fieldType.setIndexed(true);
        fieldType.setIndexOptions(IndexOptions.DOCS_AND_FREQS);
        fieldType.freeze();

        writer = new IndexWriter(FSDirectory.open(indexPath), config);
        String text;
        int count = 0;
        while (docIterator.hasNext()) {
            text = docIterator.next();
            if (text.length() > 0) {
                writer.addDocument(toLuceneDocument(analyzer, text, fieldType));
                ++count;
                if (count >= commitInterval) {
                    writer.commit();
                    System.gc();
                    count = 0;
                }
            } else {
                LOGGER.warn("Got a document without content.");
            }
        }
        LOGGER.info("Finished index creation.");
    } catch (IOException e) {
        LOGGER.error("Error while creating Index. Aborting.", e);
        return false;
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (Exception e) {
            }
        }
    }
    return true;
}

From source file:org.ala.lucene.Autocompleter.java

License:Open Source License

public Autocompleter(String autoCompleteDir) throws IOException {
    this.autoCompleteDirectory = FSDirectory.open(new File(autoCompleteDir), null);
    File file = new File(autoCompleteDir);

    // Create a dummy index so that we don't get an exception further down
    if (!file.exists()) {
        FileUtils.forceMkdir(file);/*from  w  w  w . ja  v  a  2 s. c o  m*/
        Analyzer analyzer = new StandardAnalyzer(SolrUtils.BIE_LUCENE_VERSION);
        IndexWriterConfig indexWriterConfig = new IndexWriterConfig(SolrUtils.BIE_LUCENE_VERSION, analyzer);
        Directory dir = FSDirectory.open(file);
        IndexWriter iw = new IndexWriter(dir, indexWriterConfig);
        //            IndexWriter iw = new IndexWriter(file, analyzer, MaxFieldLength.UNLIMITED);
        iw.commit();
        iw.close();
    }

    reOpenReader();
}

From source file:org.ala.lucene.SolrIndexTest.java

License:Open Source License

/**
 * @param args//from w  w w.j a v  a 2s .co  m
 */
public static void main(String[] args) throws Exception {

    System.out.print("Quick search test >>> ");
    String input = "";

    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while (!"q".equals((input = br.readLine()))) {
        long start = System.currentTimeMillis();
        if (StringUtils.trimToNull(input) != null) {
            System.out.println("---------------------------------------------");
            input = StringUtils.trimToNull(input).toLowerCase();
            //            IndexSearcher is = new IndexSearcher("/data/lucene/taxonConcept");

            String solrHome = "/data/solr/bie";

            /**
              * Initialise the SOLR server instance
              */
            System.setProperty("solr.solr.home", solrHome);
            CoreContainer coreContainer = null;
            try {
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            } catch (Exception e) {
                //FIXME this is a hack - there must be a better way of initialising SOLR here
                Directory dir = FSDirectory.open(new File(solrHome + "/index"));
                IndexWriterConfig indexWriterConfig = new IndexWriterConfig(SolrUtils.BIE_LUCENE_VERSION,
                        new StandardAnalyzer(SolrUtils.BIE_LUCENE_VERSION));
                IndexWriter idxWriter = new IndexWriter(dir, indexWriterConfig);
                idxWriter.commit();
                idxWriter.close();
                CoreContainer.Initializer initializer = new CoreContainer.Initializer();
                coreContainer = initializer.initialize();
            }

            EmbeddedSolrServer server = new EmbeddedSolrServer(coreContainer, "");

            SolrQuery query = new SolrQuery(input);
            QueryResponse qr = server.query(query);

            SolrDocumentList sdl = qr.getResults();
            Iterator iter = sdl.iterator();
            while (iter.hasNext()) {
                SolrDocument doc = (SolrDocument) iter.next();
                System.out.print(doc.getFieldValue("guid"));
                System.out.print("\t\t");
                Object valuesList = doc.getFieldValue("name");

                if (valuesList instanceof List) {
                    List<String> values = (List<String>) valuesList;
                    for (String s : values) {
                        System.out.println(">>  name: " + s);
                    }
                } else {
                    System.out.println(">>  name: " + valuesList);
                }

                System.out.print(doc.getFieldValue("guid"));
                System.out.println();
            }

            System.out.println();
        }
        System.out.print("Quick search test >>> ");
    }

    System.out.println("bye bye");
    System.exit(1);
}

From source file:org.apache.blur.command.CoreTestContext.java

License:Apache License

/**
 * Index will contain 26 documents with the following column/values: alpha =
 * double-letter a-z (lowercase characters); num = 0-25 val = val (constant
 * across all docs)/* ww w .  j a  v a 2  s.co  m*/
 * 
 * New columns may be added so don't rely on the column count in tests.
 * 
 * @return
 */
public static IndexContext newSimpleAlpaNumContext() {
    CoreTestContext ctx = new CoreTestContext();

    IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_43, new StandardAnalyzer(Version.LUCENE_43));
    try {
        IndexWriter writer = new IndexWriter(ctx.directory, conf);

        for (int i = 0; i < 26; i++) {
            String alpha = new Character((char) (97 + i)).toString();
            Document doc = new Document();

            doc.add(new Field("id", Integer.toString(i), TextField.TYPE_STORED));
            doc.add(new Field("alpha", alpha + alpha, TextField.TYPE_STORED));
            doc.add(new Field("num", Integer.toString(i), TextField.TYPE_STORED));
            doc.add(new Field("val", "val", TextField.TYPE_STORED));

            writer.addDocument(doc);

            writer.commit();
        }
        writer.commit();
        writer.close();
    } catch (IOException e) {
        throw new RuntimeException("Unable to create test context.", e);
    }

    return ctx;
}