Skip to content

Commit 66b86a7

Browse files
committed
Optimize API: Add onlyExpungeDeletes, flush and refresh parameters. Closes #15.
1 parent 9633108 commit 66b86a7

File tree

6 files changed

+151
-8
lines changed

6 files changed

+151
-8
lines changed

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/optimize/OptimizeRequest.java

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ public class OptimizeRequest extends BroadcastOperationRequest {
4444

4545
private int maxNumSegments = -1;
4646

47+
private boolean onlyExpungeDeletes = false;
48+
49+
private boolean flush = false;
50+
51+
private boolean refresh = false;
52+
4753
/**
4854
* Constructs an optimization request over one or more indices.
4955
*
@@ -101,15 +107,68 @@ public OptimizeRequest maxNumSegments(int maxNumSegments) {
101107
return this;
102108
}
103109

110+
/**
111+
* Should the optimization only expunge deletes from the index, without full optimization.
112+
* Defaults to full optimization (<tt>false</tt>).
113+
*/
114+
public boolean onlyExpungeDeletes() {
115+
return onlyExpungeDeletes;
116+
}
117+
118+
/**
119+
* Should the optimization only expunge deletes from the index, without full optimization.
120+
* Defaults to full optimization (<tt>false</tt>).
121+
*/
122+
public OptimizeRequest onlyExpungeDeletes(boolean onlyExpungeDeletes) {
123+
this.onlyExpungeDeletes = onlyExpungeDeletes;
124+
return this;
125+
}
126+
127+
/**
128+
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
129+
*/
130+
public boolean flush() {
131+
return flush;
132+
}
133+
134+
/**
135+
* Should flush be performed after the optimization. Defaults to <tt>false</tt>.
136+
*/
137+
public OptimizeRequest flush(boolean flush) {
138+
this.flush = flush;
139+
return this;
140+
}
141+
142+
/**
143+
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
144+
*/
145+
public boolean refresh() {
146+
return refresh;
147+
}
148+
149+
/**
150+
* Should refresh be performed after the optimization. Defaults to <tt>false</tt>.
151+
*/
152+
public OptimizeRequest refresh(boolean refresh) {
153+
this.refresh = refresh;
154+
return this;
155+
}
156+
104157
public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
105158
super.readFrom(in);
106159
waitForMerge = in.readBoolean();
107160
maxNumSegments = in.readInt();
161+
onlyExpungeDeletes = in.readBoolean();
162+
flush = in.readBoolean();
163+
refresh = in.readBoolean();
108164
}
109165

