constellio——基于solr的开源搜索引擎系统源码研究(二)
2013-02-17 02:42
525 查看
因为constellio系统的类比较多,先从搜索引擎最本质的功能模块索引与检索入手,看constellio系统实现索引与检索功能的源码
首先是获取solr上下文,下面是SolrCoreContext.java源码
下面是类路径工具类ClasspathUtils.java
首先是获取solr上下文,下面是SolrCoreContext.java源码
/** * Constellio, Open Source Enterprise Search * Copyright (C) 2010 DocuLibre inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package com.doculibre.constellio.solr.context; import java.io.File; import java.io.IOException; import java.net.MalformedURLException; import java.util.HashMap; import java.util.Map; import javax.xml.parsers.ParserConfigurationException; import org.apache.solr.client.solrj.SolrServer; import org.apache.solr.client.solrj.embedded.EmbeddedSolrServer; import org.apache.solr.client.solrj.impl.CommonsHttpSolrServer; import org.apache.solr.core.CoreContainer; import org.apache.solr.core.SolrCore; import org.xml.sax.SAXException; import com.doculibre.constellio.entities.RecordCollection; import com.doculibre.constellio.opensearch.OpenSearchSolrServer; import com.doculibre.constellio.services.RecordCollectionServices; import com.doculibre.constellio.utils.ClasspathUtils; import com.doculibre.constellio.utils.ConstellioSpringUtils; public class SolrCoreContext { private static File solrCoresRootDir; private static CoreContainer cores = new CoreContainer(); public static final String DEFAULT_COLLECTION_NAME = "constellio_default"; private static Map<String, SolrServer> coreServers = new HashMap<String, SolrServer>(); public static synchronized void init() { if (solrCoresRootDir == null) { solrCoresRootDir = ClasspathUtils.getCollectionsRootDir(); } init(solrCoresRootDir); } public static synchronized void init(File solrCoresRootDir) { SolrCoreContext.solrCoresRootDir = solrCoresRootDir; try { File solrXml = new File(solrCoresRootDir, "solr.xml"); cores.load(solrCoresRootDir.getPath(), solrXml); for (String coreName : cores.getCoreNames()) { // Default setEmbeddedSolrServer(coreName); } } catch (ParserConfigurationException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (SAXException e) { throw new RuntimeException(e); } } public static synchronized void shutdown() { if (cores != null) { cores.shutdown(); } } public static synchronized void clear() { solrCoresRootDir = null; cores = new CoreContainer(); coreServers = new HashMap<String, SolrServer>(); } public static File getSolrCoresRootDir() { return solrCoresRootDir; } public static File getSolrCoreRootDir(String coreName) { String coreDirName; if (coreName != null) { coreDirName = coreName; } else { coreDirName = DEFAULT_COLLECTION_NAME; } return new File(getSolrCoresRootDir(), coreDirName); } public static File getSolrCoreIndexDir(String coreName) { File solrCoreRootDir = getSolrCoreRootDir(coreName); return new File(solrCoreRootDir, "data" + File.separator + "index"); } public static CoreContainer getCores() { return cores; } public static SolrServer getSolrServer(String coreName) { RecordCollectionServices collectionServices = ConstellioSpringUtils.getRecordCollectionServices(); RecordCollection collection = collectionServices.get(coreName); if (collection != null && collection.isOpenSearch()) { return new OpenSearchSolrServer(); } else { return coreServers.get(coreName); } } //Pour tests sans collection public static SolrServer getSolrServerUtil(String coreName) { return coreServers.get(coreName); } public static void registerSolrServer(String coreName, SolrServer solrServer) { coreServers.put(coreName, solrServer); } public static void setEmbeddedSolrServer(String coreName) { registerSolrServer(coreName, new EmbeddedSolrServer(cores, coreName)); } public static void setHttpSolrServer(String coreName, String url) { try { registerSolrServer(coreName, new CommonsHttpSolrServer(url)); } catch (MalformedURLException e) { throw new RuntimeException(e); } } public static void removeCore(String coreName) { SolrCore core = cores.getCore(coreName); if (core != null) { core.closeSearcher(); core.close(); } cores.remove(coreName); coreServers.remove(coreName); } }
下面是类路径工具类ClasspathUtils.java
/** * Constellio, Open Source Enterprise Search * Copyright (C) 2010 DocuLibre inc. * * This copyrighted material is made available to anyone wishing to use, modify, * copy, or redistribute it subject to the terms and conditions of the GNU * Lesser General Public License, as published by the Free Software Foundation. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License * for more details. * * You should have received a copy of the GNU Lesser General Public License * along with this distribution; if not, write to: * Free Software Foundation, Inc. * 51 Franklin Street, Fifth Floor * Boston, MA 02110-1301 USA */ package com.doculibre.constellio.utils; import java.io.File; import java.net.URISyntaxException; import java.net.URL; import com.doculibre.constellio.entities.RecordCollection; public class ClasspathUtils { public static File getClassesDir() { URL classesOrBinDirURL = ClasspathUtils.class.getClassLoader().getResource(""); File classesOrBinDir; try { classesOrBinDir = new File(classesOrBinDirURL.toURI()); } catch (URISyntaxException e) { throw new RuntimeException(e); } return classesOrBinDir; } public static File getWebinfDir() { File webinfDir; File classesOrBinDir = getClassesDir(); if (classesOrBinDir.getName().equals("bin")) { File projectDir = classesOrBinDir.getParentFile(); File webContentDir = new File(projectDir, "WebContent"); webinfDir = new File(webContentDir, "WEB-INF"); } else { webinfDir = classesOrBinDir.getParentFile(); } return webinfDir; } /** * For a web application, it's the web-inf dir. For tests, it's a temporary folder. * We don't want unit test to modify contellio project files */ public static File getWorkDir() { File workDir; File classesOrBinDir = getClassesDir(); if (classesOrBinDir.getName().equals("bin")) { File webInfDir = getWebinfDir(); workDir = new File(classesOrBinDir, "temp"); if (!workDir.exists()) { workDir.mkdir(); } //default Solr core File constellioDir = new File(workDir, "constellio"); if (!constellioDir.exists()) { FileUtils.copyDirectory(new File(webInfDir, "constellio"), constellioDir); } workDir.deleteOnExit(); } else { workDir = getWebinfDir(); } return workDir; } public static File getCollectionsRootDir() { File webinfDir = getWorkDir(); File constellioDir = new File(webinfDir, "constellio"); File collectionsDir = new File(constellioDir, "collections"); return collectionsDir; } public static File getCollectionRootDir(RecordCollection collection) { File collectionsDir = getCollectionsRootDir(); File collectionDir = new File(collectionsDir, collection.getName()); return collectionDir; } public static File getSynonymsFile(RecordCollection collection) { File collectionDir = getCollectionRootDir(collection); File synonymsFile = new File(collectionDir, "conf" + File.separator + "synonyms.txt"); return synonymsFile; } public static void main(String[] args) { System.out.println(getClassesDir()); } }
相关文章推荐
- constellio——基于solr的开源搜索引擎系统源码研究(四)
- constellio——基于solr的开源搜索引擎系统源码研究(五)
- constellio——基于solr的开源搜索引擎系统源码研究(一)
- constellio——基于solr的开源搜索引擎系统源码研究(三)
- 一个基于.NET平台的自动化/压力测试系统设计简述(可独立运行,提供源码)
- 基于storm的实时GPS数据客流特征分析系统 源码分析之(一):GPSReceiverSpout
- 基于用户点击行为的数字图书搜索系统研究与实现 - 中国优秀硕士学位论文全文数据库
- 基于CC2530的zIgbee传感器无线数据采集系统开发(部分关键源码)
- 第一章 绪论 --基于mkCDrec的核心网网络运维系统的备份和恢复的研究与实现
- 基于springMVC+springSecurity3.x+Mybaits3.x的权限系统,,开放源码,支持开源
- 基于RF5框架的视频处理系统研究
- 基于UML的MES系统建模方法的研究
- [转载]基于数据挖掘技术入侵检测系统研究
- 基于storm和hadoop的广告系统研究【1】
- 基于B/S+C/S模式的期刊网络采编系统研究及其应用
- “基于关键字匹配的文本过滤系统”配置文件的设计和实现(C/C++源码)
- TD-SCDMA系统基于网元仿真技术的OMC功能测试研究
- 【商业版、提供全部源码】基于RBAC的C#ASP.NET支持多用户的通用权限管理系统高质量源码10月份销售20套
- 基于DSP的机器人视觉伺服系统研究
- 基于CS模式的考试系统的研究与开发