org.apache.lucene.index.Term Java Examples

The following examples show how to use org.apache.lucene.index.Term. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TestRedisQParser.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldAddTermsFromSortOrderDesc() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("sort");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.get("order")).thenReturn("desc");
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(jedisMock.sort(anyString(), any(SortingParams.class))).thenReturn(Arrays.asList("123", "321"));
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new StandardAnalyzer());
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  final ArgumentCaptor<SortingParams> argument = ArgumentCaptor.forClass(SortingParams.class);
  verify(jedisMock).sort(eq("simpleKey"), argument.capture());
  Assert.assertEquals(getSortingParamString(new SortingParams().desc()), getSortingParamString(argument.getValue()));
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(2, terms.size());
}
 
Example #2
Source File: TestSpanSearchEquivalence.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** SpanNearQuery([A B C], N, false) ⊆ SpanNearQuery([A B C], N+1, false) */
public void testSpanNearIncreasingSloppiness3() throws Exception {
  Term t1 = randomTerm();
  Term t2 = randomTerm();
  Term t3 = randomTerm();
  SpanQuery subquery[] = new SpanQuery[] { 
                           spanQuery(new SpanTermQuery(t1)), 
                           spanQuery(new SpanTermQuery(t2)), 
                           spanQuery(new SpanTermQuery(t3)) 
                         };
  for (int i = 0; i < 10; i++) {
    SpanQuery q1 = spanQuery(new SpanNearQuery(subquery, i, false));
    SpanQuery q2 = spanQuery(new SpanNearQuery(subquery, i+1, false));
    assertSubsetOf(q1, q2);
  }
}
 
Example #3
Source File: LuceneCorpusAdapter.java    From Palmetto with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public void getDocumentsWithWordAsSet(String word, IntOpenHashSet documents) {
    DocsEnum docs = null;
    Term term = new Term(fieldName, word);
    try {
        int baseDocId;
        for (int i = 0; i < reader.length; i++) {
            docs = reader[i].termDocsEnum(term);
            baseDocId = contexts[i].docBase;
            if (docs != null) {
                while (docs.nextDoc() != DocsEnum.NO_MORE_DOCS) {
                    documents.add(baseDocId + docs.docID());
                }
            }
        }
    } catch (IOException e) {
        LOGGER.error("Error while requesting documents for word \"" + word + "\".", e);
    }
}
 
Example #4
Source File: PhraseCountQueryBuilder.java    From pyramid with Apache License 2.0 6 votes vote down vote up
protected Query doToQuery(QueryShardContext context) throws IOException {
//        Analyzer analyzer = context.getMapperService().searchAnalyzer();
        Analyzer analyzer = new WhitespaceAnalyzer();
        try (TokenStream source = analyzer.tokenStream(fieldName, value.toString())) {
            CachingTokenFilter stream = new CachingTokenFilter(new LowerCaseFilter(source));
            TermToBytesRefAttribute termAtt = stream.getAttribute(TermToBytesRefAttribute.class);
            if (termAtt == null) {
                return null;
            }
            List<CustomSpanTermQuery> clauses = new ArrayList<>();
            stream.reset();
            while (stream.incrementToken()) {
                Term term = new Term(fieldName, termAtt.getBytesRef());
                    clauses.add(new CustomSpanTermQuery(term));
            }
            return new PhraseCountQuery(clauses.toArray(new CustomSpanTermQuery[clauses.size()]), slop, inOrder, weightedCount);
        } catch (IOException e) {
            throw new RuntimeException("Error analyzing query text", e);
        }


    }
 
Example #5
Source File: TestRedisQParser.java    From solr-redis with Apache License 2.0 6 votes vote down vote up
@Test
public void shouldTurnAnalysisOn() throws SyntaxError, IOException {
  when(localParamsMock.get("command")).thenReturn("smembers");
  when(localParamsMock.get("key")).thenReturn("simpleKey");
  when(localParamsMock.getBool("useAnalyzer", false)).thenReturn(true);
  when(localParamsMock.get(QueryParsing.V)).thenReturn("string_field");
  when(requestMock.getSchema()).thenReturn(schema);
  when(schema.getQueryAnalyzer()).thenReturn(new WhitespaceAnalyzer());
  when(jedisMock.smembers(anyString())).thenReturn(new HashSet<>(Arrays.asList("123 124", "321")));
  redisQParser = new RedisQParser("string_field", localParamsMock, paramsMock, requestMock, commandHandler);
  final Query query = redisQParser.parse();
  verify(jedisMock).smembers("simpleKey");
  IndexSearcher searcher = new IndexSearcher(new MultiReader());
  final Set<Term> terms = extractTerms(searcher, query);
  Assert.assertEquals(3, terms.size());
}
 
