# Custom IndexSearcher Implementation

**URL:** <https://discuss.elastic.co/t/custom-indexsearcher-implementation/140522>\
**Category:** Elasticsearch\
**Created:** [July 18, 2018, 11:14am UTC](https://discuss.elastic.co/t/custom-indexsearcher-implementation/140522 "2018-07-18T11:14:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vincent.arnaud90](https://avatars.discourse-cdn.com/v4/letter/v/c89c15/32.png) [@vincent.arnaud90](https://discuss.elastic.co/u/vincent.arnaud90)\
**Post date:** [July 18, 2018, 11:14am UTC](https://discuss.elastic.co/t/custom-indexsearcher-implementation/140522/1 "2018-07-18T11:14:46Z")

</div>

**Elasticsearch version (bin/elasticsearch --version)** :  
Version: 6.1.1, Build: bd92e7f/2017-12-17T20:23:25.338Z, JVM: 1.8.0\_144

**Plugin Installed :**  
None

**JVM version (java -version):**  
java version "1.8.0\_144"  
Java(TM) SE Runtime Environment (build 1.8.0\_144-b01)  
Java HotSpot(TM) 64-Bit Server VM (build 25.144-b01, mixed mode)

**OS version (uname -a if on a Unix-like system):**  
Debian 8

**Description of the problem including expected versus actual behavior:**

Hi,  
I am developping a plugin to apply a custom search function that searches based on some internal criteria.  
In order to achieve this, I override the **onIndexModule(IndexModule)** method to add my own IndexSearcherWrapper.  
My IndexSearcherWrapper overrides **protected void search(List leaves, Weight weight, Collector collector) throws IOException** method, but when testing, I find that this method is never called.  
Am I doing something wrong or is this the same issue that has already been reported at [https://github.com/elastic/elasticsearch/issues/30758](https://github.com/elastic/elasticsearch/issues/30758) ?

```
public class CustomPlugin extends Plugin implements ActionPlugin{
   @Override
   public void onIndexModule(IndexModule module) {
	 module.setSearcherWrapper( indexService -> new CustomIndexSearcherWrapper(indexService));
   }
}

public class CustomIndexSearcherWrapper extends IndexSearcherWrapper {
    private final IndexSettings indexSettings; 

    public CustomIndexSearcherWrapper(IndexService indexService){
	  indexSettings= indexService.getIndexSettings();
    }

    @Override
    protected IndexSearcher wrap(IndexSearcher searcher) throws IOException {
   		final DirectoryReader directoryReader = (DirectoryReader) searcher.getIndexReader();
	    if ( directoryReader!=null) {
		   IndexSearcher indexSearcher = new CustomIndexSearcher(directoryReader);
		   indexSearcher.setQueryCache(indexSearcher.getQueryCache());
		   indexSearcher.setQueryCachingPolicy(indexSearcher.getQueryCachingPolicy());
		   indexSearcher.setSimilarity(indexSearcher.getSimilarity(true));
		   return indexSearcher;
	    }
	   return searcher;
	 }

    static class CustomIndexSearcher extends IndexSearcher {
	   CustomIndexSearcher(DirectoryReader r) {
         super(r);
       }
	
	   @Override
       protected void search(List<LeafReaderContext> leaves, Weight weight, Collector collector) throws IOException {
		//my own search logic but this method is never called 			
	   }
   }
}

```

**Steps to reproduce:**  
None

**Provide logs (if relevant):**  
None

Thanks for your help.

---

<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:** [August 15, 2018, 11:14am UTC](https://discuss.elastic.co/t/custom-indexsearcher-implementation/140522/2 "2018-08-15T11:14:52Z")

</div>

Closed on August 15, 2018, 11:14am UTC

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
