cmssearchindex.java
来自「找了很久才找到到源代码」· Java 代码 · 共 1,124 行 · 第 1/3 页
JAVA
1,124 行
result.put(EXCERPT, Boolean.valueOf(m_createExcerpt));
}
if (m_dontCheckPermissions) {
result.put(PERMISSIONS, Boolean.valueOf(!m_dontCheckPermissions));
}
return result;
}
/**
* Returns the document type factory used for the given resource in this index, or <code>null</code>
* in case the resource is not indexed by this index.<p>
*
* A resource is indexed if the following is all true: <ol>
* <li>The index contains at last one index source matching the root path of the given resource.
* <li>For this matching index source, the document type factory needed by the resource is also configured.
* </ol>
*
* @param res the resource to check
*
* @return he document type factory used for the given resource in this index, or <code>null</code>
* in case the resource is not indexed by this index
*/
public I_CmsDocumentFactory getDocumentFactory(CmsResource res) {
if ((res != null) && (m_sources != null)) {
// the result can only be null or the type configured for the resource
I_CmsDocumentFactory result = OpenCms.getSearchManager().getDocumentFactory(res);
if (result != null) {
// check the path of the resource if it matches with one (or more) of the configured index sources
Iterator i = m_sources.iterator();
while (i.hasNext()) {
CmsSearchIndexSource source = (CmsSearchIndexSource)i.next();
if (source.isIndexing(res.getRootPath(), result.getName())) {
// we found an index source that indexes the resource
return result;
}
}
}
}
return null;
}
/**
* Returns a list of names (Strings) of configured document type factorys for the given resource path.<p>
*
* @param path path of the folder
*
* @return a list of names (Strings) of configured document type factorys for the given resource path
*
* @deprecated use {@link #getDocumentFactory(CmsResource)} instead to find out if this index is 'interested' in a resource
*/
public List getDocumenttypes(String path) {
List documenttypes = null;
if (m_documenttypes != null) {
for (Iterator i = m_documenttypes.entrySet().iterator(); i.hasNext();) {
Map.Entry e = (Map.Entry)i.next();
String key = (String)e.getKey();
// NOTE: assumed that configured resource paths do not overlap, otherwise result is undefined
if (path.startsWith(key)) {
documenttypes = (List)e.getValue();
break;
}
}
}
if (documenttypes == null) {
documenttypes = OpenCms.getSearchManager().getDocumentTypes();
}
return documenttypes;
}
/**
* Returns the search field configuration of this index.<p>
*
* @return the search field configuration of this index
*/
public CmsSearchFieldConfiguration getFieldConfiguration() {
return m_fieldConfiguration;
}
/**
* Returns the name of the field configuration used for this index.<p>
*
* @return the name of the field configuration used for this index
*/
public String getFieldConfigurationName() {
return m_fieldConfigurationName;
}
/**
* Returns a new index writer for this index.<p>
*
* @param create if <code>true</code> a whole new index is created, if <code>false</code> an existing index is updated
*
* @return a new instance of IndexWriter
* @throws CmsIndexException if the index can not be opened
*/
public IndexWriter getIndexWriter(boolean create) throws CmsIndexException {
IndexWriter indexWriter;
Analyzer analyzer = OpenCms.getSearchManager().getAnalyzer(m_locale);
try {
File f = new File(m_path);
if (f.exists()) {
// index already exists
indexWriter = new IndexWriter(m_path, analyzer, create);
} else {
// index does not exist yet
f = f.getParentFile();
if ((f != null) && !f.exists()) {
// create the parent folders if required
f.mkdirs();
}
indexWriter = new IndexWriter(m_path, analyzer, true);
}
} catch (Exception e) {
throw new CmsIndexException(
Messages.get().container(Messages.ERR_IO_INDEX_WRITER_OPEN_2, m_path, m_name),
e);
}
return indexWriter;
}
/**
* Gets the langauge of this index.<p>
*
* @return the language of the index, i.e. de
*/
public Locale getLocale() {
return m_locale;
}
/**
* Returns the locale of the index as a String.<p>
*
* @return the locale of the index as a String
*
* @see #getLocale()
*/
public String getLocaleString() {
return getLocale().toString();
}
/**
* Gets the name of this index.<p>
*
* @return the name of the index
*/
public String getName() {
return m_name;
}
/**
* Returns the path where this index stores it's data in the "real" file system.<p>
*
* @return the path where this index stores it's data in the "real" file system
*/
public String getPath() {
return m_path;
}
/**
* Gets the project of this index.<p>
*
* @return the project of the index, i.e. "online"
*/
public String getProject() {
return m_project;
}
/**
* Get the rebuild mode of this index.<p>
*
* @return the current rebuild mode
*/
public String getRebuildMode() {
return m_rebuild;
}
/**
* Returns all configured sources names of this search index.<p>
*
* @return a list with all configured sources names of this search index
*/
public List getSourceNames() {
return m_sourceNames;
}
/**
* Returns all configured index sources of this search index.<p>
*
* @return all configured index sources of this search index
*/
public List getSources() {
return m_sources;
}
/**
* @see java.lang.Object#hashCode()
*/
public int hashCode() {
return m_name != null ? m_name.hashCode() : 0;
}
/**
* @see org.opencms.configuration.I_CmsConfigurationParameterHandler#initConfiguration()
*/
public void initConfiguration() {
// noting to do here
}
/**
* Initializes the search index.<p>
*
* @throws CmsSearchException if the index source association failed
*/
public void initialize() throws CmsSearchException {
if (!isEnabled()) {
// index is disabled, no initialization is required
return;
}
String sourceName = null;
CmsSearchIndexSource indexSource = null;
List searchIndexSourceDocumentTypes = null;
List resourceNames = null;
String resourceName = null;
m_sources = new ArrayList();
m_path = OpenCms.getSystemInfo().getAbsoluteRfsPathRelativeToWebInf(
OpenCms.getSearchManager().getDirectory() + "/" + m_name);
for (int i = 0, n = m_sourceNames.size(); i < n; i++) {
try {
sourceName = (String)m_sourceNames.get(i);
indexSource = OpenCms.getSearchManager().getIndexSource(sourceName);
m_sources.add(indexSource);
resourceNames = indexSource.getResourcesNames();
searchIndexSourceDocumentTypes = indexSource.getDocumentTypes();
for (int j = 0, m = resourceNames.size(); j < m; j++) {
resourceName = (String)resourceNames.get(j);
m_documenttypes.put(resourceName, searchIndexSourceDocumentTypes);
}
} catch (Exception e) {
// mark this index as disabled
setEnabled(false);
throw new CmsSearchException(Messages.get().container(
Messages.ERR_INDEX_SOURCE_ASSOCIATION_1,
sourceName), e);
}
}
// initialize the search field configuration
if (m_fieldConfigurationName == null) {
// if not set, use standard field configuration
m_fieldConfigurationName = CmsSearchFieldConfiguration.STR_STANDARD;
}
m_fieldConfiguration = OpenCms.getSearchManager().getFieldConfiguration(m_fieldConfigurationName);
if (m_fieldConfiguration == null) {
// we must have a valid field configuration to continue
throw new CmsSearchException(Messages.get().container(
Messages.ERR_FIELD_CONFIGURATION_UNKNOWN_2,
m_name,
m_fieldConfigurationName));
}
}
/**
* Returns <code>true</code> if this index is currently disabled.<p>
*
* @return <code>true</code> if this index is currently disabled
*/
public boolean isEnabled() {
return m_enabled;
}
/**
* Removes an index source from this search index.<p>
*
* @param sourceName the index source name to remove
*/
public void removeSourceName(String sourceName) {
m_sourceNames.remove(sourceName);
}
/**
* Performs a search on the index within the given fields.<p>
*
* The result is returned as List with entries of type I_CmsSearchResult.<p>
* @param cms the current user's Cms object
* @param params the parameters to use for the search
* @return the List of results found or an empty list
* @throws CmsSearchException if something goes wrong
*/
public synchronized CmsSearchResultList search(CmsObject cms, CmsSearchParameters params) throws CmsSearchException {
long timeTotal = -System.currentTimeMillis();
long timeLucene;
long timeResultProcessing;
if (LOG.isDebugEnabled()) {
LOG.debug(Messages.get().getBundle().key(Messages.LOG_SEARCH_PARAMS_2, params, m_name));
}
CmsRequestContext context = cms.getRequestContext();
CmsProject currentProject = context.currentProject();
// the searcher to perform the operation in
IndexSearcher searcher = null;
// the hits found during the search
Hits hits;
// storage for the results found
CmsSearchResultList searchResults = new CmsSearchResultList();
int previousPriority = Thread.currentThread().getPriority();
try {
if (m_priority > 0) {
// change thread priority in order to reduce search impact on overall system performance
Thread.currentThread().setPriority(m_priority);
}
// change the project
context.setCurrentProject(cms.readProject(m_project));
// complete the search root
String[] roots;
if ((params.getRoots() != null) && (params.getRoots().size() > 0)) {
// add the site root to all the search root
roots = new String[params.getRoots().size()];
for (int i = 0; i < params.getRoots().size(); i++) {
roots[i] = cms.getRequestContext().addSiteRoot((String)params.getRoots().get(i));
}
} else {
// just use the site root as the search root
// this permits searching in indexes that contain content of other sites than the current selected one?!?!
roots = new String[] {cms.getRequestContext().getSiteRoot()};
}
timeLucene = -System.currentTimeMillis();
// the language analyzer to use for creating the queries
Analyzer languageAnalyzer = OpenCms.getSearchManager().getAnalyzer(m_locale);
// the main query to use, will be constructed in the next lines
BooleanQuery query = new BooleanQuery();
// implementation note:
// initially this was a simple PrefixQuery based on the DOC_PATH
// however, internally Lucene rewrote that to literally hundreds of BooleanQuery parts
// the following implementation will lead to just one Lucene PhraseQuery per directory and is thus much better
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?