Example #6
Source File: DocumentAndOp.java    From RDFS with Apache License 2.0 6 votes vote down vote up
/**
 * Constructor for an insert, a delete or an update operation.
 * @param op
 * @param doc
 * @param term
 */
public DocumentAndOp(Op op, Document doc, Term term) {
  if (op == Op.INSERT) {
    assert (doc != null);
    assert (term == null);
  } else if (op == Op.DELETE) {
    assert (doc == null);
    assert (term != null);
  } else {
    assert (op == Op.UPDATE);
    assert (doc != null);
    assert (term != null);
  }
  this.op = op;
  this.doc = doc;
  this.term = term;
}
 
Example #7
Source File: TestSimilarity2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** similar to the above, however the field exists, but we query with a term that doesnt exist too */
public void testEmptyTerm() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  doc.add(newTextField("foo", "bar", Field.Store.NO));
  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher is = newSearcher(ir);
  
  for (Similarity sim : sims) {
    is.setSimilarity(sim);
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    query.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
    query.add(new TermQuery(new Term("foo", "baz")), BooleanClause.Occur.SHOULD);
    assertEquals(1, is.search(query.build(), 10).totalHits.value);
  }
  ir.close();
  dir.close();
}
 
Example #8
Source File: TestSynonymQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testEquals() {
  QueryUtils.checkEqual(new SynonymQuery.Builder("foo").build(), new SynonymQuery.Builder("foo").build());
  QueryUtils.checkEqual(new SynonymQuery.Builder("foo").addTerm(new Term("foo", "bar")).build(),
                        new SynonymQuery.Builder("foo").addTerm(new Term("foo", "bar")).build());

  QueryUtils.checkEqual(new SynonymQuery.Builder("a").addTerm(new Term("a", "a")).addTerm(new Term("a", "b")).build(),
                        new SynonymQuery.Builder("a").addTerm(new Term("a", "b")).addTerm(new Term("a", "a")).build());

  QueryUtils.checkEqual(
      new SynonymQuery.Builder("field")
          .addTerm(new Term("field", "b"), 0.4f)
          .addTerm(new Term("field", "c"), 0.2f)
          .addTerm(new Term("field", "d")).build(),
      new SynonymQuery.Builder("field")
          .addTerm(new Term("field", "b"), 0.4f)
          .addTerm(new Term("field", "c"), 0.2f)
          .addTerm(new Term("field", "d")).build());

}
 
Example #9
Source File: IndexManager.java    From spacewalk with GNU General Public License v2.0 6 votes vote down vote up
private void debugExplainResults(String indexName, Hits hits, IndexSearcher searcher,
        Query q, Set<Term> queryTerms)
    throws IOException {
    log.debug("Parsed Query is " + q.toString());
    log.debug("Looking at index:  " + indexName);
    for (int i = 0; i < hits.length(); i++) {
        if ((i < 10)) {
            Document doc = hits.doc(i);
            Float score = hits.score(i);
            Explanation ex = searcher.explain(q, hits.id(i));
            log.debug("Looking at hit<" + i + ", " + hits.id(i) + ", " + score +
                    ">: " + doc);
            log.debug("Explanation: " + ex);
            MatchingField match = new MatchingField(q.toString(), doc, queryTerms);
            String fieldName = match.getFieldName();
            String fieldValue = match.getFieldValue();
            log.debug("Guessing that matched fieldName is " + fieldName + " = " +
                    fieldValue);
        }
    }
}
 
Example #10
Source File: TestPhraseWildcardQuery.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
protected Term[] expandMultiTerm(String field, String term, int maxExpansions) throws IOException {
  if (maxExpansions == 0) {
    return new Term[0];
  }
  Set<Term> expansions = new HashSet<>();
  WildcardQuery wq = new WildcardQuery(new Term(field, term));
  expansion:
  for (final LeafReaderContext ctx : reader.leaves()) {
    Terms terms = ctx.reader().terms(field);
    if (terms != null) {
      TermsEnum termsEnum = wq.getTermsEnum(terms);
      while (termsEnum.next() != null) {
        expansions.add(new Term(field, termsEnum.term()));
        if (expansions.size() >= maxExpansions) {
          break expansion;
        }
      }
    }
  }
  return expansions.toArray(new Term[0]);
}
 
