# Problem with preIndex in IndexingOperationListener (Changes won't take affect)

**URL:** <https://discuss.elastic.co/t/problem-with-preindex-in-indexingoperationlistener-changes-wont-take-affect/13597>\
**Category:** Elasticsearch\
**Created:** [September 13, 2013, 3:23pm UTC](https://discuss.elastic.co/t/problem-with-preindex-in-indexingoperationlistener-changes-wont-take-affect/13597 "2013-09-13T15:23:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jan\_Hendrik\_Lendholt](https://avatars.discourse-cdn.com/v4/letter/j/b4bc9f/32.png) [@Jan\_Hendrik\_Lendholt](https://discuss.elastic.co/u/Jan_Hendrik_Lendholt)\
**Post date:** [September 13, 2013, 3:23pm UTC](https://discuss.elastic.co/t/problem-with-preindex-in-indexingoperationlistener-changes-wont-take-affect/13597/1 "2013-09-13T15:23:27Z")

</div>

Hi,

I am still working on my maintenance task mentioned  
here: [https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/Yby4DQFgIRw](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/Yby4DQFgIRw)

I additionally added a IndexingOperationListener that listens to the  
preIndex action.

My question is: How can I manipulate the documents? There is an  
index.docs() method. For instance, I fetch the first (and only) document,  
add a field and then want it to be persisted, but unfortunately, the field  
won't get set / or removed.

Example:

indexingService.addListener(new IndexingOperationListener() {

@Override

public Index preIndex(Engine.Index index) {

List docs = index.docs();

List finalDocs = new ArrayList();

```
for (Document d : docs) {

```

d.removeField("field1");

finalDocs.add(d);

}

index.docs().clear();

index.docs().addAll(finalDocs);

return index;

}

});

Anything I do wrong?

Thanks,

Jan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![javanna](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/javanna/32/4698_2.png) [@javanna](https://discuss.elastic.co/u/javanna)\
**Post date:** [September 16, 2013, 10:48am UTC](https://discuss.elastic.co/t/problem-with-preindex-in-indexingoperationlistener-changes-wont-take-affect/13597/2 "2013-09-16T10:48:43Z")

</div>

Hi,  
although the `IndexingOperationListener` wasn't meant to modify documents,  
the code looks good. The only thing is that you're only modifying the  
lucene document that gets indexed, which won't contain the field you are  
removing. If you don't want it in the source either you have to modify the  
source too. But I think even though you see the field in your docs, you  
can't actually search on it, since it's not in lucene. If you can search on  
it and get results, then you need to make sure that your custom code  
actually runs.

Cheers  
Luca

On Friday, September 13, 2013 5:23:27 PM UTC+2, Jan-Hendrik Lendholt wrote:

> Hi,
> 
> I am still working on my maintenance task mentioned here:  
> [Redirecting to Google Groups](https://groups.google.com/forum/?fromgroups#!topic/elasticsearch/Yby4DQFgIRw)
> 
> I additionally added a IndexingOperationListener that listens to the  
> preIndex action.
> 
> My question is: How can I manipulate the documents? There is an  
> index.docs() method. For instance, I fetch the first (and only) document,  
> add a field and then want it to be persisted, but unfortunately, the field  
> won't get set / or removed.
> 
> Example:
> 
> indexingService.addListener(new IndexingOperationListener() {
> 
> @Override
> 
> public Index preIndex(Engine.Index index) {
> 
> List docs = index.docs();
> 
> List finalDocs = new ArrayList();
> 
> ```
> for (Document d : docs) {
> 
> ```
> 
> d.removeField("field1");
> 
> finalDocs.add(d);
> 
> }
> 
> index.docs().clear();
> 
> index.docs().addAll(finalDocs);
> 
> return index;
> 
> }
> 
> });
> 
> Anything I do wrong?
> 
> Thanks,
> 
> Jan

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 6, 2017, 2:16am UTC](https://discuss.elastic.co/t/problem-with-preindex-in-indexingoperationlistener-changes-wont-take-affect/13597/3 "2017-07-06T02:16:22Z")

</div>

Closed on July 6, 2017, 2:16am UTC