110166
public void writeTo(DataOutput out) throws IOException {
111167
super.writeTo(out);
112168
out.writeBoolean(waitForMerge);
113169
out.writeInt(maxNumSegments);
170+
out.writeBoolean(onlyExpungeDeletes);
171+
out.writeBoolean(flush);
172+
out.writeBoolean(refresh);
114173
}
115174
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/optimize/ShardOptimizeRequest.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,22 @@ public class ShardOptimizeRequest extends BroadcastShardOperationRequest {
3434

3535
private int maxNumSegments = -1;
3636

37+
private boolean onlyExpungeDeletes = false;
38+
39+
private boolean flush = false;
40+
41+
private boolean refresh = false;
42+
3743
ShardOptimizeRequest() {
3844
}
3945

4046
public ShardOptimizeRequest(String index, int shardId, OptimizeRequest request) {
4147
super(index, shardId);
4248
waitForMerge = request.waitForMerge();
4349
maxNumSegments = request.maxNumSegments();
50+
onlyExpungeDeletes = request.onlyExpungeDeletes();
51+
flush = request.flush();
52+
refresh = request.refresh();
4453
}
4554

4655
boolean waitForMerge() {
@@ -51,15 +60,33 @@ int maxNumSegments() {
5160
return maxNumSegments;
5261
}
5362

63+
public boolean onlyExpungeDeletes() {
64+
return onlyExpungeDeletes;
65+
}
66+
67+
public boolean flush() {
68+
return flush;
69+
}
70+
71+
public boolean refresh() {
72+
return refresh;
73+
}
74+
5475
@Override public void readFrom(DataInput in) throws IOException, ClassNotFoundException {
5576
super.readFrom(in);
5677
waitForMerge = in.readBoolean();
5778
maxNumSegments = in.readInt();
79+
onlyExpungeDeletes = in.readBoolean();
80+
flush = in.readBoolean();
81+
refresh = in.readBoolean();
5882
}
5983

6084
@Override public void writeTo(DataOutput out) throws IOException {
6185
super.writeTo(out);
6286
out.writeBoolean(waitForMerge);
6387
out.writeInt(maxNumSegments);
88+
out.writeBoolean(onlyExpungeDeletes);
89+
out.writeBoolean(flush);
90+
out.writeBoolean(refresh);
6491
}
6592
}

modules/elasticsearch/src/main/java/org/elasticsearch/action/admin/indices/optimize/TransportOptimizeAction.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,13 @@ public class TransportOptimizeAction extends TransportBroadcastOperationAction<O
8686

8787
@Override protected ShardOptimizeResponse shardOperation(ShardOptimizeRequest request) throws ElasticSearchException {
8888
IndexShard indexShard = indicesService.indexServiceSafe(request.index()).shardSafe(request.shardId());
89-
indexShard.optimize(new Engine.Optimize(request.waitForMerge(), request.maxNumSegments()));
89+
indexShard.optimize(new Engine.Optimize()
90+
.waitForMerge(request.waitForMerge())
91+
.maxNumSegments(request.maxNumSegments())
92+
.onlyExpungeDeletes(request.onlyExpungeDeletes())
93+
.flush(request.flush())
94+
.refresh(request.refresh())
95+
);
9096
return new ShardOptimizeResponse(request.index(), request.shardId());
9197
}
9298

modules/elasticsearch/src/main/java/org/elasticsearch/http/action/admin/indices/optimize/HttpOptimizeAction.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ public class HttpOptimizeAction extends BaseHttpServerHandler {
5151
try {
5252
optimizeRequest.waitForMerge(HttpActions.paramAsBoolean(request.param("waitForMerge"), true));
5353
optimizeRequest.maxNumSegments(HttpActions.paramAsInt(request.param("maxNumSegments"), -1));
54+
optimizeRequest.onlyExpungeDeletes(HttpActions.paramAsBoolean(request.param("onlyExpungeDeletes"), false));
55+
optimizeRequest.flush(HttpActions.paramAsBoolean(request.param("flush"), false));
56+
optimizeRequest.refresh(HttpActions.paramAsBoolean(request.param("refresh"), false));
5457

5558
// we just send back a response, no need to fork a listener
5659
optimizeRequest.listenerThreaded(false);

modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/Engine.java

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -162,24 +162,62 @@ public Flush refresh(boolean refresh) {
162162
}
163163

164164
static class Optimize {
165-
private final boolean waitForMerge;
166-
private final int maxNumSegments;
165+
private boolean waitForMerge = true;
166+
private int maxNumSegments = -1;
167+
private boolean onlyExpungeDeletes = false;
168+
private boolean flush = false;
169+
private boolean refresh = false;
167170

168-
public Optimize(boolean waitForMerge, int maxNumSegments) {
169-
this.waitForMerge = waitForMerge;
170-
this.maxNumSegments = maxNumSegments;
171+
public Optimize() {
171172
}
172173

173174
public boolean waitForMerge() {
174175
return waitForMerge;
175176
}
176177

178+
public Optimize waitForMerge(boolean waitForMerge) {
179+
this.waitForMerge = waitForMerge;
180+
return this;
181+
}
182+
177183
public int maxNumSegments() {
178184
return maxNumSegments;
179185
}
180186

187+
public Optimize maxNumSegments(int maxNumSegments) {
188+
this.maxNumSegments = maxNumSegments;
189+
return this;
190+
}
191+
192+
public boolean onlyExpungeDeletes() {
193+
return onlyExpungeDeletes;
194+
}
195+
196+
public Optimize onlyExpungeDeletes(boolean onlyExpungeDeletes) {
197+
this.onlyExpungeDeletes = onlyExpungeDeletes;
198+
return this;
199+
}
200+
201+
public boolean flush() {
202+
return flush;
203+
}
204+
205+
public Optimize flush(boolean flush) {
206+
this.flush = flush;
207+
return this;
208+
}
209+
210+
public boolean refresh() {
211+
return refresh;
212+
}
213+
214+
public Optimize refresh(boolean refresh) {
215+
this.refresh = refresh;
216+
return this;
217+
}
218+
181219
@Override public String toString() {
182-
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "]";
220+
return "waitForMerge[" + waitForMerge + "], maxNumSegments[" + maxNumSegments + "], onlyExpungeDeletes[" + onlyExpungeDeletes + "], flush[" + flush + "], refresh[" + refresh + "]";
183221
}
184222
}
185223

modules/elasticsearch/src/main/java/org/elasticsearch/index/engine/robin/RobinEngine.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -309,14 +309,24 @@ public class RobinEngine extends AbstractIndexShardComponent implements Engine,
309309
}
310310
}
311311
}
312-
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
312+
if (optimize.onlyExpungeDeletes()) {
313+
indexWriter.expungeDeletes(optimize.waitForMerge());
314+
} else {
315+
indexWriter.optimize(maxNumberOfSegments, optimize.waitForMerge());
316+
}
313317
} catch (Exception e) {
314318
throw new OptimizeFailedEngineException(shardId, e);
315319
} finally {
316320
rwl.readLock().unlock();
317321
optimizeMutex.set(false);
318322
}
319323
}
324+
if (optimize.flush()) {
325+
flush(new Flush());
326+
}
327+
if (optimize.refresh()) {
328+
refresh(new Refresh(false));
329+
}
320330
}
321331

322332
@Override public void snapshot(SnapshotHandler snapshotHandler) throws EngineException {

0 commit comments

Comments
 (0)