Example #11
Source File: SimplePrimaryNode.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void verifyAtLeastMarkerCount(int expectedAtLeastCount, DataOutput out) throws IOException {
  IndexSearcher searcher = mgr.acquire();
  try {
    long version = ((DirectoryReader) searcher.getIndexReader()).getVersion();
    int hitCount = searcher.count(new TermQuery(new Term("marker", "marker")));

    if (hitCount < expectedAtLeastCount) {
      message("marker search: expectedAtLeastCount=" + expectedAtLeastCount + " but hitCount=" + hitCount);
      TopDocs hits = searcher.search(new TermQuery(new Term("marker", "marker")), expectedAtLeastCount);
      List<Integer> seen = new ArrayList<>();
      for(ScoreDoc hit : hits.scoreDocs) {
        Document doc = searcher.doc(hit.doc);
        seen.add(Integer.parseInt(doc.get("docid").substring(1)));
      }
      Collections.sort(seen);
      message("saw markers:");
      for(int marker : seen) {
        message("saw m" + marker);
      }
      throw new IllegalStateException("at flush: marker count " + hitCount + " but expected at least " + expectedAtLeastCount + " version=" + version);
    }

    if (out != null) {
      out.writeVLong(version);
      out.writeVInt(hitCount);
    }
  } finally {
    mgr.release(searcher);
  }
}
 
Example #12
Source File: TestFunctionScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testSimpleSourceScore() throws Exception {

    FunctionScoreQuery q = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "first")),
        DoubleValuesSource.fromIntField(INT_FIELD));

    QueryUtils.check(random(), q, searcher, rarely());

    int expectedDocs[] = new int[]{ 4, 7, 9 };
    TopDocs docs = searcher.search(q, 4);
    assertEquals(expectedDocs.length, docs.totalHits.value);
    for (int i = 0; i < expectedDocs.length; i++) {
      assertEquals(docs.scoreDocs[i].doc, expectedDocs[i]);
    }

  }
 
Example #13
Source File: AnalyzingInfixSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** This is called if the last token isn't ended
 *  (e.g. user did not type a space after it).  Return an
 *  appropriate Query clause to add to the BooleanQuery. */
protected Query getLastTokenQuery(String token) throws IOException {
  if (token.length() < minPrefixChars) {
    // The leading ngram was directly indexed:
    return new TermQuery(new Term(TEXTGRAMS_FIELD_NAME, token));
  }

  return new PrefixQuery(new Term(TEXT_FIELD_NAME, token));
}
 
Example #14
Source File: TestBooleanTermExtractor.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testMatchAllDocsIsOnlyQuery() {
  // Set up - single MatchAllDocsQuery clause in a BooleanQuery
  Query q = MonitorTestBase.parse("+*:*");
  assertTrue(q instanceof BooleanQuery);
  BooleanClause clause = ((BooleanQuery)q).iterator().next();
  assertTrue(clause.getQuery() instanceof MatchAllDocsQuery);
  assertEquals(BooleanClause.Occur.MUST, clause.getOccur());

  Set<Term> terms = collectTerms(q);
  assertEquals(1, terms.size());
  Term t = terms.iterator().next();
  assertEquals(TermFilteredPresearcher.ANYTOKEN_FIELD, t.field());
}
 
Example #15
Source File: TestComplexExplanations.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFQ5() throws Exception {
  TermQuery query = new TermQuery(new Term(FIELD, "xx"));
  Query filtered = new BooleanQuery.Builder()
      .add(new BoostQuery(query, 0), Occur.MUST)
      .add(matchTheseItems(new int[] {1,3}), Occur.FILTER)
      .build();
  bqtest(filtered, new int[] {3});
}
 
Example #16
Source File: QueryWaitableTest.java    From semantic-knowledge-graph with Apache License 2.0 5 votes vote down vote up
@Before
public void init() throws IOException
{
    context = new NodeContext();
    query = new TermQuery(new Term("testField1", "testQuery1"));

    new Expectations() {{
        searcher.numDocs(query, docSet); returns(1);
    }};
}
 
