我有一个具有50个用于Elasticsearch的属性的模型,并且我正在将数据传输到Elastic Search。但是,我的Elasticsearch别名中大约有150000个文档,我想使用批量部分更新来更新这些文档的3个属性。我知道分别有批量更新和部分更新,但是弹性搜索中有部分批量更新吗?
答案 0 :(得分:2)
您可以使用批量API发送部分更新。这是一个例子
<Style TargetType="{x:Type Custom_Control:SwipableListViewItem}">
<Setter Property="SnapsToDevicePixels" Value="true"/>
<Setter Property="Background" Value="Transparent"/>
<Setter Property="IsDeletePossible" Value="{Binding IsDeletePossible, RelativeSource={RelativeSource AncestorType={x:Type Custom_Control:SwipableListView}}}"/>
<Setter Property="FocusVisualStyle" Value="{x:Null}"/>
<Setter Property="OverridesDefaultStyle" Value="true"/>
<Setter Property="Background" Value="{StaticResource Brush_ContainerFill}"/>
<Setter Property="MinHeight" Value="48"/>
<Setter Property="BorderBrush" Value="{StaticResource Brush_ContainerBorder}"/>
<Setter Property="Foreground" Value="{StaticResource Brush_FontReadonly}"/>
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<Setter Property="VerticalContentAlignment" Value="Center"/>
<Setter Property="BorderThickness" Value="1,0,1,1"/>
<Setter Property="Padding" Value="16,2" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Custom_Control:SwipableListViewItem}">
<Grid>
<Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
<ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
</Border>
<Button Width="120" HorizontalAlignment="Right" Margin="0,5,5,5" x:Name="PART_DeleteButton" Content="{TemplateBinding ButtonText}" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="true">
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Brush_ActionBackground}"/>
<Setter Property="Foreground" Value="White"/>
</Trigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="Selector.IsSelectionActive" Value="false"/>
</MultiTrigger.Conditions>
<Setter Property="Background" TargetName="Bd" Value="{StaticResource Brush_ActionBackground}"/>
<Setter Property="Foreground" Value="White"/>
</MultiTrigger>
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsSelected" Value="true"/>
<Condition Property="IsSelectedForDelete" Value="true"/>
</MultiTrigger.Conditions>
<Setter Property="Visibility" TargetName="PART_DeleteButton" Value="Visible"/>
</MultiTrigger>
<Trigger Property="IsEnabled" Value="false">
<Setter Property="Foreground" Value="{StaticResource Brush_FontReadonly}"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
最终搜索响应返回
private static void Main()
{
var defaultIndex = "documents";
var pool = new SingleNodeConnectionPool(new Uri("http://localhost:9200"));
var settings = new ConnectionSettings(pool)
.DefaultIndex(defaultIndex);
var client = new ElasticClient(settings);
if (client.IndexExists(defaultIndex).Exists)
client.DeleteIndex(defaultIndex);
var docs = Enumerable.Range(1, 10).Select(i => new MyDocument(i)
{
Message = $"message {i}"
});
// bulk index the documents
var bulkResponse = client.Bulk(b => b
.IndexMany(docs)
.Refresh(Refresh.WaitFor)
);
var searchResponse = client.Search<MyDocument>(s => s
.Sort(so => so.Ascending("_id"))
);
// update the documents
bulkResponse = client.Bulk(b => b
.UpdateMany<MyDocument, object>(docs, (bu, doc) =>
{
if (doc.Id % 3 == 0)
{
// use script to update
bu.Id(doc.Id).Script(s => s
.Source("ctx._source.message = 'message ' + (Integer.parseInt(ctx._id) * 2);")
);
}
else if (doc.Id % 2 == 0)
{
// use partial document to update
bu.Id(doc.Id).Doc(new { message = "updated message" });
}
else
{
// send the original document to update
bu.Doc(doc);
}
return bu;
})
.Refresh(Refresh.WaitFor)
);
searchResponse = client.Search<MyDocument>(s => s
.Sort(so => so.Ascending("_id"))
);
}
public class MyDocument
{
public MyDocument(int id) => Id = id;
public int Id { get; set; }
public string Message { get; set; }
}
观察源文档已更新
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : 10,
"max_score" : null,
"hits" : [
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "1",
"_score" : null,
"_source" : {
"id" : 1,
"message" : "message 1"
},
"sort" : [
"1"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "10",
"_score" : null,
"_source" : {
"id" : 10,
"message" : "updated message"
},
"sort" : [
"10"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "2",
"_score" : null,
"_source" : {
"id" : 2,
"message" : "updated message"
},
"sort" : [
"2"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "3",
"_score" : null,
"_source" : {
"id" : 3,
"message" : "message 6"
},
"sort" : [
"3"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "4",
"_score" : null,
"_source" : {
"id" : 4,
"message" : "updated message"
},
"sort" : [
"4"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "5",
"_score" : null,
"_source" : {
"id" : 5,
"message" : "message 5"
},
"sort" : [
"5"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "6",
"_score" : null,
"_source" : {
"id" : 6,
"message" : "message 12"
},
"sort" : [
"6"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "7",
"_score" : null,
"_source" : {
"id" : 7,
"message" : "message 7"
},
"sort" : [
"7"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "8",
"_score" : null,
"_source" : {
"id" : 8,
"message" : "updated message"
},
"sort" : [
"8"
]
},
{
"_index" : "documents",
"_type" : "mydocument",
"_id" : "9",
"_score" : null,
"_source" : {
"id" : 9,
"message" : "message 18"
},
"sort" : [
"9"
]
}
]
}
}
被3整除的文档已使用脚本更新来更新文档_id
被2整除的文档已使用部分更新来更新文档。_id
。