MTFulltextQueryTermsProvider implements FulltextQueryTermsProvider { @Override public Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition) { BooleanQuery query = new BooleanQuery(); try { Sentence sentence = new Sentence(text, text.hashCode(), decoder.getJoshuaConfiguration()); Translation translation = decoder.decode(sentence); log.debug("{} decoded into {}", text, translation); query.add(new BooleanClause(new TermQuery(new Term(FieldNames.FULLTEXT, translation.toString())), BooleanClause.Occur.SHOULD)); List<StructuredTranslation> structuredTranslations = translation.getStructuredTranslations(); log.debug("found {} structured translations", structuredTranslations.size()); if (!structuredTranslations.isEmpty()) { log.debug("phrase translation"); addTranslations(query, structuredTranslations); } else { log.debug("per token translation"); TokenStream tokenStream = analyzer.tokenStream(null, new StringReader(text)); tokenStream.addAttribute(CharTermAttribute.class); tokenStream.reset(); while (tokenStream.incrementToken()) { CharTermAttribute attribute = tokenStream.getAttribute(CharTermAttribute.class); String source = attribute.toString(); Translation translatedToken = decoder.decode(new Sentence(source, source.hashCode(), decoder.getJoshuaConfiguration())); addTranslations(query, translatedToken.getStructuredTranslations()); } tokenStream.end(); } } catch (Exception e) { log.error("could not translate query", e); } return query.clauses().size() > 0 ? query : null; } MTFulltextQueryTermsProvider(Decoder decoder, Set<String> nodeTypes, float minScore); @Override Query getQueryTerm(String text, Analyzer analyzer, NodeState indexDefinition); void clearResources(); @NotNull @Override Set<String> getSupportedTypes();  }
@Test public void testGetQueryTermWithPhraseTranslation() throws Exception { Decoder decoder = mock(Decoder.class); Translation translation = mock(Translation.class); List<StructuredTranslation> translations = new LinkedList<>(); StructuredTranslation structuredTranslation = mock(StructuredTranslation.class); when(structuredTranslation.getTranslationString()).thenReturn("fou bur"); translations.add(structuredTranslation); when(translation.getStructuredTranslations()).thenReturn(translations); when(decoder.decode(any(Sentence.class))).thenReturn(translation); JoshuaConfiguration configuration = mock(JoshuaConfiguration.class); when(decoder.getJoshuaConfiguration()).thenReturn(configuration); Set<String> nodeTypes = new HashSet<>(); MTFulltextQueryTermsProvider mtFulltextQueryTermsProvider = new MTFulltextQueryTermsProvider(decoder, nodeTypes, -1); Analyzer analyzer = mock(Analyzer.class); NodeState indexDefinition = mock(NodeState.class); mtFulltextQueryTermsProvider.getQueryTerm("foo bar", analyzer, indexDefinition); }