### Question:
ProspectiveSearchAE extends LuceneDocumentAE { @Override public void process(CAS aCAS) throws AnalysisEngineProcessException { MemoryIndex index = new MemoryIndex(); List fields = createDocument(aCAS).getFields(); for (Iterator it = fields.iterator(); it.hasNext(); ) { Field field = (Field) it.next(); if (field.isIndexed() && field.tokenStreamValue() != null) { index.addField(field.name(), field.tokenStreamValue()); } } for (SearchQuery query : searchQueryProvider.getSearchQueries(aCAS)) { float score = index.search(query.query()); if (score > matchingThreshold) { FeatureStructure searchResult = aCAS.createFS(searchResultType); searchResult.setLongValue(searchResultIdFeature, query.id()); aCAS.addFsToIndexes(searchResult); if (searchResultMatchingTextFeature != null) { fields = createDocument(aCAS).getFields(); for (Iterator it = fields.iterator(); it.hasNext(); ) { Field field = (Field) it.next(); if (field.isIndexed() && field.tokenStreamValue() != null) { TokenStream tokenStream = field.tokenStreamValue(); Collection<AnnotationFS> matchingTextAnnotations = new LinkedList<AnnotationFS>(); QueryScorer scorer = new QueryScorer(query.query(), field.name()); scorer.startFragment(new TextFragment( new StringBuffer(aCAS.getDocumentText()), 0, 0)); try { scorer.init(tokenStream); OffsetAttribute offsetAttr = null; while (tokenStream.incrementToken()) { offsetAttr = (OffsetAttribute)tokenStream.getAttribute(OffsetAttribute.class); float tokenScore = scorer.getTokenScore(); if (tokenScore > 0) { AnnotationFS annotation = aCAS.createAnnotation(matchingTextType, offsetAttr.startOffset(), offsetAttr.endOffset()); matchingTextAnnotations.add(annotation); } } } catch (IOException e) { throw new AnalysisEngineProcessException(e); } ArrayFS matchtingTextArray = aCAS.createArrayFS(matchingTextAnnotations.size()); int matchtingTextArrayIndex = 0; for (AnnotationFS matchingTextAnnotation: matchingTextAnnotations) { matchtingTextArray.set(matchtingTextArrayIndex++, matchingTextAnnotation); } searchResult.setFeatureValue(searchResultMatchingTextFeature, matchtingTextArray); } } } } } }  @Override void initialize(UimaContext aContext); @Override void typeSystemInit(TypeSystem aTypeSystem); @Override void process(CAS aCAS);  }### Answer:
@Test public void testSimpleSearch() throws ResourceInitializationException, AnalysisEngineProcessException { CAS cas = searchAE.newCAS(); cas.setDocumentText("The quick brown fox jumps over the lazy dog"); searchAE.process(cas); FSIterator<FeatureStructure> searchResults = cas.getIndexRepository().getAllIndexedFS(searchResultType); assertTrue(searchResults.hasNext()); FeatureStructure result = searchResults.next(); assertEquals(0, result.getLongValue(searchResultIdFeature)); ArrayFS matchingTextArray = (ArrayFS) result.getFeatureValue(searchResultMatchingTextFeature); assertEquals(2, matchingTextArray.size()); for (int i = 0; i < matchingTextArray.size(); i++) { AnnotationFS annotation = (AnnotationFS) matchingTextArray.get(i); if (i == 0) { assertEquals(4, annotation.getBegin()); assertEquals(9, annotation.getEnd()); } else if (i == 1) { assertEquals(16, annotation.getBegin()); assertEquals(19, annotation.getEnd()); } else { fail(); } } }