2016-05-30 58 views
0

我想索引10亿条记录。每个记录有2个属性(属性1和属性2)。 属性1中具有相同值的每条记录都必须合并。例如,我有两个记录在ElasticSearch建立索引后搜索

attribute1 attribute2 
1 4 
1 6 

我的弹性文档必须

{ 
    "attribute1": "1" 
    "attribute2": "4,6" 
} 

由于数据量巨大,我一定要读批量(大约1000条记录),并根据它们合并上面的规则(在内存中),然后在ElasticSearch中搜索它们并将它们与搜索结果合并,然后对它们进行索引/重新索引。 总之,我必须分别搜索和索引每个散装。 我实现了这条规则,但在某些情况下,Elastic不会返回所有结果,并且某些文档已被重复编入索引。 之后每个索引我刷新ElasticSearch,以便它准备好下一个搜索。但在某些情况下,它不起作用。 我的索引设置之后为:

{ 
"test_index": { 
    "settings": { 
     "index": { 
      "refresh_interval": "-1", 
      "translog": { 
       "flush_threshold_size": "1g" 
      }, 
      "max_result_window": "1000000", 
      "creation_date": "1464577964635", 
      "store": { 
       "throttle": { 
        "type": "merge" 
       } 
      } 
     }, 
     "number_of_replicas": "0", 
     "uuid": "TZOse2tLRqGk-vHRMGc2GQ", 
     "version": { 
      "created": "2030199" 
     }, 
     "warmer": { 
      "enabled": "false" 
     }, 
     "indices": { 
      "memory": { 
       "index_buffer_size": "40%" 
      } 
     }, 
     "number_of_shards": "5", 
     "merge": { 
      "policy": { 
       "max_merge_size": "2g" 
      } 
     } 
    } 
} 

我怎样才能解决这个问题呢?

是否有其他设置来处理这种情况?

回答

0

在批量命令,你需要使用index操作中第一次出现,然后update用一个脚本来更新您的attribute2属性:

{ "index" : { "_index" : "test_index", "_type" : "test_type", "_id" : "1" } } 
{ "attribute1" : "1", "attribute2": [4] } 
{ "update" : { "_index" : "test_index", "_type" : "test_type", "_id" : "1" } } 
{ "script" : { "inline": "ctx._source.attribute2 += attr2", "params" : {"attr2" : 6}}} 

第一index操作后您的文档会像

{ 
    "attribute1": "1" 
    "attribute2": [4] 
} 

第二update手术后,你的文档会像

{ 
    "attribute1": "1" 
    "attribute2": [4, 6] 
} 

请注意,也可以仅使用update操作doc_as_upsertscript

+0

我的问题是为什么有时当我索引数据并刷新弹性,并立即搜索它时,弹性不返回结果? – Ghasem

+0

如何刷新您的索引?我看到'refresh_interval'设置为-1,意思是“永不刷新”。批量索引后的 – Val

+0

。我使用API​​调用刷新。 – Ghasem