Local update operations in Elasticsearch (work notes)

Source: Internet
Author: User
Tags throwable

The equivalent of a memo, I feel that the memory is not good, I do some time may not remember clearly, so write a note to forget

1. First is the ES configuration because it involves scripting to update

You need to add the following configuration to the Elasticsearch configuration file elasticsearch.yml:

Script.engine.groovy.inline.update:on


2. The two scripts that are locally updated are actual business scenarios (groovy)

The car_target_id and item are data, and the + = operation of the data in the script corresponds to the push operation of the array

1.if (ctx._source.car_target_id==null) {ctx._source.car_target_id=car_target_id} else{ctx._source.car_target_id+=car_target_id;}
2.if (ctx._source.item==null) {Ctx._source.item=item} Else{ctx._source.item+=item;}


3. Main function

1. Index creation and deletion

2.BulkProcessor do asynchronous bulk commit use


4. Main Project Code

1.IndexConstants


Package com.kom.giant.userbehavior.analyzer.constants;

/**
 * indexconstants
 *
 littlehow
 2017-06-28 11:03/public
Abstract Class Indexconstants {
    /** index type *
    /public static final String Common_index_type = "behavior";
    /** Index name *
    /public static final String Index_name_behavior_log = "Behavior_log";
    public static final String index_name_behavior_statistic = "behavior_statistic";
}


2.BehaviorLogIndex


Package com.kom.giant.userbehavior.analyzer.constants; /** * Behaviorlogindex * *@authorLittlehow *@time2017-06-27 17:52 */public enum Behaviorlogindex {trace_id ("trace_id"),//Index ID description ("description"),//description
    Terminal_type ("Terminal_type"),//terminal type person_id ("person_id"),//Caller ID page_no ("page_no"),//page number, if paging needs to know the current page number Product_ids ("Product_ids"),//product result set Id,number array product_info ("Product_info"),//Product Information Object array item ("Item"),//Commodity information OB Ject array product_id ("product_id"),//Product ID item_info ("item_info"),//Commodity Information Object array result_id ("result_id"),//Result Information pro DUCT_INFO.RESULT_ID,ITEM_INFO.RESULT_ID label ("label"),//label information string array Product_info.label, Item_info.label Car_targ et_id ("car_target_id"),//Add the ID number array of the shopping cart order_target_id ("order_target_id"),//order ID number array keywords ("keywords" ),//Keyword real_search_words ("real_search_words"),//may have been corrected interface_name ("Interface_name"),//Call Interface Call_time ("CA
    Ll_time "),//Call time Last_data_time (" Last_update_time "),//Last updated data time;

    Public final String value;
 Behaviorlogindex (String value) {       This.value = value;
 }
}


3.EsOperationDao (This class is the main action class)


Package Com.kom.giant.userbehavior.analyzer.dao;
Import Com.kom.base.elasticsearch.client.ESClientFactory;
Import Org.elasticsearch.action.ActionListener;
Import Org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
Import org.elasticsearch.action.bulk.*;
Import Org.elasticsearch.action.update.UpdateRequest;
Import org.elasticsearch.client.Client;
Import Org.elasticsearch.common.unit.TimeValue;
Import Org.elasticsearch.common.xcontent.XContentBuilder;
Import Org.elasticsearch.common.xcontent.XContentFactory;
Import Org.elasticsearch.script.Script;
Import Org.elasticsearch.script.ScriptService;
Import Org.slf4j.Logger;
Import Org.slf4j.LoggerFactory;
Import Org.springframework.beans.factory.InitializingBean;
Import org.springframework.beans.factory.annotation.Autowired;

Import Org.springframework.stereotype.Repository;
Import java.io.IOException;
Import java.util.List;

Import Java.util.Map; Import Static Com.kom.giant.userbehavior.analyzer.constants.IndexConstants.*;

