ElasticSearch使字段的行为类似于set而不是list

时间:2018-10-31 08:28:09

标签: elasticsearch

我在弹性搜索中有一个包含元素列表的字段,我想确保此列表中没有重复的元素,我对应用程序进行了更改,有没有一种方法可以运行ES命令来更新已经建立索引的文件。

1 个答案:

答案 0 :(得分:0)

抱歉,这有点晚了,但这可以通过两种方式完成:

假设我有以下文件:

__next__

通过查询更新使用脚本

POST sourceindex/mydocs/1
{
  "myIntegerArray": [1,2,3,4,5,5,6],
  "myStringArray" : [ "Chelsea", "Manu", "Arsenal", "Arsenal"]
}

使用摄取/管道/处理器技术

在这里,您需要执行重新索引,这与更新查询不同,这需要您从POST sourceindex/_update_by_query { "script": { "lang": "painless", "source": """ int size = ctx._source['myIntegerArray'].size(); int i=0; Set myset = new HashSet(); for(ListIterator it = ctx._source['myIntegerArray'].listIterator(); it.hasNext();){ i = it.next(); it.remove(); myset.add(i); } ctx._source['myIntegerArray'].addAll(myset); """ } } 重新索引,并且如果索引您需要从sourceindex -> destinationindex重新索引到destinationindex如果您不使用任何别名功能,则打算使用sourceindex。建议您看看Ingest功能

打破步骤:

sourceindex

示例管道

Create ingestion pipeline
Create dest_index
Apply reindex operation using pipeline to ingest from source_index to dest_index

重新索引查询

PUT _ingest/pipeline/my-pipeline
{
  "description" : "duplicate removal pipeline",
  "processors" : [
    {

        "script": {
          "lang": "painless",
          "source": """
            int size = ctx.myIntegerArray.size();
            int i=0;
            Set myset = new HashSet();
            for(ListIterator it = ctx.myIntegerArray.listIterator(); it.hasNext();)
            {
              i = it.next();
              it.remove();
              myset.add(i);

            }
            ctx.myIntegerArray.addAll(myset);
            """
        }
   }]
}

请注意,我已经创建了此管道来删除重复的整数。您可以类似的方式使用POST _reindex { "source": { "index": "sourceindex" }, "dest": { "index": "destinationindex", "pipeline": "my-pipeline" } } 。唯一需要做的更改是,将行String更改为int i=0(很抱歉此处的编码标准),并将String i = null;更改为myIntegerArray

希望对您有帮助!

相关问题