private int indexRandomNumbers(String analyzer, int shards, boolean dummyDocs) throws Exception { Builder builder = Settings.builder().put(indexSettings()); if (shards > 0) { builder.put(SETTING_NUMBER_OF_SHARDS, shards); } assertAcked(prepareCreate("test") .addMapping( "type1", jsonBuilder().startObject().startObject("type1").startObject("properties").startObject("field1") .field("analyzer", analyzer).field("type", "text").endObject().endObject().endObject().endObject()) .setSettings(builder)); int numDocs = randomIntBetween(100, 150); IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; i++) { docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource("field1", English.intToEnglish(i)); } indexRandom(true, dummyDocs, docs); ensureGreen(); return numDocs; }
/** * This test makes sure no profile results are returned when profiling is disabled */ public void testNoProfile() throws Exception { createIndex("test"); ensureGreen(); int numDocs = randomIntBetween(100, 150); IndexRequestBuilder[] docs = new IndexRequestBuilder[numDocs]; for (int i = 0; i < numDocs; i++) { docs[i] = client().prepareIndex("test", "type1", String.valueOf(i)).setSource( "field1", English.intToEnglish(i), "field2", i ); } indexRandom(true, docs); refresh(); QueryBuilder q = QueryBuilders.rangeQuery("field2").from(0).to(5); logger.info("Query: {}", q); SearchResponse resp = client().prepareSearch().setQuery(q).setProfile(false).execute().actionGet(); assertThat("Profile response element should be an empty map", resp.getProfileResults().size(), equalTo(0)); }
@Override public void setUp() throws Exception { super.setUp(); dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir); int numDocs = TestUtil.nextInt(random(), 2049, 4000); for (int i = 0; i < numDocs; i++) { Document document = new Document(); document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO)); document.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO)); document.add(newStringField("byte", "" + ((byte) random().nextInt()), Field.Store.NO)); document.add(newStringField("short", "" + ((short) random().nextInt()), Field.Store.NO)); document.add(new IntField("int", random().nextInt(), Field.Store.NO)); document.add(new LongField("long", random().nextLong(), Field.Store.NO)); document.add(new FloatField("float", random().nextFloat(), Field.Store.NO)); document.add(new DoubleField("double", random().nextDouble(), Field.Store.NO)); document.add(new NumericDocValuesField("intdocvalues", random().nextInt())); document.add(new FloatDocValuesField("floatdocvalues", random().nextFloat())); iw.addDocument(document); } reader = iw.getReader(); iw.close(); searcher = newSearcher(reader); }
public void testBogusField() throws Exception { DirectSpellChecker spellChecker = new DirectSpellChecker(); Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true)); for (int i = 0; i < 20; i++) { Document doc = new Document(); doc.add(newTextField("numbers", English.intToEnglish(i), Field.Store.NO)); writer.addDocument(doc); } IndexReader ir = writer.getReader(); SuggestWord[] similar = spellChecker.suggestSimilar(new Term( "bogusFieldBogusField", "fvie"), 2, ir, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX); assertEquals(0, similar.length); ir.close(); writer.close(); dir.close(); }
public void testTransposition() throws Exception { DirectSpellChecker spellChecker = new DirectSpellChecker(); Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true)); for (int i = 0; i < 20; i++) { Document doc = new Document(); doc.add(newTextField("numbers", English.intToEnglish(i), Field.Store.NO)); writer.addDocument(doc); } IndexReader ir = writer.getReader(); SuggestWord[] similar = spellChecker.suggestSimilar(new Term( "numbers", "fvie"), 1, ir, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX); assertEquals(1, similar.length); assertEquals("five", similar[0].string); ir.close(); writer.close(); dir.close(); }
public void testTransposition2() throws Exception { DirectSpellChecker spellChecker = new DirectSpellChecker(); Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir, new MockAnalyzer(random(), MockTokenizer.SIMPLE, true)); for (int i = 0; i < 20; i++) { Document doc = new Document(); doc.add(newTextField("numbers", English.intToEnglish(i), Field.Store.NO)); writer.addDocument(doc); } IndexReader ir = writer.getReader(); SuggestWord[] similar = spellChecker.suggestSimilar(new Term( "numbers", "seevntene"), 2, ir, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX); assertEquals(1, similar.length); assertEquals("seventeen", similar[0].string); ir.close(); writer.close(); dir.close(); }
private void doTestStopPositons(StopFilter stpf, boolean enableIcrements) throws IOException { log("---> test with enable-increments-"+(enableIcrements?"enabled":"disabled")); stpf.setEnablePositionIncrements(enableIcrements); CharTermAttribute termAtt = stpf.getAttribute(CharTermAttribute.class); PositionIncrementAttribute posIncrAtt = stpf.getAttribute(PositionIncrementAttribute.class); stpf.reset(); for (int i=0; i<20; i+=3) { assertTrue(stpf.incrementToken()); log("Token "+i+": "+stpf); String w = English.intToEnglish(i).trim(); assertEquals("expecting token "+i+" to be "+w,w,termAtt.toString()); assertEquals("all but first token must have position increment of 3",enableIcrements?(i==0?1:3):1,posIncrAtt.getPositionIncrement()); } assertFalse(stpf.incrementToken()); stpf.end(); stpf.close(); }
/** * Test Position increments applied by TypeTokenFilter with and without enabling this option. */ public void testStopPositons() throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 10; i < 20; i++) { if (i % 3 != 0) { sb.append(i).append(" "); } else { String w = English.intToEnglish(i).trim(); sb.append(w).append(" "); } } log(sb.toString()); String stopTypes[] = new String[]{"<NUM>"}; Set<String> stopSet = asSet(stopTypes); // with increments StringReader reader = new StringReader(sb.toString()); TypeTokenFilter typeTokenFilter = new TypeTokenFilter(Version.LATEST, new StandardTokenizer(reader), stopSet); testPositons(typeTokenFilter); // without increments reader = new StringReader(sb.toString()); typeTokenFilter = new TypeTokenFilter(Version.LUCENE_4_3, false, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet); testPositons(typeTokenFilter); }
@BeforeClass public static void beforeClass() throws Exception { simplePayloadAnalyzer = new Analyzer() { @Override public TokenStreamComponents createComponents(String fieldName, Reader reader) { Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.SIMPLE, true); return new TokenStreamComponents(tokenizer, new SimplePayloadFilter(tokenizer)); } }; directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(simplePayloadAnalyzer) .setMaxBufferedDocs(TestUtil.nextInt(random(), 100, 1000)).setMergePolicy(newLogMergePolicy())); //writer.infoStream = System.out; for (int i = 0; i < 2000; i++) { Document doc = new Document(); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); searcher = newSearcher(reader); writer.close(); }
public void testThousandDocuments() throws Exception { Directory dir = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), dir); for (int i = 0; i < 1000; i++) { Document doc = new Document(); doc.add(newStringField("field", English.intToEnglish(i), Field.Store.NO)); writer.addDocument(doc); } IndexReader reader = writer.getReader(); writer.close(); IndexSearcher searcher = newSearcher(reader); for (int i = 0; i < 1000; i++) { TermQuery termQuery = new TermQuery(new Term("field", English.intToEnglish(i))); QueryWrapperFilter qwf = new QueryWrapperFilter(termQuery); TopDocs td = searcher.search(new MatchAllDocsQuery(), qwf, 10); assertEquals(1, td.totalHits); } reader.close(); dir.close(); }
/** * Sets up a RAMDirectory, and adds documents (using English.intToEnglish()) with two fields: field and multiField * and analyzes them using the PayloadAnalyzer * @param similarity The Similarity class to use in the Searcher * @param numDocs The num docs to add * @return An IndexSearcher */ // TODO: randomize public IndexSearcher setUp(Random random, Similarity similarity, int numDocs) throws IOException { Directory directory = new MockDirectoryWrapper(random, new RAMDirectory()); PayloadAnalyzer analyzer = new PayloadAnalyzer(); // TODO randomize this IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig( TEST_VERSION_CURRENT, analyzer).setSimilarity(similarity)); // writer.infoStream = System.out; for (int i = 0; i < numDocs; i++) { Document doc = new Document(); doc.add(new TextField(FIELD, English.intToEnglish(i), Field.Store.YES)); doc.add(new TextField(MULTI_FIELD, English.intToEnglish(i) + " " + English.intToEnglish(i), Field.Store.YES)); doc.add(new TextField(NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES)); writer.addDocument(doc); } reader = DirectoryReader.open(writer, true); writer.close(); IndexSearcher searcher = LuceneTestCase.newSearcher(reader); searcher.setSimilarity(similarity); return searcher; }
@BeforeClass public static void beforeClass() throws Exception { directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new PayloadAnalyzer()) .setSimilarity(similarity)); //writer.infoStream = System.out; for (int i = 0; i < 1000; i++) { Document doc = new Document(); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); String txt = English.intToEnglish(i) +' '+English.intToEnglish(i+1); doc.add(newTextField("field2", txt, Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); writer.close(); searcher = newSearcher(reader); searcher.setSimilarity(similarity); }
@BeforeClass public static void beforeClass() throws Exception { directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(new PayloadAnalyzer()) .setSimilarity(similarity).setMergePolicy(newLogMergePolicy())); //writer.infoStream = System.out; for (int i = 0; i < 1000; i++) { Document doc = new Document(); Field noPayloadField = newTextField(PayloadHelper.NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES); //noPayloadField.setBoost(0); doc.add(noPayloadField); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); doc.add(newTextField("multiField", English.intToEnglish(i) + " " + English.intToEnglish(i), Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); writer.close(); searcher = newSearcher(reader); searcher.setSimilarity(similarity); }
@Override public void setUp() throws Exception { super.setUp(); directory = newDirectory(); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy())); //writer.setNoCFSRatio(0.0); //writer.infoStream = System.out; FieldType customType = new FieldType(TextField.TYPE_STORED); customType.setTokenized(false); customType.setStoreTermVectors(true); for (int i = 0; i < numDocs; i++) { Document doc = new Document(); Field fld = newField("field", English.intToEnglish(i), customType); doc.add(fld); writer.addDocument(doc); } writer.close(); }
public void update(IndexWriter writer) throws IOException { // Add 10 docs: FieldType customType = new FieldType(StringField.TYPE_NOT_STORED); customType.setStoreTermVectors(true); for(int j=0; j<10; j++) { Document d = new Document(); int n = random().nextInt(); d.add(newField("id", Integer.toString(nextID++), customType)); d.add(newTextField("contents", English.intToEnglish(n), Field.Store.NO)); writer.addDocument(d); } // Delete 5 docs: int deleteID = nextID-1; for(int j=0; j<5; j++) { writer.deleteDocuments(new Term("id", ""+deleteID)); deleteID -= 2; } }
private File buildIndex() throws IOException { File path = createTempDir("buildIndex"); Directory dir = newFSDirectory(path); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE)); // add some documents Document doc = null; for (int i = 0; i < docsToAdd; i++) { doc = new Document(); doc.add(newStringField("content", English.intToEnglish(i).trim(), Field.Store.YES)); writer.addDocument(doc); } assertEquals(docsToAdd, writer.maxDoc()); writer.close(); dir.close(); return path; }
/** * Test Position increments applied by TypeTokenFilter with and without enabling this option. */ public void testStopPositons() throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 10; i < 20; i++) { if (i % 3 != 0) { sb.append(i).append(" "); } else { String w = English.intToEnglish(i).trim(); sb.append(w).append(" "); } } log(sb.toString()); String stopTypes[] = new String[]{"<NUM>"}; Set<String> stopSet = asSet(stopTypes); // with increments StringReader reader = new StringReader(sb.toString()); TypeTokenFilter typeTokenFilter = new TypeTokenFilter(true, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet); testPositons(typeTokenFilter); // without increments reader = new StringReader(sb.toString()); typeTokenFilter = new TypeTokenFilter(false, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet); testPositons(typeTokenFilter); }
@BeforeClass public static void beforeClass() throws Exception { simplePayloadAnalyzer = new Analyzer() { @Override public TokenStreamComponents createComponents(String fieldName, Reader reader) { Tokenizer tokenizer = new MockTokenizer(reader, MockTokenizer.SIMPLE, true); return new TokenStreamComponents(tokenizer, new SimplePayloadFilter(tokenizer)); } }; directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(TEST_VERSION_CURRENT, simplePayloadAnalyzer) .setMaxBufferedDocs(_TestUtil.nextInt(random(), 100, 1000)).setMergePolicy(newLogMergePolicy())); //writer.infoStream = System.out; for (int i = 0; i < 2000; i++) { Document doc = new Document(); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); searcher = newSearcher(reader); writer.close(); }
@BeforeClass public static void beforeClass() throws Exception { directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()) .setSimilarity(similarity)); //writer.infoStream = System.out; for (int i = 0; i < 1000; i++) { Document doc = new Document(); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); String txt = English.intToEnglish(i) +' '+English.intToEnglish(i+1); doc.add(newTextField("field2", txt, Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); writer.close(); searcher = newSearcher(reader); searcher.setSimilarity(similarity); }
@BeforeClass public static void beforeClass() throws Exception { directory = newDirectory(); RandomIndexWriter writer = new RandomIndexWriter(random(), directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()) .setSimilarity(similarity).setMergePolicy(newLogMergePolicy())); //writer.infoStream = System.out; for (int i = 0; i < 1000; i++) { Document doc = new Document(); Field noPayloadField = newTextField(PayloadHelper.NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES); //noPayloadField.setBoost(0); doc.add(noPayloadField); doc.add(newTextField("field", English.intToEnglish(i), Field.Store.YES)); doc.add(newTextField("multiField", English.intToEnglish(i) + " " + English.intToEnglish(i), Field.Store.YES)); writer.addDocument(doc); } reader = writer.getReader(); writer.close(); searcher = newSearcher(reader); searcher.setSimilarity(similarity); }
@Override public void setUp() throws Exception { super.setUp(); directory = newDirectory(); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setMergePolicy(newLogMergePolicy())); //writer.setUseCompoundFile(false); //writer.infoStream = System.out; FieldType customType = new FieldType(TextField.TYPE_STORED); customType.setTokenized(false); customType.setStoreTermVectors(true); for (int i = 0; i < numDocs; i++) { Document doc = new Document(); Field fld = newField("field", English.intToEnglish(i), customType); doc.add(fld); writer.addDocument(doc); } writer.close(); }
@Override public void setUp() throws Exception { super.setUp(); indexDir = _TestUtil.getTempDir("RAMDirIndex"); Directory dir = newFSDirectory(indexDir); IndexWriter writer = new IndexWriter(dir, new IndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random())).setOpenMode(OpenMode.CREATE)); // add some documents Document doc = null; for (int i = 0; i < docsToAdd; i++) { doc = new Document(); doc.add(newStringField("content", English.intToEnglish(i).trim(), Field.Store.YES)); writer.addDocument(doc); } assertEquals(docsToAdd, writer.maxDoc()); writer.close(); dir.close(); }
@Override public void setUp() throws Exception { super.setUp(); dir = newDirectory(); RandomIndexWriter iw = new RandomIndexWriter(random(), dir); int numDocs = _TestUtil.nextInt(random(), 2049, 4000); for (int i = 0; i < numDocs; i++) { Document document = new Document(); document.add(newTextField("english", English.intToEnglish(i), Field.Store.NO)); document.add(newTextField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Store.NO)); document.add(newStringField("byte", "" + ((byte) random().nextInt()), Field.Store.NO)); document.add(newStringField("short", "" + ((short) random().nextInt()), Field.Store.NO)); document.add(new IntField("int", random().nextInt(), Field.Store.NO)); document.add(new LongField("long", random().nextLong(), Field.Store.NO)); document.add(new FloatField("float", random().nextFloat(), Field.Store.NO)); document.add(new DoubleField("double", random().nextDouble(), Field.Store.NO)); document.add(new NumericDocValuesField("intdocvalues", random().nextInt())); document.add(new FloatDocValuesField("floatdocvalues", random().nextFloat())); iw.addDocument(document); } reader = iw.getReader(); iw.close(); searcher = newSearcher(reader); }
/** * Test Position increments applied by TypeTokenFilter with and without enabling this option. */ public void testStopPositons() throws IOException { StringBuilder sb = new StringBuilder(); for (int i = 10; i < 20; i++) { if (i % 3 != 0) { sb.append(i).append(" "); } else { String w = English.intToEnglish(i).trim(); sb.append(w).append(" "); } } log(sb.toString()); String stopTypes[] = new String[]{"<NUM>"}; Set<String> stopSet = asSet(stopTypes); // with increments StringReader reader = new StringReader(sb.toString()); TypeTokenFilter typeTokenFilter = new TypeTokenFilter(TEST_VERSION_CURRENT, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet); testPositons(typeTokenFilter); // without increments reader = new StringReader(sb.toString()); typeTokenFilter = new TypeTokenFilter(Version.LUCENE_43, false, new StandardTokenizer(TEST_VERSION_CURRENT, reader), stopSet); testPositons(typeTokenFilter); }