2019 Common Elasticsearch high-frequency face questions answer detailed analysis

Foreword

1.Elasticsearch RESTful style is a distributed search and data analysis engine.

(1) query: Elasticsearch allowed to perform and patients with multiple types of search - structured, unstructured, location metrics - heart and change the way the search.

(2) Analysis: find the best match the query ten documents is one thing. But if faced with billions of lines of log, how should we interpret it? Elasticsearch polymerization enables you to think big, to explore trends and patterns in the data.

(3) speed: Elasticsearch quickly. Really, really fast.

(4) Scalability: can run on a laptop. It can also run on hundreds of servers in the PB level data carrier.

(5) elastic: Elasticsearch run in a distributed environment, from initial design to take into account this point.

(6) Flexibility: with multiple case scenarios. Numbers, text, location, structured, unstructured. All types are welcome.

(7)HADOOP & SPARK : Elasticsearch + Hadoop

2.Elasticsearch is a highly scalable open source full-text search and analysis engine. It allows you to quickly and close to the store in real-time, search and analyze large amounts of data.

Here are some use cases using Elasticsearch:

(1) you run an online store, you allow your customers to search for products you sell. In this case, you can use Elasticsearch to store the entire product catalog and inventory, and to provide them with search and auto-complete suggestions.

(2) you want to collect log or transaction data and want to analyze and mine data to find trends, statistics, summary or abnormal. In this case, you can use loghide (part Elasticsearch / loghide / Kibana stack) to collect, aggregate, and parses the data, and then let loghide input data into the elasticsearch. Once the data is in Elasticsearch, you can run a search and aggregation to exploit any information you are interested in.

(3) price alert you run a platform that allows customers to specify proficient prices following rules: "I am interested in buying a particular electronic device, if any vendor's products next month for less than $ X, I want to be notified." In this case, you can grab the price of vendors, push them into the Elasticsearch and use its reverse lookup (Percolator) function to match prices with customer inquiries, and will ultimately find matching alarm push to the customer.

(4) Do you have analysis / business intelligence needs and wants quick survey, analysis, visualization, and large amounts of data presented special problems (think millions or billions of records of). In this case, you can use Elasticsearch to store data, and then use Kibana (part Elasticsearch / loghide / Kibana stack) to build custom dashboards to visualize all aspects that are important for your data. In addition, you can also use Elasticsearch aggregation function to perform complex business intelligence queries against the data.

v2-b2df9a051c55832e1c7f57495d48acee_hd.png

Elasticsearch face questions

13, a detailed description of what Elasticsearch update, and delete documentation process.

14, describe in detail the process of Elasticsearch search.

15, in Elasticsearch in, is how to find the corresponding inverted index based on a word?

16, Elasticsearch at deployment, optimization methods which have set for Linux?

17, for GC aspect, when using Elasticsearch pay attention to what?

18, Elasticsearch for a large amount of data (on the order of hundreds of millions) of polymeric how to achieve?

19, in the concurrent case, Elasticsearch if to ensure consistent read and write?

20, how to monitor Elasticsearch cluster status?

21, describes the overall technical architecture your electricity supplier search.

22, tell us about your personalized search program?

23, understand whether the trie?

24, spelling correction is how to achieve?

v2-29ea8cd048867befa2724e5806dee8dd_hd.png

13, a detailed description of what Elasticsearch update, and delete documentation process.

(1) deletes and updates are also writes, but Elasticsearch the document is immutable, and therefore can not be deleted or modified to change its display;

(2)磁盘上的每个段都有一个相应的.del 文件。当删除请求发送后,文档并没有真的被删除,而是在.del 文件中被标记为删除。该文档依然能匹配查询,但是会在结果中被过滤掉。当段合并时,在.del 文件中被标记为删除的文档将不会被写入新段。

(3)在新的文档被创建时,Elasticsearch 会为该文档指定一个版本号,当执行更新时,旧版本的文档在.del 文件中被标记为删除,新版本的文档被索引到一个新段。旧版本的文档依然能匹配查询,但是会在结果中被过滤掉。

14、详细描述一下 Elasticsearch 搜索的过程。