Import static com.kom.giant.userbehavior.analyzer.constants.behaviorlogindex.*;  /** * Esoperationdao * *@authorLittlehow *@time2017-06-27 17:49/@Repository public class Esoperationdao implements Initializingbean {//log record private Logger l
    Ogger = Loggerfactory.getlogger (This.getclass ());
    @Autowired private Esclientfactory esclientfactory;
    Private Bulkprocessor bulkprocessor; Add Cart Script Private final String updatecartransscript = "if (ctx._source.") + Car_target_id.value + "==null" {ctx._source. "+ car_target_id.value +" = "+ Car_target_id.value +";} Else{ctx._source. "
    + Car_target_id.value + + + + Car_target_id.value + ";}"; Add product Information Private final String Updateitemscript = "if (ctx._source." + Item.value + "==null) {ctx._source." + Item.valu E + "=" + Item + ";}

    Else{ctx._source. "+ Item.value + + + +" + Item.value + ";}"; /** * INSERT Update index DATA *@paramIndexmaplist */public void upsertbehaviorlogmaplist (list<map<string, object>> indexmaplist) {
        if (indexmaplist = null | | indexmaplist.size () = = 0) {return;
        Client client = Esclientfactory.getclient ();
        Bulkrequestbuilder Bulkrequestbuilder = Client.preparebulk (); 
            For (map<string, object> indexmap:indexmaplist) {String id = indexmap.get (trace_id.value). toString (); try {bulkrequestbuilder.add (new Updaterequest (Index_name_behavior_log, Common_index_type, ID
            ). Docasupsert (True). doc (Indexmap);
            catch (Throwable t) {t.printstacktrace (); } logger.info ("Upsert index[{}]" s doc;
            id={} ", Index_name_behavior_log, id);
            if (logger.isdebugenabled ()) {Logger.debug (indexmap.tostring ());
        } final int num = Indexmaplist.size (); Bulkrequestbuilder.execute (New ACTIOnlistener<bulkresponse> () {@Override public void Onresponse (Bulkresponse bulkitemresponses
                {if (logger.isdebugenabled ()) {logger.debug ("num [{}] Bulk success!!!", num); @Override public void OnFailure (Throwable e) {Logge
            R.error ("Bulk num [{}], error msg: {}", num, E.getlocalizedmessage ());
    }
        });  /** * Execute Log Insert *@paramIndexmap */public void Upsertbehaviorlogmap (map<string, object> indexmap) {String id = indexmap.g
        ET (trace_id.value). toString ();
        Logger.info (Index_name_behavior_log + "Upsert ID" + ID); Bulkprocessor.add (New Updaterequest (Index_name_behavior_log, Common_index_type, id). Docasupsert (True). doc (
    Indexmap)); /** * Add Shopping cart conversion *@paramParammap-Script modification parameters/public void Upsertbehaviorcartrans (map<string, object> parammap) {String ID
        = (String) parammap.get (Trace_id.value);
        Logger.info (Index_name_behavior_log + "Upsert ID" + ID);
        Updaterequest updaterequest = new Updaterequest (Index_name_behavior_log, common_index_type, id);
        Updaterequest.script (New script (Updatecartransscript, ScriptService.ScriptType.INLINE, NULL, parammap));
    Bulkprocessor.add (updaterequest); /** * Commodity Information Store *@paramParammap */public void Upsertbehavioritem (map<string, object> parammap) {String id = (string) para
        Mmap.get (Trace_id.value);
        Logger.info (Index_name_behavior_log + "Upsert ID" + ID);
        Updaterequest updaterequest = new Updaterequest (Index_name_behavior_log, common_index_type, id);
        Updaterequest.script (New script (Updateitemscript, ScriptService.ScriptType.INLINE, NULL, parammap));
    Bulkprocessor.add (updaterequest);
        /** * Active Refresh processor, storing data in ES/public void Flushbulkprocessor () {//This method is marked as synchronized
    Bulkprocessor.flush (); }

    /**
     *

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.