Example #17
Source File: TestFunctionScoreQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testEqualities() {

    Query q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "a")), DoubleValuesSource.constant(1));
    Query q2 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "b")), DoubleValuesSource.constant(1));
    Query q3 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "b")), DoubleValuesSource.constant(2));
    Query q4 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "b")), DoubleValuesSource.constant(2));

    QueryUtils.check(q1);
    QueryUtils.checkUnequal(q1, q3);
    QueryUtils.checkUnequal(q1, q2);
    QueryUtils.checkUnequal(q2, q3);
    QueryUtils.checkEqual(q3, q4);

    Query bq1 = FunctionScoreQuery.boostByValue(new TermQuery(new Term(TEXT_FIELD, "a")), DoubleValuesSource.constant(2));
    QueryUtils.check(bq1);
    Query bq2 = FunctionScoreQuery.boostByValue(new TermQuery(new Term(TEXT_FIELD, "a")), DoubleValuesSource.constant(4));
    QueryUtils.checkUnequal(bq1, bq2);
    Query bq3 = FunctionScoreQuery.boostByValue(new TermQuery(new Term(TEXT_FIELD, "b")), DoubleValuesSource.constant(4));
    QueryUtils.checkUnequal(bq1, bq3);
    QueryUtils.checkUnequal(bq2, bq3);
    Query bq4 = FunctionScoreQuery.boostByValue(new TermQuery(new Term(TEXT_FIELD, "b")), DoubleValuesSource.constant(4));
    QueryUtils.checkEqual(bq3, bq4);

    Query qq1 = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(TEXT_FIELD, "a")), new TermQuery(new Term(TEXT_FIELD, "z")), 0.1f);
    QueryUtils.check(qq1);
    Query qq2 = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(TEXT_FIELD, "a")), new TermQuery(new Term(TEXT_FIELD, "z")), 0.2f);
    QueryUtils.checkUnequal(qq1, qq2);
    Query qq3 = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(TEXT_FIELD, "b")), new TermQuery(new Term(TEXT_FIELD, "z")), 0.1f);
    QueryUtils.checkUnequal(qq1, qq3);
    QueryUtils.checkUnequal(qq2, qq3);
    Query qq4 = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(TEXT_FIELD, "a")), new TermQuery(new Term(TEXT_FIELD, "zz")), 0.1f);
    QueryUtils.checkUnequal(qq1, qq4);
    QueryUtils.checkUnequal(qq2, qq4);
    QueryUtils.checkUnequal(qq3, qq4);
    Query qq5 = FunctionScoreQuery.boostByQuery(new TermQuery(new Term(TEXT_FIELD, "a")), new TermQuery(new Term(TEXT_FIELD, "z")), 0.1f);
    QueryUtils.checkEqual(qq1, qq5);

  }
 
Example #18
Source File: FieldQueryTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testFlattenTermAndPhrase2gram() throws Exception {
  BooleanQuery.Builder query = new BooleanQuery.Builder();
  query.add(new TermQuery(new Term(F, "AA")), Occur.MUST);
  query.add(toPhraseQuery(analyze("BCD", F, analyzerB), F), Occur.MUST);
  query.add(toPhraseQuery(analyze("EFGH", F, analyzerB), F), Occur.SHOULD);

  FieldQuery fq = new FieldQuery( query.build(), true, true );
  Set<Query> flatQueries = new HashSet<>();
  fq.flatten( query.build(), reader, flatQueries, 1f );
  assertCollectionQueries( flatQueries, tq( "AA" ), pqF( "BC", "CD" ), pqF( "EF", "FG", "GH" ) );
}
 
Example #19
Source File: HighlighterTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testQueryScorerMultiPhraseQueryHighlighting() throws Exception {
  MultiPhraseQuery.Builder mpqb = new MultiPhraseQuery.Builder();

  mpqb.add(new Term[] { new Term(FIELD_NAME, "wordx"), new Term(FIELD_NAME, "wordb") });
  mpqb.add(new Term(FIELD_NAME, "wordy"));

  doSearching(mpqb.build());

  final int maxNumFragmentsRequired = 2;
  assertExpectedHighlightCount(maxNumFragmentsRequired, 6);
}
 
Example #20
Source File: TestPrefixQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPrefixQuery() throws Exception {
  Directory directory = newDirectory();

  String[] categories = new String[] {"/Computers",
                                      "/Computers/Mac",
                                      "/Computers/Windows"};
  RandomIndexWriter writer = new RandomIndexWriter(random(), directory);
  for (int i = 0; i < categories.length; i++) {
    Document doc = new Document();
    doc.add(newStringField("category", categories[i], Field.Store.YES));
    writer.addDocument(doc);
  }
  IndexReader reader = writer.getReader();

  PrefixQuery query = new PrefixQuery(new Term("category", "/Computers"));
  IndexSearcher searcher = newSearcher(reader);
  ScoreDoc[] hits = searcher.search(query, 1000).scoreDocs;
  assertEquals("All documents in /Computers category and below", 3, hits.length);

  query = new PrefixQuery(new Term("category", "/Computers/Mac"));
  hits = searcher.search(query, 1000).scoreDocs;
  assertEquals("One in /Computers/Mac", 1, hits.length);

  query = new PrefixQuery(new Term("category", ""));
  hits = searcher.search(query, 1000).scoreDocs;
  assertEquals("everything", 3, hits.length);
  writer.close();
  reader.close();
  directory.close();
}
 