(1)搜索被执行成一个两阶段过程,我们称之为 Query Then Fetch;

(2)在初始查询阶段时,查询会广播到索引中每一个分片拷贝(主分片或者副本分片)。 每个分片在本地执行搜索并构建一个匹配文档的大小为 from + size 的优先队列。

PS:在搜索的时候是会查询 Filesystem Cache 的,但是有部分数据还在 MemoryBuffer,所以搜索是近实时的。

(3)每个分片返回各自优先队列中 所有文档的 ID 和排序值 给协调节点,它合并这些值到自己的优先队列中来产生一个全局排序后的结果列表。

(4)接下来就是 取回阶段,协调节点辨别出哪些文档需要被取回并向相关的分片提交多个 GET 请求。每个分片加载并 丰 富 文档,如果有需要的话,接着返回文档给协调节点。一旦所有的文档都被取回了,协调节点返回结果给客户端。

(5)补充:Query Then Fetch 的搜索类型在文档相关性打分的时候参考的是本分片的数据,这样在文档数量较少的时候可能不够准确,DFS Query Then Fetch 增加了一个预查询的处理,询问 Term 和 Document frequency,这个评分更准确,但是性能会变差。*

v2-24a5f411224666d47b552aa21cabc761_hd.jpg

15、在 Elasticsearch 中,是怎么根据一个词找到对应的倒排索引的?

(1)Lucene的索引过程,就是按照全文检索的基本过程,将倒排表写成此文件格式的过程。

(2)Lucene的搜索过程,就是按照此文件格式将索引进去的信息读出来,然后计算每篇文档打分(score)的过程。

16、Elasticsearch 在部署时,对 Linux 的设置有哪些优化方法?

(1)64 GB 内存的机器是非常理想的, 但是 32 GB 和 16 GB 机器也是很常见的。少于 8 GB 会适得其反。

(2)如果你要在更快的 CPUs 和更多的核心之间选择,选择更多的核心更好。多个内核提供的额外并发远胜过稍微快一点点的时钟频率。

(3)如果你负担得起 SSD,它将远远超出任何旋转介质。 基于 SSD 的节点,查询和索引性能都有提升。如果你负担得起,SSD 是一个好的选择。

(4)即使数据中心们近在咫尺,也要避免集群跨越多个数据中心。绝对要避免集群跨越大的地理距离。

(5)请确保运行你应用程序的 JVM 和服务器的 JVM 是完全一样的。 在Elasticsearch 的几个地方,使用 Java 的本地序列化。

(6)通过设置 gateway.recover_after_nodes、gateway.expected_nodes、gateway.recover_after_time 可以在集群重启的时候避免过多的分片交换,这可能会让数据恢复从数个小时缩短为几秒钟。

(7)Elasticsearch 默认被配置为使用单播发现,以防止节点无意中加入集群。只有在同一台机器上运行的节点才会自动组成集群。最好使用单播代替组播。

(8)不要随意修改垃圾回收器(CMS)和各个线程池的大小。

(9)把你的内存的(少于)一半给 Lucene(但不要超过 32 GB!),通过ES_HEAP_SIZE 环境变量设置。

(10)内存交换到磁盘对服务器性能来说是致命的。如果内存交换到磁盘上,一个100 微秒的操作可能变成 10 毫秒。 再想想那么多 10 微秒的操作时延累加起来。 不难看出 swapping 对于性能是多么可怕。

(11)Lucene 使用了大 量 的文件。同时,Elasticsearch 在节点和 HTTP 客户端之间进行通信也使用了大量的套接字。 所有这一切都需要足够的文件描述符。你应该增加你的文件描述符,设置一个很大的值,如 64,000。

补充:索引阶段性能提升方法

(1)使用批量请求并调整其大小:每次批量数据 5–15 MB 大是个不错的起始点。

(2)存储:使用 SSD

(3)段和合并:Elasticsearch 默认值是 20 MB/s,对机械磁盘应该是个不错的设置。如果你用的是 SSD,可以考虑提高到 100–200 MB/s。如果你在做批量导入,完全不在意搜索,你可以彻底关掉合并限流。另外还可以增加index.translog.flush_threshold_size 设置,从默认的 512 MB 到更大一些的值,比如 1 GB,这可以在一次清空触发的时候在事务日志里积累出更大的段。

