ElasticSearch Filtered Aliases Creation - Best Practice

淺唱寂寞╮ 提交于 2019-12-13 09:50:03

问题


We are planning to use Filtered Aliases as mentioned here - https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

Our input data is going to be a stream with each line of the stream corresponding to an object we would like to store in ES.

Each object contains an 'id', which we are using for routing and filtering.

QUESTION - How do we create alias and index data in a performant way ?

-- Do we index all data, keep track of all the unique 'id's and the very end create the filtered alias ? OR

-- For each object, check if an alias for that 'id' exists; if it doesn't create one ?

I'm leaning towards the first approach. Is it advisable and performant when compared to the second approach ?

TIA.


回答1:


Based on our discussion above and after having glanced over the blog article you posted, I'm pretty positive that in your case you don't need aliases at all and the routing key would suffice. Again, only because you have a single index, if you had many indices this would not be true anymore!

You simply need to specify the routing key to use when indexing your document. Until ES 2.0, you can use the _routing field for that purpose, even though it's been deprecated in ES 1.5, but in your case it serves your purpose.

{
    "customer" : {
        "_routing" : {
            "required" : true,
            "path" : "customer_id"     <----- the field you use as the routing key
        },
        "properties": { ... }
    }
}

Then when searching you simply need to specify &routing=<customer_id> in your search URL in addition to your customer id filter (since a given shard can host documents for different customers). Your search will go directly to the shard identified by the given routing key, and thus, only retrieve data from the specified customer.

Using a filtered alias for this brings nothing as the filter and routing key you'd include in your alias definition would not contribute anything additional, since the retrieved documents are already "filtered" (kind of) by the routing key. This is way easier than trying to detect (on each new document to index) if an alias exists or not and create it if it doesn't.

UPDATE:

Now if you absolutely have/want to create filtered aliases, the more performant way would be the first one you mentioned:

  1. First index your daily data
  2. Then run a terms aggregation on your customer_id field with size high enough (i.e. higher than the cardinality of the field, which was ~100 in your case) to make sure you capture all unique customer ids to create your aliases
  3. Loop over all the buckets to retrieve all unique customer ids
  4. Create all aliases in one shot using one action for each customer_id
curl -XPOST 'http://localhost:9200/_aliases' -d '{
    "actions" : [
        {
            "add" : {
                 "index" : "customers",
                 "alias" : "alias_cid1",
                 "routing" : "cid1",
                 "filter" : { "term" : { "customer_id" : "cid1" } }
            }
        },
        {
            "add" : {
                 "index" : "customers",
                 "alias" : "alias_cid2",
                 "routing" : "cid2",
                 "filter" : { "term" : { "customer_id" : "cid2" } }
            }
        },
        {
            "add" : {
                 "index" : "customers",
                 "alias" : "alias_cid3",
                 "routing" : "cid3",
                 "filter" : { "term" : { "customer_id" : "cid3" } }
            }
        },
        ...
    ]
}'

Note that you don't have to worry if an alias already exists, the whole command won't fail and silently ignore the existing alias.

When this command has run, you'll have all your aliases on your unique index, properly configured with a filter and a routing key.



来源:https://stackoverflow.com/questions/30495154/elasticsearch-filtered-aliases-creation-best-practice

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!