Example #21
Source File: LindenCoreImpl.java    From linden with Apache License 2.0 5 votes vote down vote up
public JSONObject getInputDocument(Term term) throws IOException {
  SearcherTaxonomyManager.SearcherAndTaxonomy searcherAndTaxonomy = lindenNRTSearcherManager.acquire();
  try {
    IndexSearcher indexSearcher = searcherAndTaxonomy.searcher;
    TopDocs results = indexSearcher.search(new TermQuery(term), 1);
    if (results.scoreDocs.length == 0) {
      return null;
    }
    int docId = results.scoreDocs[0].doc;
    String source = LindenUtil.getSource(indexSearcher, docId, null, null, config);
    return JSONObject.parseObject(source);
  } finally {
    lindenNRTSearcherManager.release(searcherAndTaxonomy);
  }
}
 
Example #22
Source File: TestFieldValueQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testRandom() throws IOException {
  final int iters = atLeast(10);
  for (int iter = 0; iter < iters; ++iter) {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    final int numDocs = atLeast(100);
    for (int i = 0; i < numDocs; ++i) {
      Document doc = new Document();
      final boolean hasValue = random().nextBoolean();
      if (hasValue) {
        doc.add(new NumericDocValuesField("dv1", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 1));
        doc.add(new SortedNumericDocValuesField("dv2", 2));
        doc.add(new StringField("has_value", "yes", Store.NO));
      }
      doc.add(new StringField("f", random().nextBoolean() ? "yes" : "no", Store.NO));
      iw.addDocument(doc);
    }
    if (random().nextBoolean()) {
      iw.deleteDocuments(new TermQuery(new Term("f", "no")));
    }
    iw.commit();
    final IndexReader reader = iw.getReader();
    final IndexSearcher searcher = newSearcher(reader);
    iw.close();

    assertSameMatches(searcher, new TermQuery(new Term("has_value", "yes")), new DocValuesFieldExistsQuery("dv1"), false);
    assertSameMatches(searcher, new TermQuery(new Term("has_value", "yes")), new DocValuesFieldExistsQuery("dv2"), false);

    reader.close();
    dir.close();
  }
}
 
Example #23
Source File: BlendedTermQuery.java    From crate with Apache License 2.0 5 votes vote down vote up
private Term[] equalsTerms() {
    if (terms.length == 1) {
        return terms;
    }
    if (equalTerms == null) {
        // sort the terms to make sure equals and hashCode are consistent
        // this should be a very small cost and equivalent to a HashSet but less object creation
        final Term[] t = new Term[terms.length];
        System.arraycopy(terms, 0, t, 0, terms.length);
        ArrayUtil.timSort(t);
        equalTerms = t;
    }
    return equalTerms;

}
 
Example #24
Source File: AbstractLTRQueryTestCase.java    From ltr4l with Apache License 2.0 5 votes vote down vote up
protected FieldFeatureExtractorFactory getIDF(String featureName, String fieldName, IndexReaderContext context, Term... terms){
  FieldFeatureExtractorFactory factory = new FieldFeatureIDFExtractorFactory(featureName, fieldName);
  if(context != null){
    factory.init(context, terms);
  }
  return factory;
}
 
Example #25
Source File: TestUnifiedHighlighterMTQ.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCustomSpanQueryHighlighting() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
  Document doc = new Document();
  doc.add(new Field("body", "alpha bravo charlie delta echo foxtrot golf hotel india juliet", fieldType));
  doc.add(newTextField("id", "id", Field.Store.YES));

  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer);

  int docId = searcher.search(new TermQuery(new Term("id", "id")), 1).scoreDocs[0].doc;

  WildcardQuery wildcardQuery = new WildcardQuery(new Term("body", "foxtr*"));
  SpanMultiTermQueryWrapper<WildcardQuery> wildcardQueryWrapper = new SpanMultiTermQueryWrapper<>(wildcardQuery);

  SpanQuery wrappedQuery = new MyWrapperSpanQuery(wildcardQueryWrapper);

  BooleanQuery query = new BooleanQuery.Builder()
      .add(wrappedQuery, BooleanClause.Occur.SHOULD)
      .build();

  int[] docIds = new int[]{docId};

  String snippets[] = highlighter.highlightFields(new String[]{"body"}, query, docIds, new int[]{2}).get("body");
  assertEquals(1, snippets.length);
  assertEquals("alpha bravo charlie delta echo <b>foxtrot</b> golf hotel india juliet", snippets[0]);
  ir.close();
}
 