(4)如果你的搜索结果不需要近实时的准确度,考虑把每个索引的index.refresh_interval 改到 30s。

(5)如果你在做大批量导入,考虑通过设置 index.number_of_replicas: 0 关闭副本。

17、对于 GC 方面,在使用 Elasticsearch 时要注意什么?

(1)倒排词典的索引需要常驻内存,无法 GC,需要监控 data node 上 segmentmemory 增长趋势。

(2)各类缓存,field cache, filter cache, indexing cache, bulk queue 等等,要设置合理的大小,并且要应该根据最坏的情况来看 heap 是否够用,也就是各类缓存全部占满的时候,还有 heap 空间可以分配给其他任务吗?避免采用 clear cache等“自欺欺人”的方式来释放内存。

(3)避免返回大量结果集的搜索与聚合。确实需要大量拉取数据的场景,可以采用scan & scroll api 来实现。

(4)cluster stats 驻留内存并无法水平扩展,超大规模集群可以考虑分拆成多个集群通过 tribe node 连接。

(5)想知道 heap 够不够,必须结合实际应用场景,并对集群的 heap 使用情况做持续的监控。

(6)根据监控数据理解内存需求,合理配置各类circuit breaker,将内存溢出风险降低到最低。欢迎大家关注我的公种浩【程序员追风】,2019年多家公司java面试题整理了1000多道400多页pdf文档,文章都会在里面更新,整理的资料也会放在里面。

v2-580d281447a3e4c336e9000797c48ee9_hd.png

18、Elasticsearch 对于大数据量(上亿量级)的聚合如何实现?

Elasticsearch 提供的首个近似聚合是 cardinality 度量。它提供一个字段的基数,即该字段的 distinct 或者 unique 值的数目。它是基于 HLL 算法的。HLL 会先对我们的输入作哈希运算,然后根据哈希运算的结果中的 bits 做概率估算从而得到基数。其特点是:可配置的精度,用来控制内存的使用(更精确 = 更多内存);小的数据集精度是非常高的;我们可以通过配置参数,来设置去重需要的固定内存使用量。无论数千还是数十亿的唯一值,内存使用量只与你配置的精确度相关。

19、在并发情况下,Elasticsearch 如果保证读写一致?

(1)可以通过版本号使用乐观并发控制,以确保新版本不会被旧版本覆盖,由应用层来处理具体的冲突;

(2)另外对于写操作,一致性级别支持 quorum/one/all,默认为 quorum,即只有当大多数分片可用时才允许写操作。但即使大多数可用,也可能存在因为网络等原因导致写入副本失败,这样该副本被认为故障,分片将会在一个不同的节点上重建。

(3)对于读操作,可以设置 replication 为 sync(默认),这使得操作在主分片和副本分片都完成后才会返回;如果设置 replication 为 async 时,也可以通过设置搜索请求参数_preference 为 primary 来查询主分片,确保文档是最新版本。

20、如何监控 Elasticsearch 集群状态?

Marvel 让你可以很简单的通过 Kibana 监控 Elasticsearch。你可以实时查看你的集群健康状态和性能,也可以分析过去的集群、索引和节点指标。

21、介绍下你们电商搜索的整体技术架构。

v2-8ef7d370b932d7726aceecfbccc1a49d_hd.jpg

22、介绍一下你们的个性化搜索方案?

基于word2vec和Elasticsearch实现个性化搜索

(1)基于word2vec、Elasticsearch和自定义的脚本插件,我们就实现了一个个性化的搜索服务,相对于原有的实现,新版的点击率和转化率都有大幅的提升;

(2)基于word2vec的商品向量还有一个可用之处,就是可以用来实现相似商品的推荐;

(3)使用word2vec来实现个性化搜索或个性化推荐是有一定局限性的,因为它只能处理用户点击历史这样的时序数据,而无法全面的去考虑用户偏好,这个还是有很大的改进和提升的空间;

23、是否了解字典树?

常用字典数据结构如下所示:

