List of usage examples for org.apache.lucene.index IndexReader document
public final Document document(int docID) throws IOException
n
th Document
in this index. From source file:demo.jaxrs.search.server.Catalog.java
License:Apache License
@GET @Produces(MediaType.APPLICATION_JSON)//from w w w . j a v a 2 s . co m @CrossOriginResourceSharing(allowAllOrigins = true) @Path("/search") public Response findBook(@Context SearchContext searchContext, @Context final UriInfo uri) throws IOException { final IndexReader reader = DirectoryReader.open(directory); final IndexSearcher searcher = new IndexSearcher(reader); final JsonArrayBuilder builder = Json.createArrayBuilder(); try { visitor.reset(); visitor.visit(searchContext.getCondition(SearchBean.class)); final Query query = visitor.getQuery(); if (query != null) { final TopDocs topDocs = searcher.search(query, 1000); for (final ScoreDoc scoreDoc : topDocs.scoreDocs) { final Document document = reader.document(scoreDoc.doc); final String source = document.getField(LuceneDocumentMetadata.SOURCE_FIELD).stringValue(); builder.add(Json.createObjectBuilder().add("source", source).add("score", scoreDoc.score).add( "url", uri.getBaseUriBuilder().path(Catalog.class).path(source).build().toString())); } } return Response.ok(builder.build()).build(); } finally { reader.close(); } }
From source file:dk.dbc.opensearch.fedora.search.LuceneFieldIndex.java
License:Open Source License
public int findHighestId(String namespace) throws IOException { TermQuery luceneQuery = new TermQuery(new Term(PID_NAMESPACE, namespace)); searchManager.maybeRefreshBlocking(); IndexSearcher localSearcher = searchManager.acquire(); try {//from ww w. j a v a 2s .c o m log.debug("Query: {}", luceneQuery.toString()); TopFieldDocs search = localSearcher.search(luceneQuery, 1, new Sort(new SortField(PID_INT, SortField.Type.INT, true))); if (search.scoreDocs.length > 0) { IndexReader localReader = localSearcher.getIndexReader(); Document document = localReader.document(search.scoreDocs[0].doc); IndexableField identifer = document.getField(PID_INT); if (identifer != null) { return identifer.numericValue().intValue(); } } return 0; } finally { searchManager.release(localSearcher); } }
From source file:dk.dbc.opensearch.fedora.search.WriteAheadLogTest.java
License:Open Source License
@Test @Ignore/* w ww.jav a 2 s . c om*/ public void testSearchInUncomittedWriterReturnsMultipleDocumentsIfNotAskedToApplyDeletes() throws Exception { // This is a piece of test code that shows that duplicate search results // can be returned if the reader is not configured to flush updates. // It is not part of normal test. boolean applyAllDeletes = false; String pid = "obj:1"; Document doc = makeLuceneDocument(pid); Term pidTerm = new Term("pid", pid); writer.updateDocument(pidTerm, doc); writer.commit(); writer.updateDocument(pidTerm, doc); IndexReader reader = IndexReader.open(writer, applyAllDeletes); IndexSearcher searcher = new IndexSearcher(reader); TopDocs result = searcher.search(new TermQuery(pidTerm), 100); System.out.println("Found " + result.scoreDocs.length); for (ScoreDoc sdoc : result.scoreDocs) { Bits liveDocs = MultiFields.getLiveDocs(reader); boolean isDeleted = (liveDocs != null && !liveDocs.get(sdoc.doc)); System.out.println(sdoc); System.out.println(isDeleted); Document document = reader.document(sdoc.doc); System.out.println(document); } assertEquals(1, result.totalHits); }
From source file:dk.dbc.opensearch.fedora.search.WriteAheadLogTest.java
License:Open Source License
@Test public void testInitializeRecoversUncomittedFiles() throws Exception { File comitting = new File(folder.getRoot(), "writeaheadlog.committing"); File writeaheadlog = new File(folder.getRoot(), "writeaheadlog.log"); RandomAccessFile comittingRaf = new RandomAccessFile(comitting, "rwd"); RandomAccessFile writeaheadlogRaf = new RandomAccessFile(writeaheadlog, "rwd"); String pid1 = "obj:1"; Document doc1 = makeLuceneDocument(pid1); String pid2 = "obj:2"; Document doc2a = makeLuceneDocument(pid2, new Pair<String, String>("field", "value1")); Document doc2b = makeLuceneDocument(pid2, new Pair<String, String>("field", "value2")); String pid3 = "obj:3"; Document doc3 = makeLuceneDocument(pid3); // Given a writer with one document writer.updateDocument(WriteAheadLog.getPidTerm(pid1), doc1); writer.commit();//from w w w . j a v a 2s . co m // And a comitting file with that document deleted and a new document added WriteAheadLog.writeDocumentData(comittingRaf, pid1, null); WriteAheadLog.writeDocumentData(comittingRaf, pid2, doc2a); // And a log file with one new document and one updated document WriteAheadLog.writeDocumentData(writeaheadlogRaf, pid2, doc2b); WriteAheadLog.writeDocumentData(writeaheadlogRaf, pid3, doc3); comittingRaf.close(); writeaheadlogRaf.close(); // Initialize the WAL to recover the lost files WriteAheadLog wal = new WriteAheadLog(writer, folder.getRoot(), 1000, true); int recovered = wal.initialize(); assertEquals(4, recovered); // Verify that IndexReader reader = DirectoryReader.open(writer, false); IndexSearcher searcher = new IndexSearcher(reader); TopDocs result = searcher.search(new TermQuery(WriteAheadLog.getPidTerm(pid1)), 100); assertEquals(0, result.scoreDocs.length); System.out.println(""); result = searcher.search(new TermQuery(WriteAheadLog.getPidTerm(pid2)), 100); assertEquals(1, result.scoreDocs.length); Document doc2 = reader.document(result.scoreDocs[0].doc); Iterator<IndexableField> it1 = doc2b.iterator(); Iterator<IndexableField> it2 = doc2.iterator(); do { IndexableField expected = it1.next(); IndexableField actual = it2.next(); assertEquals(expected.fieldType().stored(), actual.fieldType().stored()); if (!(expected instanceof LongField)) { assertEquals(expected.fieldType().indexed(), actual.fieldType().indexed()); assertEquals(expected.fieldType().omitNorms(), actual.fieldType().omitNorms()); assertEquals(expected.fieldType().indexOptions(), actual.fieldType().indexOptions()); } assertEquals(expected.name(), actual.name()); assertEquals(expected.stringValue(), actual.stringValue()); assertEquals(expected.numericValue(), actual.numericValue()); } while (it1.hasNext() && it2.hasNext()); // assertEquals( doc2b.toString(), doc2.toString() ); result = searcher.search(new TermQuery(WriteAheadLog.getPidTerm(pid3)), 100); assertEquals(1, result.scoreDocs.length); }
From source file:dk.defxws.fedoragsearch.server.Config.java
License:Open Source License
private void checkConfig() throws ConfigException { if (logger.isDebugEnabled()) logger.debug("fedoragsearch.properties=" + fgsProps.toString()); // Check for unknown properties, indicating typos or wrong property names String[] propNames = { "fedoragsearch.deployFile", "fedoragsearch.soapBase", "fedoragsearch.soapUser", "fedoragsearch.soapPass", "fedoragsearch.defaultNoXslt", "fedoragsearch.defaultGfindObjectsRestXslt", "fedoragsearch.defaultUpdateIndexRestXslt", "fedoragsearch.defaultBrowseIndexRestXslt", "fedoragsearch.defaultGetRepositoryInfoRestXslt", "fedoragsearch.defaultGetIndexInfoRestXslt", "fedoragsearch.mimeTypes", "fedoragsearch.maxPageSize", "fedoragsearch.defaultBrowseIndexTermPageSize", "fedoragsearch.defaultGfindObjectsHitPageSize", "fedoragsearch.defaultGfindObjectsSnippetsMax", "fedoragsearch.defaultGfindObjectsFieldMaxLength", "fedoragsearch.repositoryNames", "fedoragsearch.indexNames", "fedoragsearch.updaterNames", "fedoragsearch.searchResultFilteringModule", "fedoragsearch.searchResultFilteringType" }; //checkPropNames("fedoragsearch.properties", fgsProps, propNames); // Check rest stylesheets checkRestStylesheet("fedoragsearch.defaultNoXslt"); checkRestStylesheet("fedoragsearch.defaultGfindObjectsRestXslt"); checkRestStylesheet("fedoragsearch.defaultUpdateIndexRestXslt"); checkRestStylesheet("fedoragsearch.defaultBrowseIndexRestXslt"); checkRestStylesheet("fedoragsearch.defaultGetRepositoryInfoRestXslt"); checkRestStylesheet("fedoragsearch.defaultGetIndexInfoRestXslt"); // Check mimeTypes checkMimeTypes("fedoragsearch", fgsProps, "fedoragsearch.mimeTypes"); // Check resultPage properties try {//from ww w .jav a 2 s . c o m maxPageSize = Integer.parseInt(fgsProps.getProperty("fedoragsearch.maxPageSize")); } catch (NumberFormatException e) { errors.append("\n*** maxPageSize is not valid:\n" + e.toString()); } try { defaultBrowseIndexTermPageSize = Integer .parseInt(fgsProps.getProperty("fedoragsearch.defaultBrowseIndexTermPageSize")); } catch (NumberFormatException e) { errors.append("\n*** defaultBrowseIndexTermPageSize is not valid:\n" + e.toString()); } try { defaultGfindObjectsHitPageSize = Integer .parseInt(fgsProps.getProperty("fedoragsearch.defaultGfindObjectsHitPageSize")); } catch (NumberFormatException e) { errors.append("\n*** defaultGfindObjectsHitPageSize is not valid:\n" + e.toString()); } try { defaultGfindObjectsSnippetsMax = Integer .parseInt(fgsProps.getProperty("fedoragsearch.defaultGfindObjectsSnippetsMax")); } catch (NumberFormatException e) { errors.append("\n*** defaultGfindObjectsSnippetsMax is not valid:\n" + e.toString()); } try { defaultGfindObjectsFieldMaxLength = Integer .parseInt(fgsProps.getProperty("fedoragsearch.defaultGfindObjectsFieldMaxLength")); } catch (NumberFormatException e) { errors.append("\n*** defaultGfindObjectsFieldMaxLength is not valid:\n" + e.toString()); } // Check updater properties String updaterProperty = fgsProps.getProperty("fedoragsearch.updaterNames"); if (updaterProperty == null) { updaterNameToProps = null; // No updaters will be created } else { updaterNameToProps = new Hashtable(); StringTokenizer updaterNames = new StringTokenizer(updaterProperty); while (updaterNames.hasMoreTokens()) { String updaterName = updaterNames.nextToken(); try { InputStream propStream = null; try { propStream = getResourceInputStream("/updater/" + updaterName + "/updater.properties"); } catch (ConfigException e) { errors.append("\n" + e.getMessage()); } Properties props = new Properties(); props.load(propStream); propStream.close(); //MIH convertProperties(props); if (logger.isInfoEnabled()) { logger.info( configName + "/updater/" + updaterName + "/updater.properties=" + props.toString()); } // Check properties String propsNamingFactory = props.getProperty("java.naming.factory.initial"); String propsProviderUrl = props.getProperty("java.naming.provider.url"); String propsConnFactory = props.getProperty("connection.factory.name"); String propsClientId = props.getProperty("client.id"); if (propsNamingFactory == null) { errors.append("\n*** java.naming.factory.initial not provided in " + configName + "/updater/" + updaterName + "/updater.properties"); } if (propsProviderUrl == null) { errors.append("\n*** java.naming.provider.url not provided in " + configName + "/updater/" + updaterName + "/updater.properties"); } if (propsConnFactory == null) { errors.append("\n*** connection.factory.name not provided in " + configName + "/updater/" + updaterName + "/updater.properties"); } if (propsClientId == null) { errors.append("\n*** client.id not provided in " + configName + "/updater/" + updaterName + "/updater.properties"); } updaterNameToProps.put(updaterName, props); } catch (IOException e) { errors.append("\n*** Error loading " + configName + "/updater/" + updaterName + ".properties:\n" + e.toString()); } } } // Check searchResultFilteringModule property searchResultFilteringModuleProperty = fgsProps.getProperty("fedoragsearch.searchResultFilteringModule"); if (searchResultFilteringModuleProperty != null && searchResultFilteringModuleProperty.length() > 0) { try { getSearchResultFiltering(); } catch (ConfigException e) { errors.append(e.getMessage()); } String searchResultFilteringTypeProperty = fgsProps .getProperty("fedoragsearch.searchResultFilteringType"); StringTokenizer srft = new StringTokenizer(""); if (searchResultFilteringTypeProperty != null) { srft = new StringTokenizer(searchResultFilteringTypeProperty); } int countTokens = srft.countTokens(); if (searchResultFilteringTypeProperty == null || countTokens == 0 || countTokens > 1) { errors.append("\n*** " + configName + ": fedoragsearch.searchResultFilteringType=" + searchResultFilteringTypeProperty + ": one and only one of 'presearch', 'insearch', 'postsearch' must be stated.\n"); } else { for (int i = 0; i < countTokens; i++) { String token = srft.nextToken(); if (!("presearch".equals(token) || "insearch".equals(token) || "postsearch".equals(token))) { errors.append("\n*** " + configName + ": fedoragsearch.searchResultFilteringType=" + searchResultFilteringTypeProperty + ": only 'presearch', 'insearch', 'postsearch' may be stated, not '" + token + "'.\n"); } } } } // Check repository properties Enumeration repositoryNames = repositoryNameToProps.keys(); while (repositoryNames.hasMoreElements()) { String repositoryName = (String) repositoryNames.nextElement(); Properties props = (Properties) repositoryNameToProps.get(repositoryName); if (logger.isDebugEnabled()) logger.debug(configName + "/repository/" + repositoryName + "/repository.properties=" + props.toString()); // Check for unknown properties, indicating typos or wrong property names String[] reposPropNames = { "fgsrepository.repositoryName", "fgsrepository.fedoraSoap", "fgsrepository.fedoraUser", "fgsrepository.fedoraPass", "fgsrepository.fedoraObjectDir", "fgsrepository.fedoraVersion", "fgsrepository.defaultGetRepositoryInfoResultXslt", "fgsrepository.trustStorePath", "fgsrepository.trustStorePass" }; //checkPropNames(configName+"/repository/"+repositoryName+"/repository.properties", props, reposPropNames); // Check repositoryName String propsRepositoryName = props.getProperty("fgsrepository.repositoryName"); if (!repositoryName.equals(propsRepositoryName)) { errors.append("\n*** " + configName + "/repository/" + repositoryName + ": fgsrepository.repositoryName must be=" + repositoryName); } // Check fedoraObjectDir // String fedoraObjectDirName = insertSystemProperties(props.getProperty("fgsrepository.fedoraObjectDir")); // File fedoraObjectDir = new File(fedoraObjectDirName); // if (fedoraObjectDir == null) { // errors.append("\n*** "+configName+"/repository/" + repositoryName // + ": fgsrepository.fedoraObjectDir=" // + fedoraObjectDirName + " not found"); // } // Check result stylesheets checkResultStylesheet("/repository/" + repositoryName, props, "fgsrepository.defaultGetRepositoryInfoResultXslt"); } // Check index properties Enumeration indexNames = indexNameToProps.keys(); while (indexNames.hasMoreElements()) { String indexName = (String) indexNames.nextElement(); Properties props = (Properties) indexNameToProps.get(indexName); if (logger.isDebugEnabled()) logger.debug(configName + "/index/" + indexName + "/index.properties=" + props.toString()); // Check for unknown properties, indicating typos or wrong property names String[] indexPropNames = { "fgsindex.indexName", "fgsindex.indexBase", "fgsindex.indexUser", "fgsindex.indexPass", "fgsindex.operationsImpl", "fgsindex.defaultUpdateIndexDocXslt", "fgsindex.defaultUpdateIndexResultXslt", "fgsindex.defaultGfindObjectsResultXslt", "fgsindex.defaultBrowseIndexResultXslt", "fgsindex.defaultGetIndexInfoResultXslt", "fgsindex.indexDir", "fgsindex.analyzer", "fgsindex.untokenizedFields", "fgsindex.defaultQueryFields", "fgsindex.snippetBegin", "fgsindex.snippetEnd", "fgsindex.maxBufferedDocs", "fgsindex.mergeFactor", "fgsindex.ramBufferSizeMb", "fgsindex.defaultWriteLockTimeout", "fgsindex.defaultSortFields", "fgsindex.uriResolver" }; //checkPropNames(configName+"/index/"+indexName+"/index.properties", props, indexPropNames); // Check indexName String propsIndexName = props.getProperty("fgsindex.indexName"); if (!indexName.equals(propsIndexName)) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.indexName must be=" + indexName); } // Check operationsImpl class String operationsImpl = props.getProperty("fgsindex.operationsImpl"); if (operationsImpl == null || operationsImpl.equals("")) { errors.append( "\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl must be set in " + configName + "/index/ " + indexName + ".properties"); } try { Class operationsImplClass = Class.forName(operationsImpl); try { GenericOperationsImpl ops = (GenericOperationsImpl) operationsImplClass .getConstructor(new Class[] {}).newInstance(new Object[] {}); } catch (InstantiationException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl=" + operationsImpl + ": instantiation error.\n" + e.toString()); } catch (IllegalAccessException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl=" + operationsImpl + ": instantiation error.\n" + e.toString()); } catch (InvocationTargetException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl=" + operationsImpl + ": instantiation error.\n" + e.toString()); } catch (NoSuchMethodException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl=" + operationsImpl + ": instantiation error.\n" + e.toString()); } } catch (ClassNotFoundException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.operationsImpl=" + operationsImpl + ": class not found.\n" + e); } // Check result stylesheets checkResultStylesheet("/index/" + indexName, props, "fgsindex.defaultUpdateIndexDocXslt"); checkResultStylesheet("/index/" + indexName, props, "fgsindex.defaultUpdateIndexResultXslt"); checkResultStylesheet("/index/" + indexName, props, "fgsindex.defaultGfindObjectsResultXslt"); checkResultStylesheet("/index/" + indexName, props, "fgsindex.defaultBrowseIndexResultXslt"); checkResultStylesheet("/index/" + indexName, props, "fgsindex.defaultGetIndexInfoResultXslt"); // Check indexDir String indexDir = insertSystemProperties(props.getProperty("fgsindex.indexDir")); File indexDirFile = new File(indexDir); if (indexDirFile == null) { errors.append("\n*** " + configName + "/index/" + indexName + " fgsindex.indexDir=" + indexDir + " must exist as a directory"); } // Check analyzer class for lucene and solr if (operationsImpl.indexOf("fgslucene") > -1 || operationsImpl.indexOf("fgssolr") > -1) { String analyzer = props.getProperty("fgsindex.analyzer"); if (analyzer == null || analyzer.equals("")) { analyzer = defaultAnalyzer; } try { Class analyzerClass = Class.forName(analyzer); try { Analyzer a = (Analyzer) analyzerClass.getConstructor(new Class[] {}) .newInstance(new Object[] {}); } catch (InstantiationException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + analyzer + ": fgsindex.analyzer=" + analyzer + ": instantiation error.\n" + e.toString()); } catch (IllegalAccessException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + analyzer + ": fgsindex.analyzer=" + analyzer + ": instantiation error.\n" + e.toString()); } catch (InvocationTargetException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + analyzer + ": fgsindex.analyzer=" + analyzer + ": instantiation error.\n" + e.toString()); } catch (NoSuchMethodException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + analyzer + ": fgsindex.analyzer=" + analyzer + ": instantiation error:\n" + e.toString()); } } catch (ClassNotFoundException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.analyzer=" + analyzer + ": class not found:\n" + e.toString()); } } // Add untokenizedFields property for lucene if (operationsImpl.indexOf("fgslucene") > -1) { String defaultUntokenizedFields = props.getProperty("fgsindex.untokenizedFields"); if (defaultUntokenizedFields == null) props.setProperty("fgsindex.untokenizedFields", ""); if (indexDirFile != null) { StringBuffer untokenizedFields = new StringBuffer( props.getProperty("fgsindex.untokenizedFields")); IndexReader ir = null; try { ir = IndexReader.open(FSDirectory.open(new File(indexDir)), true); int max = ir.numDocs(); if (max > 10) max = 10; for (int i = 0; i < max; i++) { Document doc = ir.document(i); for (ListIterator li = doc.getFields().listIterator(); li.hasNext();) { Field f = (Field) li.next(); if (!f.isTokenized() && f.isIndexed() && untokenizedFields.indexOf(f.name()) < 0) { untokenizedFields.append(" " + f.name()); } } } } catch (Exception e) { } props.setProperty("fgsindex.untokenizedFields", untokenizedFields.toString()); if (logger.isDebugEnabled()) logger.debug("indexName=" + indexName + " fgsindex.untokenizedFields=" + untokenizedFields); } } // Check defaultQueryFields - how can we check this? String defaultQueryFields = props.getProperty("fgsindex.defaultQueryFields"); // Use custom URIResolver if given //MIH: also check for solr if (operationsImpl.indexOf("fgslucene") > -1 || operationsImpl.indexOf("fgssolr") > -1) { Class uriResolverClass = null; String uriResolver = props.getProperty("fgsindex.uriResolver"); if (!(uriResolver == null || uriResolver.equals(""))) { try { uriResolverClass = Class.forName(uriResolver); try { URIResolverImpl ur = (URIResolverImpl) uriResolverClass.getConstructor(new Class[] {}) .newInstance(new Object[] {}); if (ur != null) { ur.setConfig(this); indexNameToUriResolvers.put(indexName, ur); } } catch (InstantiationException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + uriResolver + ": fgsindex.uriResolver=" + uriResolver + ": instantiation error.\n" + e.toString()); } catch (IllegalAccessException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + uriResolver + ": fgsindex.uriResolver=" + uriResolver + ": instantiation error.\n" + e.toString()); } catch (InvocationTargetException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + uriResolver + ": fgsindex.uriResolver=" + uriResolver + ": instantiation error.\n" + e.toString()); } catch (NoSuchMethodException e) { errors.append("\n*** " + configName + "/index/" + indexName + " " + uriResolver + ": fgsindex.uriResolver=" + uriResolver + ": instantiation error:\n" + e.toString()); } } catch (ClassNotFoundException e) { errors.append("\n*** " + configName + "/index/" + indexName + ": fgsindex.uriResolver=" + uriResolver + ": class not found:\n" + e.toString()); } } } } if (logger.isDebugEnabled()) logger.debug("configCheck configName=" + configName + " errors=" + errors.toString()); if (errors.length() > 0) throw new ConfigException(errors.toString()); }
From source file:dk.statsbiblioteket.netark.dvenabler.DVReaderTest.java
License:Apache License
public void testLargerDVEnableIndex() throws IOException { final int DOCS = 1000; log.info("testLargerDVEnableIndex started"); final File INDEX_SRC = generateIndex(DOCS); final File INDEX_DEST = new File("target/testindex.deletefreely.dest"); try {//w ww.j a v a 2s . co m IndexUtils.convert(INDEX_SRC, INDEX_DEST, createDVFieldDescriptions(INDEX_SRC)); IndexReader readerSrc = DirectoryReader.open(MMapDirectory.open(INDEX_SRC)); IndexReader readerDest = DirectoryReader.open(MMapDirectory.open(INDEX_DEST)); long multiCount = 0; long singleCount = 0; long longCount = 0; long doubleCount = 0; for (int docID = 0; docID < DOCS; docID++) { { String[] multisSrc = readerSrc.document(docID).getValues(MULTI); if (multisSrc != null) { List<String> dvs = getSortedSetDocValues(readerDest, docID, MULTI); Arrays.sort(multisSrc); Collections.sort(dvs); assertEquals("There should be as many DV as stored for field " + MULTI, multisSrc.length, dvs.size()); for (int i = 0; i < multisSrc.length; i++) { assertEquals("Value " + i + " for field " + MULTI + " should be equal", multisSrc[i], dvs.get(i)); multiCount++; } } } { String singleSrc = readerSrc.document(docID).get(SINGLE); if (singleSrc != null) { String dv = getSortedDocValue(readerDest, docID, SINGLE); assertEquals("The DV for field " + SINGLE + " should match the stored value", singleSrc, dv); singleCount++; } } { IndexableField fieldSrc = readerSrc.document(docID).getField(LONG); if (fieldSrc != null) { long longSrc = fieldSrc.numericValue().longValue(); long dv = getLongDocValue(readerDest, docID, LONG); assertEquals("The DV for field " + LONG + " should match the stored value", longSrc, dv); longCount++; } } { IndexableField fieldSrc = readerSrc.document(docID).getField(DOUBLE); if (fieldSrc != null) { double doubleSrc = fieldSrc.numericValue().doubleValue(); double dv = getDoubleDocValue(readerDest, docID, DOUBLE); assertEquals("The DV for field " + DOUBLE + " should match the stored value", doubleSrc, dv); doubleCount++; } } } assertTrue("There should be at least 1 value for field " + MULTI + " in a document", multiCount > 0); assertTrue("There should be at least 1 value for field " + SINGLE + " in a document", singleCount > 0); assertTrue("There should be at least 1 value for field " + LONG + " in a document", longCount > 0); assertTrue("There should be at least 1 value for field " + DOUBLE + " in a document", doubleCount > 0); readerSrc.close(); readerDest.close(); } finally { delete(INDEX_SRC); delete(INDEX_DEST); } }
From source file:dk.statsbiblioteket.netark.dvenabler.DVReaderTest.java
License:Apache License
private static void assertIndexValues(IndexReader reader, IndexSearcher searcher, boolean dvExpected) throws ParseException, IOException { final String M = "dvExpected=" + dvExpected + ". "; Analyzer analyzer = new StandardAnalyzer(LUCENE_VERSION); QueryParser queryParser = new QueryParser(LUCENE_VERSION, SEARCH, analyzer); Query query = queryParser.parse(SEARCH_CONTENT); TopDocs topDocs = searcher.search(query, 10); assertEquals(M + "Search for 'somecontent' should give the right number of results", 1, topDocs.totalHits); Document doc = reader.document(topDocs.scoreDocs[0].doc); assertEquals(M + "The stored value for the document should be correct", SINGLE_CONTENT, doc.get(SINGLE)); assertEquals(M + "The stored long value for the document should be correct", Long.toString(LONG_CONTENT), doc.get(LONG));// w ww . j a va2 s. c o m for (String value : Arrays.asList(MULTI_CONTENT_1, MULTI_CONTENT_2)) { assertTrue("The value " + value + " should be stored in field " + MULTI, Arrays.asList(doc.getValues(MULTI)).contains(value)); } /* assertEquals(M + "The stored double value for the document should be correct", Double.toString(DOUBLE_CONTENT), doc.get(DOUBLE)); assertEquals(M + "The stored float value for the document should be correct", Double.toString(FLOAT_CONTENT), doc.get(FLOAT));*/ String dv = getSortedDocValue(reader, topDocs.scoreDocs[0].doc, DV); assertEquals("The plain DocValues content for the document should be correct", DV_CONTENT, dv); // SINGLE (single value String) try { String nonexistingDV = getSortedDocValue(reader, topDocs.scoreDocs[0].doc, SINGLE); if (!dvExpected) { fail(M + "Requesting the DocValue from the non-DV field " + SINGLE + " should have failed but returned " + nonexistingDV); } assertEquals("Requesting DV from a stored field should work due to the wrapper", SINGLE_CONTENT, nonexistingDV); } catch (Exception e) { if (dvExpected) { fail(M + "There should have been a DV-value for field " + SINGLE); } } // LONG try { long nonexistingDV = getLongDocValue(reader, topDocs.scoreDocs[0].doc, LONG); if (!dvExpected) { fail(M + "Requesting the DocValue from the non-DV field " + LONG + " should have failed but returned " + nonexistingDV); } assertEquals("Requesting DV from a stored field should work due to the wrapper", LONG_CONTENT, nonexistingDV); } catch (Exception e) { if (dvExpected) { fail(M + "There should have been a DV-value for field " + LONG); } } // MULTI (multi value String) try { List<String> dvs = getSortedSetDocValues(reader, topDocs.scoreDocs[0].doc, MULTI); if (!dvExpected) { fail(M + "Requesting the SortedSet DocValues from the non-DV field " + SINGLE + " should have failed but returned " + dvs); } assertEquals("The number of returned DVs for field " + MULTI + " should match", 2, dvs.size()); for (String value : Arrays.asList(MULTI_CONTENT_1, MULTI_CONTENT_2)) { assertTrue("The value " + value + " should be DocValued in field " + MULTI, dvs.contains(value)); } } catch (Exception e) { if (dvExpected) { fail(M + "There should have been a DV-value for field " + MULTI); } } }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
public static List<String> getAllFilePaths(final CaseFacade caseFacade) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); if (!relativePath.isEmpty()) { String fullpath = caseFacade.getFullPath(relativePath); files.add(fullpath); }/* ww w . java 2 s . com*/ } } } } indexReader.close(); return files; }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
private static List<String> getAllFilePathsHaveAuther(final CaseFacade caseFacade, final List<String> authers) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); String auther = document.get("Author"); if (!relativePath.isEmpty() && auther != null && !auther.trim().isEmpty() && Utilities.isFound(authers, auther)) { String fullpath = caseFacade.getFullPath(relativePath); files.add(fullpath); }//from w w w.j a va2 s . c o m } } } } indexReader.close(); return files; }
From source file:edu.coeia.reports.IndexUtil.java
License:Open Source License
public static List<String> getAllAuthers(final CaseFacade caseFacade) throws IOException { List<String> files = new ArrayList<String>(); String indexDir = caseFacade.getCaseIndexFolderLocation(); Directory dir = FSDirectory.open(new File(indexDir)); IndexReader indexReader = IndexReader.open(dir); for (int i = 0; i < indexReader.maxDoc(); i++) { Document document = indexReader.document(i); if (document != null) { Field field = document.getField(IndexingConstant.DOCUMENT_TYPE); if (field != null && field.stringValue() != null) { String path = field.stringValue(); if (path.equals(IndexingConstant .fromDocumentTypeToString(IndexingConstant.DOCUMENT_GENERAL_TYPE.FILE))) { String relativePath = document.get(IndexingConstant.FILE_PATH); String auther = document.get("Author"); if (!relativePath.isEmpty() && auther != null && !auther.trim().isEmpty()) { files.add(auther); }/*from w w w . j a v a2 s. c om*/ } } } } indexReader.close(); return files; }