《从Lucene到Elasticsearch:全文检索实战》 基于es5较新,国内的
《深入理解Elasticsearch》 基于es0.9比较老,国外的
《Elasticsearch技术解析与实战》 基于es2.2和es5较新,国内的
《Elasticsearch in Action》 国外的
《Elasticsearch 权威指南》 国外的
https://www.cnblogs.com/dreamroute/p/8484457.html
http://www.ruanyifeng.com/blog/2017/08/elasticsearch.html
https://www.yiibai.com/elasticsearch/elasticsearch-getting-start.html
https://es.xiaoleilu.com/
https://www.elastic.co/guide/cn/elasticsearch/guide/current/_how_to_read_this_book.html
https://blog.youkuaiyun.com/napoay/article/details/78698226?utm_source=gold_browser_extension
https://blog.youkuaiyun.com/laoyang360/article/details/78554610
https://cloud.tencent.com/developer/article/1106755
ES的关键是构建倒排索引;搜索(Lucene)是按照关键词查询的,因此需要解析句子得到关键词;
首先需要分词;然后去掉一些无意义的词,如a、an、the以及‘的’等;然后统一大小写如he、HE;
甚至还原词根,如livs、lived变为live;去掉标点符号;这样就得到一个句子的所有关键词;
(以上由Lucene的Analyzer类执行;)
有了关键词就能构造倒排索引;即根据关键词找到所在的文章;
也可以看成一个‘关键词-文章’矩阵,这就类似LSA(潜在语义分析)了;(有一个不同是,es或倒排索引需要考虑关键词的出现次数和位置;)
(插入的过程就是构建倒排索引的过程?)
(es或倒排索引没有用到词向量?)
(关键词按字典顺序排序,Lucene没有使用B树结构!)
(Lucene使用三个文件构造倒排索引:词典文件、频率文件、位置文件;其中词典文件除了关键词,还保留指向频率文件和位置文件的指针)
(Lucene考虑到关键词能同时出现在标题、文章、url多种情况,使用field字段记录该信息,field字段也记录在词典文件中;每个关键词有一个field信息)
创建索引后,可以改变副本的数量,但不能改变分片的数量;
分片是有存储大小限制的,大概是Integer.max_value-128;
Relational DB -> Databases -> Tables -> Rows -> Columns
Elasticsearch -> Indices(索引簇)-> Types -> Documents -> Fields
BM25打分算法:
https://en.wikipedia.org/wiki/Okapi_BM25
https://www.cnblogs.com/hdflzh/p/4034602.html
ES5用BM25代替tf-idf算法;
ES基于对等架构,虽然会自动选举管理节点,但对用户来说,并不比其他节点更重要;
(ES是自动选举leader或master的,和传统的master/slave不一样,自带HA,没有单点问题?)
(一般自动选举的,都是过半原则,即只要超过一半节点正常就行?)
ES查询分为分散阶段和合并阶段,前者将query分散到各个分片执行查询,后者从多个分片收集返回结果,进行合并、排序、后续处理;
Zen发现是ES自带的默认发现机制;分为多播和单播;默认使用多播;
###########ES源码分析##########
es的启动类是org.elasticsearch.bootstrap.Elasticsearch;
(该类的main方法依赖org.elasticsearch.bootstrap.Bootstrap;)
(Bootstrap依赖多个包和文件,如org.elasticsearch.common、org.elasticsearch.env、org.elasticsearch.monitor、org.elasticsearch.node等;)
(Bootstrap也依赖同目录的JNAKernel32Library、Natives、BootstrapCLIParser、JVMCheck、JarHell、ConsoleCtrlHandler等类,继而依赖JNANatives、JNACLibrary等类;)
(Bootstrap的init、start方法会调用Node的start方法;)
(org.elasticsearch.monitor.process.ProcessProbe是进程探针类;利用了java.lang.management和反射机制;)
(同时依赖ProcessStats类实现对CPU、内存、pid的监控;具体是ProcessStats的内部类Cpu、Mem;以及ProcessInfo、JvmInfo类;)
(ProcessService类是对ProcessProbe、ProcessInfo的封装;)
(org.elasticsearch.node的Node类封装了Lifecycle、Injector、Settings、Environment、PluginsService、Client等类;)
(Node依赖InternalSettingsPreparer构造Settings;)
(Node依赖org.elasticsearch.threadpool的ThreadPool构造线程池;后者依赖java.util.concurrent.Executor、ExecutorService和ScheduledThreadPoolExecutor;)
(Node利用ModulesBuilder中存放多种module,如CircuitBreakerModule、PluginsModule、SettingsModule、NodeModule、NetworkModule、ScriptModule、EnvironmentModule、
NodeEnvironmentModule、ClusterNameModule、DiscoveryModule、ClusterModule、RestModule、TransportModule、IndicesModule、SearchModule、ActionModule、
MonitorModule、GatewayModule、NodeClientModule、ShapeModule、PercolatorModule、ResourceWatcherModule、RepositoriesModule、TribeModule、ThreadPoolModule;
Node调用PluginsService的processModule方法执行module;)
(Node主要方法是start、stop、close、isClosed、writePortsFile;)
(Node的start方法启动多个服务,如IndicesService、IndicesClusterStateService、IndicesTTLService、SnapshotsService、SnapshotShardsService、RoutingService、
SearchService、MonitorService、ResourceWatcherService、GatewayService、TribeService等;)
(Node的stop方法关闭以上服务;)
(NodeModule类依赖org.elasticsearch.cache.recycler.PageCacheRecycler和NodeService、NodeSettingsService;)
(NodeService又封装了MonitorService、TransportService、IndicesService、PluginsService、CircuitBreakerService、ScriptService、HttpServer等多个服务;)
(NodeService也封装了Discovery用于发现其他节点、发布集群状态、选主;)
(NodeSettingsService利用CopyOnWriteArrayList封装了多个Listener;)
(MonitorService和MonitorModule都依赖JvmMonitorService、OsService、ProcessService、JvmService、FsService;)
(JvmMonitorService依赖JvmStats、JvmInfo、GcNames; JvmService依赖JvmInfo、JvmStats;)
(OsService依赖OsInfo、OsProbe、OsStats;其中OsProbe利用反射机制实现;)
(FsService依赖FsProbe、FsInfo;FsProbe依赖NodeEnvironment;)
(ProcessService依赖ProcessProbe、ProcessInfo、ProcessStats;其中ProcessProbe利用反射机制和java.lang.management.OperatingSystemMXBean实现;)
(TransportService依赖TransportStats、TransportRequest、TransportRequestHandler、TransportServiceAdapter、TransportResponse、TransportMessage、
TransportConnectionListener、PlainTransportFuture、RequestHandlerRegistry、Transport、TransportChannel、TransportFuture等同目录文件;)
(TransportModule依赖TransportService,将其存入hashmap;)
(TransportModule依赖NettyTransport实现通信;后者依赖SizeHeaderFrameDecoder、NettyHeader、MessageChannelHandler;)
(NettyTransportChannel、MessageChannelHandler封装了NettyTransport;)
(IndicesService依赖PluginsService、IndicesAnalysisService、IndexService、InternalIndicesLifecycle、IndicesLifecycle、SyncedFlushService、NodeIndicesStats等;)
(IndicesModule依赖大量org.elasticsearch.index.query下的各种XXXParser;以及org.elasticsearch.index.mapper.core下的各种XXXMapper;
org.elasticsearch.index.mapper.internal下的各种XXXMapper;)
(IndicesModule还依赖IndicesClusterStateService、SyncedFlushService、IndicesTTLService、IndicesFieldDataCache、IndicesFieldDataCacheListener、
HunspellService、IndexingMemoryController、IndicesQueriesRegistry、IndicesStore、RecoveryTarget、RecoveriesCollection等;)
(IndexService封装了PluginsService、AnalysisService、MapperService、IndexQueryParserService、SimilarityService、IndexAliasesService、IndicesService、
IndexSettingsService、IndexFieldDataService等;)
(IndexService还依赖IndexShard、BitsetFilterCache、IndexCache等;)
(IndexService方法有createShard、removeShard等;)
(IndexModule依赖IndexService;)
(es和lucene的BM25算法:org.apache.lucene.search.similarities.BM25Similarity、org.elasticsearch.index.similarity.BM25SimilarityProvider)
(除了BM25相似性,还有IB、DFR、LMDirichlet、DFI等;)
(PluginsService依赖PluginInfo、SitePlugin; PluginsModule依赖PluginsService;)
(ScriptService依赖ScriptEngineService、CompiledScript、ScriptModes、ScriptContextRegistry、ScriptMetrics、ScriptMode、ScriptStats、Template、
SearchScript、ScriptContext等类; ScriptModule依赖ScriptService、NativeScriptFactory等;)
(HttpServer依赖HttpServerTransport、HttpInfo、HttpStats、HttpServerAdapter等;)
(注意:org.elasticsearch.http.netty.cors是跨域资源共享,cors代表Cross Origin Resource Sharing)
(GatewayModule依赖GatewayService、MetaStateService、DanglingIndicesState、LocalAllocateDangledIndices、TransportNodesListGatewayStartedShards等;
GatewayService依赖Gateway、ClusterService、AllocationService、DiscoveryService等;)
(DiscoveryService依赖Discovery、DiscoverySettings,方法有publish、doStart、doStop等;)
(DiscoveryModule依赖localDiscovery、ZenDiscovery、DiscoveryService、UnicastHostsProvider等;)
(ZenDiscovery又依赖PublishClusterStateAction、NodeJoinController、ZenPingService、MembershipAction、NodesFaultDetection、MasterFaultDetection、
ElectMasterService、FaultDetection等;)
(LocalDiscovery依赖RoutingService、ClusterService等;)
(ClusterModule依赖org.elasticsearch.cluster.routing.allocation.decider的多个Decider、org.elasticsearch.cluster.settings的DynamicSettings、
org.elasticsearch.cluster.action的Action、org.elasticsearch.cluster.routing的RoutingService、org.elasticsearch.cluster.metadata的Service;)
(org.elasticsearch.env的EnvironmentModule依赖Environment、NodeEnvironment、ESFileStore等;)
(PercolatorModule依赖PercolatorService;后者依赖QueryCollector、SingleDocumentPercolatorIndex、MultiDocumentPercolatorIndex、PercolateContext等;)
(RestModule依赖BaseRestHandler、RestController、RestHandler、RestRequest、RestResponse、RestFilterChain、RestStatus、RestChannel、RestFilter、RestUtils等;)
(RestModule还依赖RestActionModule;后者是org.elasticsearch.rest.action整个模块的对外接口;)
(RestActionModule依赖RestMainAction、RestIndexAction、RestGetAction、RestGetSourceAction、RestHeadAction、RestMultiGetAction等所有action;)
(ActionModule依赖GenericAction、ActionRequest、ActionResponse,以及org.elasticsearch.action.admin.cluster、org.elasticsearch.action.search、
org.elasticsearch.action.support、org.elasticsearch.action.admin.indices、org.elasticsearch.action.termvectors等;)
(SearchModule依赖org.elasticsearch.search.suggest、org.elasticsearch.search.fetch、org.elasticsearch.search.aggregations、org.elasticsearch.search.dfs、org.elasticsearch.search.action、org.elasticsearch.search.controller、org.elasticsearch.search.highlight等;
同时依赖SearchService;)
(RepositoriesModule依赖RepositoriesService、RepositoryTypesRegistry、URLRepository、BlobStoreRepository、Repository、RepositoryName、RepositorySettings、VerificationFailure、FsRepository等; RepositoriesModule和RepositoryModule的关系?)
(org.elasticsearch.client包括AdminClient、ElasticsearchClient、IndicesAdminClient、ClusterAdminClient、FilterClient、NodeClient、TransportClient等多种client;)
Node -> RestModule -> RestActionModule -> RestMainAction
es2.4.6最重要、代码最多的模块或包:
org.elasticsearch.index、org.elasticsearch.common、org.elasticsearch.action、org.elasticsearch.cluster、org.elasticsearch.indices、org.elasticsearch.rest、org.elasticsearch.search;
es2.4.6基于netty3.10.6实现,如org.elasticsearch.http.netty和org.elasticsearch.transport.netty;
es的发现机制对应org.elasticsearch.discovery,分为local和zen;
es使用google Guice实现依赖注入,见org.elasticsearch.common.inject.Guice;
(https://blog.youkuaiyun.com/ZYC88888/article/details/80402293 Google Guice基础应用)