v2-83a119f2700bab1fa8ab7c924e760a17_hd.jpg

Trie 的核心思想是空间换时间,利用字符串的公共前缀来降低查询时间的开销以达到提高效率的目的。它有 3 个基本性质:

1)根节点不包含字符,除根节点外每一个节点都只包含一个字符。

2)从根节点到某一节点,路径上经过的字符连接起来,为该节点对应的字符串。

3)每个节点的所有子节点包含的字符都不相同。

v2-1e31bebad38bfaab59e5384e6dd062d7_hd.jpg

(1)可以看到,trie 树每一层的节点数是 26^i 级别的。所以为了节省空间,我们还可以用动态链表,或者用数组来模拟动态。而空间的花费,不会超过单词数×单词长度。

(2)实现:对每个结点开一个字母集大小的数组,每个结点挂一个链表,使用左儿子右兄弟表示法记录这棵树;

(3)对于中文的字典树,每个节点的子节点用一个哈希表存储,这样就不用浪费太大的空间,而且查询速度上可以保留哈希的复杂度 O(1)。

24、拼写纠错是如何实现的?

(1)拼写纠错是基于编辑距离来实现;编辑距离是一种标准的方法,它用来表示经过插入、删除和替换操作从一个字符串转换到另外一个字符串的最小操作步数;

(2)编辑距离的计算过程:比如要计算 batyu 和 beauty 的编辑距离,先创建一个7×8 的表(batyu 长度为 5,coffee 长度为 6,各加 2),接着,在如下位置填入黑色数字。其他格的计算过程是取以下三个值的最小值:

如果最上方的字符等于最左方的字符,则为左上方的数字。否则为左上方的数字+1。(对于 3,3 来说为 0)

左方数字+1(对于 3,3 格来说为 2)

上方数字+1(对于 3,3 格来说为 2)

最终取右下角的值即为编辑距离的值 3。

v2-57fb52790c10465912f9e608703d7497_hd.jpg


对于拼写纠错,我们考虑构造一个度量空间(Metric Space),该空间内任何关系满足以下三条基本条件:

d(x,y) = 0 -- 假如 x 与 y 的距离为 0,则 x=y

d(x,y) = d(y,x) -- x 到 y 的距离等同于 y 到 x 的距离

d(x,y) + d(y,z) >= d(x,z) -- 三角不等式

(1)根据三角不等式,则满足与 query 距离在 n 范围内的另一个字符转 B,其与 A的距离最大为 d+n,最小为 d-n。

Configuration (2) BK on the tree as follows: Each node has any child nodes, each edge value indicates that there is an edit distance. All child nodes of the parent node to the label side of exactly n represents an edit distance is n. For example, we have a tree parent node is "book" and two child nodes "cake" and "books", "book" to "books" edge label 1, "book" to "cake" of the edge of the label 4. After a good tree structure from the dictionary, whenever you want when you insert a new word, the word edit distance calculation and root, and look for a value of d (neweord, root) side. Recursive was compared with each child node until no child nodes, you can create a new child node and save new words in that. For example, insert "Boo" tree to just the above examples, we first check the root node, find d ( "book", "boo") = 1 side and check node side sub-numeral 1, to obtain the word " books ". We then calculate the distance d ( "books", "boo") = 2, then a new word is inserted after "books", reference numeral 2 side.

3, similar to the query word as follows: the root node of the word is calculated edit distance d, and then recursively locate each child node to the designated dn d + n (inclusive) side. If the distance is checked and Word Search node d is less than n, and the node returns to research. Such as input cape and a maximum tolerable distance of 1, the first edit distance calculation and root d ( "book", "cape") = 4, and then find and edit distance between the root of 3 to 5, to find the the node of the cake, calculated d ( "cake", "cape") = 1, the condition is returned cake, cake, and then find the node edit distance is 0 to 2, respectively, and to find the cape cart node, thus obtaining cape this satisfies criteria.

v2-414dc1cfe3428df85f5d947a793e9505_hd.jpg


At last

Welcome to share with everyone, like I remember the article I focus a point like yo, thanks for the support!


Guess you like

Origin blog.51cto.com/14442094/2462142