弹性搜索有复合指数吗? [英] does elasticsearch have compound indexes?

查看:97
本文介绍了弹性搜索有复合指数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道弹性搜索是否需要先验定义复合索引。通过复合索引,我的意思是什么像mongodb有的。

i'm wondering if elasticsearch needs to have compound indexes defined a priori. by a compound index, i mean something like what mongodb has.


db.collection.ensureIndex({field1:1,field2:1, field3:1})

db.collection.ensureIndex( { field1: 1, field2: 1, field3: 1 } )

或类似mysql数据库的东西。

or something like what mysql db has.


在mytable(field1,field2,field3)上创建索引adhoc_index;

create index adhoc_index on mytable(field1, field2, field3);

所以我正在处理的数据与非常平坦(大部分只是csv格式)。它看起来像以下(完整性)。

so the data i am dealing with is very flat (most of it is just csv format). it looks like the following (for completeness).


field1,field2,...,fieldN

field1, field2, ..., fieldN

字段数是任意的。一个数据集可能有10个字段,另外20个字段,另一个数据库1000个。我基本上将每行转换成一个JSON文档,如下所示。

the number of fields is arbitrary. one dataset may have 10 fields, another 20, another 1000. i basically convert each row into a JSON document that looks like the following.

{
 "field1" : "value1",
 "field2" : "value2",
 ...
 "fieldN" : "valueN"
}



< ...,fieldN}。在任何给定的时间,我必须建立一个动态查询过滤A = a,B = b和C = c的记录。

denote A, B, and C as three mutually exclusive subsets of the fields: {field1, field2, ..., fieldN}. at any given time, i have to build a dynamic query that filters the records for A=a, B=b, and C=c.

例如,


  • A = {field1},B = {field2 ,field3},C = {field6}

  • A = {field2},B = {field1},C = {field1000,field50}

所以我的弹性搜索DSL查询可能看起来像以下(不知道这是否正确我自己,但只是为了说明)。

so my elasticsearch DSL query may look something like the following (not sure if this is correct myself, but just to illustrate).

"bool" : {
 "must" : [
  {"term" : { "field1" : "val1" },
  {"term" : { "field2" : "val2" },
  {"term" : { "field3" : "val3" },
  {"term" : { "field4" : "val4" }
 ]
}

基本上,这个查询说:给我所有field1 = val1,field2 = val2,field3 = val3,field4 = val4的文档。

basically, this query says, "give me all the documents with field1=val1, field2=val2, field3=val3, field4=val4".

我问这个关于弹性搜索的原因是因为我在互联网上找不到关于复合索引的明确答案。他们甚至需要吗?

the reason why i ask this about elasticsearch is because i could not find a clear answer searching on the internet about compound indexes. are they even needed?

我也在评估mongodb和mysql,我不认为他们会很好地适应我的情况,只因为这些复合/复合索引必须被定义在运行之前,我将不会拥有该信息,需要将这些字段编入索引以优化查询速度。当然,使用mysql,一旦我找出哪些字段需要被索引在一起(按顺序),我可以返回创建索引,但是如果数据集很大,则可能需要很长时间行> 100万)。

i'm evaluating mongodb and mysql as well, and i don't think they will work well with my situation simply because these compound/composite indexes have to be defined a priori, and i won't have that information until runtime which group of fields need to be indexed together to optimize the query speed. of course, with mysql, once i find out which group of fields need to be indexed together (and in which order), i can go back an create the index, but that may take a long time if the dataset is large (number of rows > 1 million).

我是否可以使用弹性搜索将这个复合索引功能开箱即用?意思是,我甚至不必去摸索引映射文件/定义?

do i simply get this compound index feature out of the box with elastic search? meaning, i won't even have to touch the index mapping file/definition?

推荐答案

ElasticSearch没有复合索引,但是在查询多个索引并将其相交时非常有效(相交的位向量FTW )。

ElasticSearch doesn't have composite indexes, but it's very efficient at querying multiple indexes and intersecting them (intersecting bit-vectors FTW).

大多数情况下,复合索引不需要,即使您提到在四个不同字段中查询的情况。 ElasticSearch将高兴地查询4个不同的索引,然后以有效的方式与结果相交。根据我的经验,在类似的情况下,其性能与MongoDB相匹配,超过了MongoDB。

Most of the time, composite indexes are not needed, even for cases like you mentioned where you query for 4 different fields. ElasticSearch will happily query 4 different indexes and then intersect the results in an efficient manner. In my experience its performance matches and surpasses that of MongoDB in similar situations.

如果您绝对必须具有复合索引,则可以考虑将其值为组合您要索引的值。

If you absolutely must have a composite index, you might consider indexing an auxiliary field whose value is a composite of the values you want to index.

这篇关于弹性搜索有复合指数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