ES基本操作

用Python操作ES

Posted by 陶叔 on March 18, 2020
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
    def load_es():
        es = Elasticsearch(['http://url:port'], http_auth=('username', 'password'))
        actions = []
        count = 1
        data = pd.read_csv('data.csv')
        #     apply_data2 = df.as_matrix()
        for i in range(len(data)):
            df = data[i:i + 1]
            action = {
                '_index': 'qxk_offline_yx_two',
                '_type': 'qxk_offline_yx_two',
                '_id': str(df['user_id'].iloc[0]),
                '_source': {
                    'user_id': str(df['user_id'].iloc[0]),
                    'channel_no': str(df['channel_no'].iloc[0]),
                    'credit_amount': str(df['credit_amount'].iloc[0]),
                    'text_label': str(df['text_label'].iloc[0]),
                    'time_label': str(df['time_label'].iloc[0]),
                    'dt': str(df['dt'].iloc[0])
                }
            }
            count = count + 1
            actions.append(action)
            print(action)
            if len(actions) == 500:
                try:
                    helpers.bulk(es, actions)
                    del actions[0:len(actions)]
                except:
                    print(line)
        if len(actions) > 0:
            helpers.bulk(es, actions)

http://blog.51yip.com/server/1916.html http://www.kancloud.cn:8080/guanfuchang/es_search/859005