Example #26
Source File: CompositeTermRecognitionProcessor.java    From jate with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public Boolean candidateExtraction(SolrCore core, String jatePropertyFile)
        throws IOException, JATEException {
    SolrIndexSearcher indexSearcher = core.getSearcher().get();
    IndexWriter writerIn = null;
    try {
    	writerIn = core.getSolrCoreState().getIndexWriter(core).get();
     Map<String,List<CopyField>> copyFields = core.getLatestSchema().getCopyFieldsMap();
	
     for (int i=0; i<indexSearcher.maxDoc(); i++) {
         Document doc = indexSearcher.doc(i);
	
         SolrUtil.copyFields(copyFields, DEFAULT_BOOST_VALUE, doc);
	
         writerIn.updateDocument(new Term("id",doc.get("id")), doc);
     }
     writerIn.commit();
	
     return true;
    } finally {
    	indexSearcher.close();
    	if (writerIn != null) {
    		writerIn.close();
    	}
    	
    }
}
 
Example #27
Source File: BlendedTermQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public BlendedTermQuery(Term[] terms, float[] boosts) {
    if (terms == null || terms.length == 0) {
        throw new IllegalArgumentException("terms must not be null or empty");
    }
    if (boosts != null && boosts.length != terms.length) {
        throw new IllegalArgumentException("boosts must have the same size as terms");
    }
    this.terms = terms;
    this.boosts = boosts;
}
 
Example #28
Source File: TestUnifiedHighlighterMTQ.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testPositionSensitiveWithWildcardDoesNotHighlight() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);
  Document doc = new Document();
  doc.add(new Field("body", "iterate insect ipswitch illinois indirect", fieldType));
  doc.add(newTextField("id", "id", Field.Store.YES));

  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  iw.close();

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  int docID = searcher.search(new TermQuery(new Term("id", "id")), 1).scoreDocs[0].doc;

  PhraseQuery pq = new PhraseQuery.Builder()
      .add(new Term("body", "consent"))
      .add(new Term("body", "order"))
      .build();

  BooleanQuery query = new BooleanQuery.Builder()
      .add(new WildcardQuery(new Term("body", "enforc*")), BooleanClause.Occur.MUST)
      .add(pq, BooleanClause.Occur.MUST)
      .build();

  int[] docIds = new int[]{docID};

  String snippets[] = highlighter.highlightFields(new String[]{"body"}, query, docIds, new int[]{2}).get("body");
  assertEquals(1, snippets.length);
  assertEquals("iterate insect ipswitch illinois indirect", snippets[0]);
  ir.close();
}
 
Example #29
Source File: TestFuzzyQuery.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testDistanceAsEditsSearching() throws Exception {
  Directory index = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), index);
  addDoc("foobar", w);
  addDoc("test", w);
  addDoc("working", w);
  IndexReader reader = w.getReader();
  IndexSearcher searcher = newSearcher(reader);
  w.close();
  
  FuzzyQuery q = new FuzzyQuery(new Term("field", "fouba"), 2);
  ScoreDoc[] hits = searcher.search(q, 10).scoreDocs;
  assertEquals(1, hits.length);
  assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
  
  q = new FuzzyQuery(new Term("field", "foubara"), 2);
  hits = searcher.search(q, 10).scoreDocs;
  assertEquals(1, hits.length);
  assertEquals("foobar", searcher.doc(hits[0].doc).get("field"));
  
  expectThrows(IllegalArgumentException.class, () -> {
    new FuzzyQuery(new Term("field", "t"), 3);
  });

  reader.close();
  index.close();
}
 
Example #30
Source File: ExtendedCommonTermsQuery.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
protected Query newTermQuery(Term term, TermContext context) {
    if (fieldType == null) {
        return super.newTermQuery(term, context);
    }
    final Query query = fieldType.queryStringTermQuery(term);
    if (query == null) {
        return super.newTermQuery(term, context);
    } else {
        return query;
    }
}