List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory
public RAMDirectory()
From source file:com.mathworks.xzheng.tools.SpatialLuceneExample.java
License:Apache License
SpatialLuceneExample() throws IOException { directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new WhitespaceAnalyzer(Version.LUCENE_46)); writer = new IndexWriter(directory, config); }
From source file:com.meltwater.elasticsearch.index.RamDirectoryPercolatorIndex.java
License:Apache License
public Directory indexDocuments(List<ParsedDocument> parsedDocuments) { try {// w ww .j a va 2 s . c om Directory directory = new RAMDirectory(); IndexWriterConfig conf = new IndexWriterConfig(Version.LUCENE_4_10_4, mapperService.analysisService().defaultIndexAnalyzer()); IndexWriter iwriter = new IndexWriter(directory, conf); for (ParsedDocument document : parsedDocuments) { for (ParseContext.Document doc : document.docs()) { iwriter.addDocument(doc, document.analyzer()); } } iwriter.close(); return directory; } catch (IOException e) { throw new ElasticsearchException("Failed to write documents to RAMDirectory", e); } }
From source file:com.miliworks.virgo.test.LuceneIndexAndSearchDemo.java
License:Apache License
/** * //w w w . j av a 2 s . c o m * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text = "IK Analyzer???????"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(true); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(Version.LUCENE_40, analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc = new Document(); doc.add(new StringField("ID", "10000", Field.Store.YES)); doc.add(new TextField(fieldName, text, Field.Store.YES)); iwriter.addDocument(doc); iwriter.close(); //?********************************** //? ireader = DirectoryReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(Version.LUCENE_40, fieldName, analyzer); qp.setDefaultOperator(QueryParser.AND_OPERATOR); Query query = qp.parse(keyword); System.out.println("Query = " + query); //?5? TopDocs topDocs = isearcher.search(query, 5); System.out.println("" + topDocs.totalHits); // ScoreDoc[] scoreDocs = topDocs.scoreDocs; for (int i = 0; i < topDocs.totalHits; i++) { Document targetDoc = isearcher.doc(scoreDocs[i].doc); System.out.println("" + targetDoc.toString()); } } catch (CorruptIndexException e) { e.printStackTrace(); } catch (LockObtainFailedException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } catch (ParseException e) { e.printStackTrace(); } finally { if (ireader != null) { try { ireader.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory != null) { try { directory.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.mmiagency.knime.nodes.keyworddensity.util.KeywordDensityHelper.java
License:Open Source License
public void execute() throws IOException { org.jsoup.nodes.Document jdoc = null; // pull content using Jsoup if (m_content != null && !m_content.trim().isEmpty()) { jdoc = Jsoup.parse(m_content);/*from w w w .ja v a 2 s. c o m*/ } else { Connection conn = Jsoup.connect(m_url); conn.validateTLSCertificates(false); conn.followRedirects(true); conn.userAgent("Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:40.0) Gecko/20100101 Firefox/40.0"); conn.header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); conn.header("Accept-Language", "en-US,en;q=0.5"); conn.header("Accept-Encoding", "gzip, deflate"); conn.execute(); jdoc = conn.get(); } StringWriter text = new StringWriter(); if (m_includeMetaKeywords) { text.write(jdoc.select("meta[name=keywords]").attr("content")); text.write(" "); } if (m_includeMetaDescription) { text.write(jdoc.select("meta[name=description]").attr("content")); text.write(" "); } if (m_includePageTitle) { text.write(jdoc.select("title").text()); text.write(" "); } text.write(jdoc.select("body").text()); // analyze content with Lucene StandardAnalyzer analyzer = new StandardAnalyzer(Version.LUCENE_CURRENT); Directory directory = new RAMDirectory(); IndexWriter indexWriter = new IndexWriter(directory, analyzer, MaxFieldLength.LIMITED); Document doc = new Document(); Field textField = new Field("content", text.toString(), Field.Store.YES, Field.Index.ANALYZED, TermVector.WITH_POSITIONS_OFFSETS); doc.add(textField); indexWriter.addDocument(doc); indexWriter.commit(); indexWriter.close(); IndexReader indexReader = IndexReader.open(directory, true); TermFreqVector termFreqVector = null; for (int i = 0; i < indexReader.maxDoc(); i++) { termFreqVector = indexReader.getTermFreqVector(i, "content"); String[] terms = termFreqVector.getTerms(); int[] freqs = termFreqVector.getTermFrequencies(); for (int n = 0; n < termFreqVector.size(); n++) { if (m_excludeList.contains(terms[n])) { continue; } add(terms[n], freqs[n]); } } indexReader.close(); directory.close(); // sort map by value sortMap(); }
From source file:com.mysema.query.lucene.LuceneQueryTest.java
License:Apache License
@Before public void setUp() throws Exception { final QDocument entityPath = new QDocument("doc"); title = entityPath.title;//from w w w. j a v a2 s . c o m year = entityPath.year; gross = entityPath.gross; idx = new RAMDirectory(); writer = createWriter(idx); writer.addDocument(createDocument("Jurassic Park", "Michael Crichton", "It's a UNIX system! I know this!", 1990, 90.00)); writer.addDocument( createDocument("Nummisuutarit", "Aleksis Kivi", "ESKO. Ja iloitset ja riemuitset?", 1864, 10.00)); writer.addDocument(createDocument("The Lord of the Rings", "John R. R. Tolkien", "One Ring to rule them all, One Ring to find them, One Ring to bring them all and in the darkness bind them", 1954, 89.00)); writer.addDocument(createDocument("Introduction to Algorithms", "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein", "Bubble sort", 1990, 30.50)); writer.optimize(); writer.close(); searcher = new IndexSearcher(idx); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); }
From source file:com.mysema.query.lucene.LuceneQueryTest.java
License:Apache License
@Test public void Empty_Index_Should_Return_Empty_List() throws Exception { idx = new RAMDirectory(); writer = new IndexWriter(idx, new StandardAnalyzer(Version.LUCENE_30), true, MaxFieldLength.UNLIMITED); writer.optimize();/*w w w .j ava 2 s . c o m*/ writer.close(); searcher = new IndexSearcher(idx); query = new LuceneQuery(new LuceneSerializer(true, true), searcher); assertTrue(query.list().isEmpty()); }
From source file:com.mysema.query.LuceneSerializerNotTokenizedTest.java
License:Apache License
@Before public void Before() throws Exception { serializer = new LuceneSerializer(false, false); idx = new RAMDirectory(); writer = new IndexWriter(idx, new StandardAnalyzer(Version.LUCENE_30), true, MaxFieldLength.UNLIMITED); writer.addDocument(createDocument(clooney)); writer.addDocument(createDocument(pitt)); Document document = new Document(); for (String movie : Arrays.asList("Interview with the Vampire", "Up in the Air")) { document.add(new Field("movie", movie, Store.YES, Index.NOT_ANALYZED)); }// www. j a v a2 s .co m writer.addDocument(document); writer.optimize(); writer.close(); searcher = new IndexSearcher(idx); }
From source file:com.mysema.query.LuceneSerializerTest.java
License:Apache License
@Before public void setUp() throws Exception { serializer = new LuceneSerializer(true, true); entityPath = new PathBuilder<Object>(Object.class, "obj"); title = entityPath.getString("title"); author = entityPath.getString("author"); text = entityPath.getString("text"); publisher = entityPath.getString("publisher"); year = entityPath.getNumber("year", Integer.class); rating = entityPath.getString("rating"); gross = entityPath.getNumber("gross", Double.class); longField = entityPath.getNumber("longField", Long.class); shortField = entityPath.getNumber("shortField", Short.class); byteField = entityPath.getNumber("byteField", Byte.class); floatField = entityPath.getNumber("floatField", Float.class); idx = new RAMDirectory(); writer = new IndexWriter(idx, new StandardAnalyzer(Version.LUCENE_30), true, MaxFieldLength.UNLIMITED); writer.addDocument(createDocument()); writer.optimize();//from w ww .j a v a2 s .co m writer.close(); searcher = new IndexSearcher(idx); }
From source file:com.nearinfinity.blur.search.RandomSuperQueryTest.java
License:Apache License
private Directory createIndex(Random random, Collection<String> sampler) throws CorruptIndexException, LockObtainFailedException, IOException { Directory directory = new RAMDirectory(); String[] columnFamilies = genWords(random, MIN_NUM_COL_FAM, MAX_NUM_COL_FAM, "colfam"); Map<String, String[]> columns = new HashMap<String, String[]>(); for (int i = 0; i < columnFamilies.length; i++) { columns.put(columnFamilies[i], genWords(random, MIN_NUM_COLS, MAX_NUM_COLS, "col")); }//from ww w .j av a 2 s . co m IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION))); RowIndexWriter indexWriter = new RowIndexWriter(writer, new BlurAnalyzer(new StandardAnalyzer(LUCENE_VERSION))); int numberOfDocs = random.nextInt(MAX_NUM_OF_DOCS) + 1; for (int i = 0; i < numberOfDocs; i++) { indexWriter.replace(false, generatSuperDoc(random, columns, sampler)); } writer.close(); return directory; }
From source file:com.nearinfinity.blur.search.SuperQueryTest.java
License:Apache License
public static Directory createIndex() throws CorruptIndexException, LockObtainFailedException, IOException { Directory directory = new RAMDirectory(); IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LUCENE_VERSION, new StandardAnalyzer(LUCENE_VERSION))); BlurAnalyzer analyzer = new BlurAnalyzer(new StandardAnalyzer(LUCENE_VERSION)); RowIndexWriter indexWriter = new RowIndexWriter(writer, analyzer); indexWriter.replace(false,//from www. j a va 2s . c om newRow("1", newRecord(PERSON, UUID.randomUUID().toString(), newColumn(NAME, NAME1)), newRecord(PERSON, UUID.randomUUID().toString(), newColumn(NAME, NAME1)), newRecord(ADDRESS, UUID.randomUUID().toString(), newColumn(STREET, STREET1)))); indexWriter.replace(false, newRow("2", newRecord(PERSON, UUID.randomUUID().toString(), newColumn(NAME, NAME2)), newRecord(ADDRESS, UUID.randomUUID().toString(), newColumn(STREET, STREET1)))); indexWriter.replace(false, newRow("3", newRecord(PERSON, UUID.randomUUID().toString(), newColumn(NAME, NAME1)), newRecord(ADDRESS, UUID.randomUUID().toString(), newColumn(STREET, STREET2)))); ; writer.close(); return directory; }