List of usage examples for org.apache.lucene.store RAMDirectory RAMDirectory
public RAMDirectory()
From source file:com.lorelib.analyzer.sample.LuceneIndexAndSearchDemo.java
License:Apache License
/** * /*from ww w . j a va2 s . c om*/ * ??? * @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(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(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.lucene.index.test.IKAnalyzerdemo.java
License:Apache License
/** * //from www . j a v a 2 s.c o m * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text1 = "oracle,?"; String text2 = "?"; String text3 = "?"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(); Directory directory1 = null; Directory directory2 = null; IndexWriter iwriter1 = null; IndexWriter iwriter2 = null; IndexReader ireader1 = null; IndexReader ireader2 = null; IndexSearcher isearcher = null; try { // directory1 = new RAMDirectory(); directory2 = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig1 = new IndexWriterConfig(analyzer); iwConfig1.setOpenMode(OpenMode.CREATE); IndexWriterConfig iwConfig2 = new IndexWriterConfig(analyzer); iwConfig2.setOpenMode(OpenMode.CREATE); iwriter1 = new IndexWriter(directory1, iwConfig1); iwriter2 = new IndexWriter(directory2, iwConfig2); // Document doc1 = new Document(); doc1.add(new StringField("ID", "10000", Field.Store.YES)); doc1.add(new TextField("text1", text1, Field.Store.YES)); iwriter1.addDocument(doc1); Document doc2 = new Document(); doc2.add(new StringField("ID", "10001", Field.Store.YES)); doc2.add(new TextField("text2", text2, Field.Store.YES)); iwriter2.addDocument(doc2); iwriter1.close(); iwriter2.close(); //?********************************** //? ireader1 = DirectoryReader.open(directory1); ireader2 = DirectoryReader.open(directory2); IndexReader[] mreader = { ireader1, ireader2 }; MultiReader multiReader = new MultiReader(mreader); isearcher = new IndexSearcher(multiReader); String keyword = "?"; //QueryParser?Query String[] fields = { "text1", "text2" }; Map<String, Float> boosts = new HashMap<String, Float>(); boosts.put("text1", 5.0f); boosts.put("text2", 2.0f); /**MultiFieldQueryParser??? * */ MultiFieldQueryParser parser = new MultiFieldQueryParser(fields, analyzer, boosts); Query query = parser.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 (ireader1 != null) { try { ireader1.close(); ireader2.close(); } catch (IOException e) { e.printStackTrace(); } } if (directory1 != null) { try { directory1.close(); directory2.close(); } catch (IOException e) { e.printStackTrace(); } } } }
From source file:com.lucene.index.test.IKAnalyzerdemoMutilField.java
License:Apache License
/** * /*w ww.j a v a 2 s. co m*/ * ??? * @param args */ public static void main(String[] args) { //Lucene Document?? String fieldName = "text"; // String text1 = "oracle?"; String text2 = "?"; String text3 = "?"; //IKAnalyzer? Analyzer analyzer = new IKAnalyzer(); Directory directory = null; IndexWriter iwriter = null; IndexReader ireader = null; IndexSearcher isearcher = null; try { // directory = new RAMDirectory(); //?IndexWriterConfig IndexWriterConfig iwConfig = new IndexWriterConfig(analyzer); iwConfig.setOpenMode(OpenMode.CREATE_OR_APPEND); iwriter = new IndexWriter(directory, iwConfig); // Document doc1 = new Document(); doc1.add(new StringField("ID", "10000", Field.Store.YES)); doc1.add(new TextField(fieldName, text1, Field.Store.YES)); iwriter.addDocument(doc1); Document doc2 = new Document(); doc2.add(new StringField("ID", "10000", Field.Store.YES)); doc2.add(new TextField(fieldName, text2, Field.Store.YES)); iwriter.addDocument(doc2); Document doc3 = new Document(); doc3.add(new StringField("ID", "10000", Field.Store.YES)); doc3.add(new TextField(fieldName, text3, Field.Store.YES)); iwriter.addDocument(doc3); iwriter.close(); //?********************************** //? ireader = DirectoryReader.open(directory); isearcher = new IndexSearcher(ireader); String keyword = "?"; //QueryParser?Query QueryParser qp = new QueryParser(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.mathworks.xzheng.advsearching.MultiPhraseQueryTest.java
License:Apache License
protected void setUp() throws Exception { Directory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new WhitespaceAnalyzer(Version.LUCENE_46)); IndexWriter writer = new IndexWriter(directory, config); Document doc1 = new Document(); doc1.add(new Field("field", "the quick brown fox jumped over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc1);//from w w w . j a v a 2 s . c om Document doc2 = new Document(); doc2.add(new Field("field", "the fast fox hopped over the hound", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc2); writer.close(); searcher = new IndexSearcher(DirectoryReader.open(directory)); }
From source file:com.mathworks.xzheng.advsearching.MultiSearcherTest.java
License:Apache License
public void setUp() throws Exception { String[] animals = { "aardvark", "beaver", "coati", "dog", "elephant", "frog", "gila monster", "horse", "iguana", "javelina", "kangaroo", "lemur", "moose", "nematode", "orca", "python", "quokka", "rat", "scorpion", "tarantula", "uromastyx", "vicuna", "walrus", "xiphias", "yak", "zebra" }; Analyzer analyzer = new WhitespaceAnalyzer(Version.LUCENE_46); Directory aTOmDirectory = new RAMDirectory(); // #1 Directory nTOzDirectory = new RAMDirectory(); // #1 IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer); IndexWriter aTOmWriter = new IndexWriter(aTOmDirectory, config); IndexWriter nTOzWriter = new IndexWriter(nTOzDirectory, config); for (int i = animals.length - 1; i >= 0; i--) { Document doc = new Document(); String animal = animals[i]; doc.add(new Field("animal", animal, Field.Store.YES, Field.Index.NOT_ANALYZED)); if (animal.charAt(0) < 'n') { aTOmWriter.addDocument(doc); // #2 } else {// w w w .ja va 2 s.c o m nTOzWriter.addDocument(doc); // #2 } } aTOmWriter.close(); nTOzWriter.close(); searchers = new IndexSearcher[2]; searchers[0] = new IndexSearcher(DirectoryReader.open(aTOmDirectory)); searchers[1] = new IndexSearcher(DirectoryReader.open(nTOzDirectory)); }
From source file:com.mathworks.xzheng.advsearching.SecurityFilterTest.java
License:Apache License
protected void setUp() throws Exception { Directory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new WhitespaceAnalyzer(Version.LUCENE_46)); IndexWriter writer = new IndexWriter(directory, config); Document document = new Document(); // 1 document.add(new Field("owner", // 1 "elwood", // 1 Field.Store.YES, // 1 Field.Index.NOT_ANALYZED)); // 1 document.add(new Field("keywords", // 1 "elwood's sensitive info", // 1 Field.Store.YES, // 1 Field.Index.ANALYZED)); // 1 writer.addDocument(document);/* w w w . java2 s .co m*/ document = new Document(); // 2 document.add(new Field("owner", // 2 "jake", // 2 Field.Store.YES, // 2 Field.Index.NOT_ANALYZED)); // 2 document.add(new Field("keywords", // 2 "jake's sensitive info", // 2 Field.Store.YES, // 2 Field.Index.ANALYZED)); // 2 writer.addDocument(document); writer.close(); searcher = new IndexSearcher(DirectoryReader.open(directory)); }
From source file:com.mathworks.xzheng.advsearching.SpanQueryTest.java
License:Apache License
protected void setUp() throws Exception { directory = new RAMDirectory(); analyzer = new WhitespaceAnalyzer(Version.LUCENE_46); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer); IndexWriter writer = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new Field("f", "the quick brown fox jumps over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from w ww.ja v a 2s .c om doc = new Document(); doc.add(new Field("f", "the quick red fox jumps over the sleepy cat", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc); writer.close(); searcher = new IndexSearcher(DirectoryReader.open(directory)); reader = searcher.getIndexReader(); quick = new SpanTermQuery(new Term("f", "quick")); brown = new SpanTermQuery(new Term("f", "brown")); red = new SpanTermQuery(new Term("f", "red")); fox = new SpanTermQuery(new Term("f", "fox")); lazy = new SpanTermQuery(new Term("f", "lazy")); sleepy = new SpanTermQuery(new Term("f", "sleepy")); dog = new SpanTermQuery(new Term("f", "dog")); cat = new SpanTermQuery(new Term("f", "cat")); }
From source file:com.mathworks.xzheng.analysis.codec.MetaphoneAnalyzerTest.java
License:Apache License
public void testKoolKat() throws Exception { RAMDirectory directory = new RAMDirectory(); Analyzer analyzer = new MetaphoneReplacementAnalyzer(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, analyzer); config.setOpenMode(IndexWriterConfig.OpenMode.CREATE); IndexWriter writer = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new Field("contents", //#A "cool cat", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);/*ww w . j a va 2 s. c o m*/ writer.close(); IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(directory)); Query query = new QueryParser(Version.LUCENE_46, //#B "contents", analyzer) //#B .parse("kool kat"); //#B TopDocs hits = searcher.search(query, 1); assertEquals(1, hits.totalHits); //#C int docID = hits.scoreDocs[0].doc; doc = searcher.doc(docID); assertEquals("cool cat", doc.get("contents")); //#D }
From source file:com.mathworks.xzheng.analysis.keyword.KeywordAnalyzerTest.java
License:Apache License
public void setUp() throws Exception { Directory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, new SimpleAnalyzer(Version.LUCENE_46)); IndexWriter writer = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new Field("partnum", "Q36", Field.Store.NO, Field.Index.NOT_ANALYZED_NO_NORMS)); //A doc.add(new Field("description", "Illidium Space Modulator", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//w ww .j ava 2 s . c o m writer.close(); searcher = new IndexSearcher(DirectoryReader.open(directory)); }
From source file:com.mathworks.xzheng.analysis.positional.PositionalPorterStopAnalyzerTest.java
License:Apache License
public void setUp() throws Exception { RAMDirectory directory = new RAMDirectory(); IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_46, porterAnalyzer); IndexWriter writer = new IndexWriter(directory, config); Document doc = new Document(); doc.add(new Field("contents", "The quick brown fox jumps over the lazy dog", Field.Store.YES, Field.Index.ANALYZED)); writer.addDocument(doc);//from www . j ava 2s . c o m writer.close(); searcher = new IndexSearcher(DirectoryReader.open(directory)); parser = new QueryParser(Version.LUCENE_46, "contents", porterAnalyzer); }