Java Code Examples for org.apache.lucene.search.IndexSearcher#search()

The following examples show how to use org.apache.lucene.search.IndexSearcher#search() . These examples are extracted from open source projects. 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 Project: lucene-solr   File: JoinUtil.java    License: Apache License 2.0 6 votes vote down vote up
private static Query createJoinQuery(boolean multipleValuesPerDocument, String toField, Query fromQuery, String fromField,
    IndexSearcher fromSearcher, ScoreMode scoreMode, final GenericTermsCollector collector) throws IOException {
  
  fromSearcher.search(fromQuery, collector);
  switch (scoreMode) {
    case None:
      return new TermsQuery(toField, collector.getCollectedTerms(), fromField, fromQuery, fromSearcher.getTopReaderContext().id());
    case Total:
    case Max:
    case Min:
    case Avg:
      return new TermsIncludingScoreQuery(
          scoreMode,
          toField,
          multipleValuesPerDocument,
          collector.getCollectedTerms(),
          collector.getScoresPerTerm(),
          fromField,
          fromQuery,
          fromSearcher.getTopReaderContext().id()
      );
    default:
      throw new IllegalArgumentException(String.format(Locale.ROOT, "Score mode %s isn't supported.", scoreMode));
  }
}
 
Example 2
private void test(int expected, boolean rowQuery, Collection<String> readAuthorizations) throws IOException,
    ParseException {
  DirectoryReader reader = DirectoryReader.open(_dir);
  SuperParser parser = new SuperParser(Version.LUCENE_43, _fieldManager, rowQuery, null, ScoreType.SUPER, new Term(
      BlurConstants.PRIME_DOC, BlurConstants.PRIME_DOC_VALUE));

  Query query = parser.parse("fam.string:value");

  Collection<String> discoverAuthorizations = null;
  Set<String> discoverableFields = null;
  IndexSearcher searcher = new SecureIndexSearcher(reader, getAccessControlFactory(), readAuthorizations,
      discoverAuthorizations, discoverableFields, null);

  TopDocs topDocs = searcher.search(query, 10);
  assertEquals(expected, topDocs.totalHits);
  reader.close();
}
 
Example 3
Source Project: RDFS   File: TestMixedDirectory.java    License: Apache License 2.0 6 votes vote down vote up
private void verify(Directory dir, int expectedHits) throws IOException {
  IndexSearcher searcher = new IndexSearcher(dir);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  int numHits = hits.length();

  assertEquals(expectedHits, numHits);

  int[] docs = new int[numHits];
  for (int i = 0; i < numHits; i++) {
    Document hit = hits.doc(i);
    docs[Integer.parseInt(hit.get("id"))]++;
  }
  for (int i = 0; i < numHits; i++) {
    assertEquals(1, docs[i]);
  }

  searcher.close();
}
 
Example 4
public void testHighlightLastWord() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test");
  iw.addDocument(doc);

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

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  Query query = new IntervalQuery("body", Intervals.term("test"));
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(1, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(1, snippets.length);
  assertEquals("This is a <b>test</b>", snippets[0]);

  ir.close();
}
 
Example 5
Source Project: tutorials   File: InMemoryLuceneIndex.java    License: MIT License 6 votes vote down vote up
public List<Document> searchIndex(Query query, Sort sort) {
    try {
        IndexReader indexReader = DirectoryReader.open(memoryIndex);
        IndexSearcher searcher = new IndexSearcher(indexReader);
        TopDocs topDocs = searcher.search(query, 10, sort);
        List<Document> documents = new ArrayList<>();
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            documents.add(searcher.doc(scoreDoc.doc));
        }

        return documents;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return null;

}
 
Example 6
/**
 * Perform search
 */
public static TopDocs performSearch(String field, String qs) throws IOException, ParseException {
	IndexSearcher searcher = Indexer.getIndexSearcher();
	QueryParser parser = new QueryParser(Config.LUCENE_VERSION, field, Indexer.getAnalyzer());
	Query query = parser.parse(qs);
	TopDocs result = searcher.search(query, Indexer.HITS_PER_PAGE);
	return result;
}
 
Example 7
@Test
public void testSuperScoreTypes() throws Exception {
  IndexSearcher searcher = createSearcher();
  BooleanQuery booleanQuery = new BooleanQuery();
  booleanQuery.add(wrapSuper(PERSON_NAME, NAME1, ScoreType.SUPER), Occur.SHOULD);
  booleanQuery.add(wrapSuper(ADDRESS_STREET, STREET1, ScoreType.SUPER), Occur.MUST);
  TopDocs topDocs = searcher.search(booleanQuery, 10);
  assertEquals(3, topDocs.totalHits);
  printTopDocs(topDocs);
}
 
Example 8
@Override
public List<MagicCard> search(String q)
{
	if(dir==null)
		open();
	
	List<MagicCard> ret = new ArrayList<>();
	
	try (IndexReader indexReader = DirectoryReader.open(dir))
	{
		 IndexSearcher searcher = new IndexSearcher(indexReader);
		 Query query = new QueryParser("name", analyzer).parse(q);
		 logger.trace(query);
		 
		 TotalHitCountCollector collector = new TotalHitCountCollector();
		 searcher.search(query,collector);
		 
		 TopDocs top= searcher.search(query, Math.max(1, collector.getTotalHits()));
		 
		 for(int i =0;i<top.totalHits.value;i++)
			 ret.add(serializer.fromJson(searcher.doc(top.scoreDocs[i].doc).get("data"),MagicCard.class));
		 
		 
	} catch (Exception e) {
		logger.error(e);
	}
	
	return ret;
}
 
Example 9
public void testBuddhism() throws Exception {
  String text = "This eight-volume set brings together seminal papers in Buddhist studies from a vast " +
      "range of academic disciplines published over the last forty years. With a new introduction " +
      "by the editor, this collection is a unique and unrivalled research resource for both " +
      "student and scholar. Coverage includes: - Buddhist origins; early history of Buddhism in " +
      "South and Southeast Asia - early Buddhist Schools and Doctrinal History; Theravada Doctrine " +
      "- the Origins and nature of Mahayana Buddhism; some Mahayana religious topics - Abhidharma " +
      "and Madhyamaka - Yogacara, the Epistemological tradition, and Tathagatagarbha - Tantric " +
      "Buddhism (Including China and Japan); Buddhism in Nepal and Tibet - Buddhism in South and " +
      "Southeast Asia, and - Buddhism in China, East Asia, and Japan.";
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", text, fieldType);
  Document document = new Document();
  document.add(body);
  iw.addDocument(document);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher searcher = newSearcher(ir);
  PhraseQuery query = new PhraseQuery.Builder()
      .add(new Term("body", "buddhist"))
      .add(new Term("body", "origins"))
      .build();
  TopDocs topDocs = searcher.search(query, 10);
  assertEquals(1, topDocs.totalHits.value);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setHighlightPhrasesStrictly(false);
  String snippets[] = highlighter.highlight("body", query, topDocs, 2);
  assertEquals(1, snippets.length);
  if (highlighter.getFlags("body").containsAll(EnumSet.of(HighlightFlag.WEIGHT_MATCHES, HighlightFlag.PHRASES))) {
    assertTrue(snippets[0], snippets[0].contains("<b>Buddhist origins</b>"));
  } else {
    assertTrue(snippets[0], snippets[0].contains("<b>Buddhist</b> <b>origins</b>"));
  }
  ir.close();
}
 
Example 10
public void testBasics() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test. Just a test highlighting from postings. Feel free to ignore.");
  iw.addDocument(doc);
  body.setStringValue("Highlighting the first term. Hope it works.");
  iw.addDocument(doc);

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

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  Query query = new TermQuery(new Term("body", "highlighting"));
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(2, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(2, snippets.length);
  assertEquals("Just a test <b>highlighting</b> from postings. ", snippets[0]);
  assertEquals("<b>Highlighting</b> the first term. ", snippets[1]);

  ir.close();
}
 
Example 11
public void testWithMaxLen() throws IOException {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("Alpha Bravo foo foo foo. Foo foo Alpha Bravo");//44 char long, 2 sentences
  iw.addDocument(doc);

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

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = randomUnifiedHighlighter(searcher, indexAnalyzer);
  highlighter.setMaxLength(25);//a little past first sentence

  BooleanQuery query = new BooleanQuery.Builder()
      .add(new TermQuery(new Term("body", "alpha")), BooleanClause.Occur.MUST)
      .add(new PrefixQuery(new Term("body", "bra")), BooleanClause.Occur.MUST)
      .build();
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  String snippets[] = highlighter.highlight("body", query, topDocs, 2);//ask for 2 but we'll only get 1
  assertArrayEquals(
      new String[]{"<b>Alpha</b> <b>Bravo</b> foo foo foo. "}, snippets
  );

  ir.close();
}
 
Example 12
public void testStopWords() throws IOException, InvalidTokenOffsetsException {
  MockAnalyzer stopAnalyzer = new MockAnalyzer(random(), MockTokenizer.WHITESPACE, true,
      MockTokenFilter.ENGLISH_STOPSET);    
  final String TEXT = "the ab the the cd the the the ef the";
  final Directory directory = newDirectory();
  try (IndexWriter indexWriter = new IndexWriter(directory,
      newIndexWriterConfig(stopAnalyzer))) {
    final Document document = new Document();
    document.add(newTextField(FIELD, TEXT, Store.YES));
    indexWriter.addDocument(document);
  }
  try (IndexReader indexReader = DirectoryReader.open(directory)) {
    assertEquals(1, indexReader.numDocs());
    final IndexSearcher indexSearcher = newSearcher(indexReader);
    //equivalent of "ab the the cd the the the ef"
    final PhraseQuery phraseQuery = new PhraseQuery.Builder()
        .add(new Term(FIELD, "ab"), 0)
        .add(new Term(FIELD, "cd"), 3)
        .add(new Term(FIELD, "ef"), 7).build();

    TopDocs hits = indexSearcher.search(phraseQuery, 100);
    assertEquals(1, hits.totalHits.value);
    final Highlighter highlighter = new Highlighter(
        new SimpleHTMLFormatter(), new SimpleHTMLEncoder(),
        new QueryScorer(phraseQuery));
    assertEquals(1, highlighter.getBestFragments(stopAnalyzer, FIELD, TEXT, 10).length);
  } finally {
    directory.close();
  }
}
 
Example 13
@Test
public void testBooleanExpression() throws Exception {
    BooleanColumnReference booleanColumn = new BooleanColumnReference(column);
    booleanColumn.startCollect(ctx);
    booleanColumn.setNextReader(readerContext);
    IndexSearcher searcher = new IndexSearcher(readerContext.reader());
    TopDocs topDocs = searcher.search(new MatchAllDocsQuery(), 20);
    int i = 0;
    for (ScoreDoc doc : topDocs.scoreDocs) {
        booleanColumn.setNextDocId(doc.doc);
        assertThat(booleanColumn.value(), is(i % 2 == 0));
        i++;
    }
}
 
Example 14
Source Project: RDFS   File: TestDistributionPolicy.java    License: Apache License 2.0 5 votes vote down vote up
private void verify(Shard[] shards) throws IOException {
  // verify the index
  IndexReader[] readers = new IndexReader[shards.length];
  for (int i = 0; i < shards.length; i++) {
    Directory dir =
        new FileSystemDirectory(fs, new Path(shards[i].getDirectory()),
            false, conf);
    readers[i] = IndexReader.open(dir);
  }

  IndexReader reader = new MultiReader(readers);
  IndexSearcher searcher = new IndexSearcher(reader);
  Hits hits = searcher.search(new TermQuery(new Term("content", "apache")));
  assertEquals(0, hits.length());

  hits = searcher.search(new TermQuery(new Term("content", "hadoop")));
  assertEquals(numDocsPerRun / 2, hits.length());

  int[] counts = new int[numDocsPerRun];
  for (int i = 0; i < hits.length(); i++) {
    Document doc = hits.doc(i);
    counts[Integer.parseInt(doc.get("id"))]++;
  }

  for (int i = 0; i < numDocsPerRun; i++) {
    if (i % 2 == 0) {
      assertEquals(0, counts[i]);
    } else {
      assertEquals(1, counts[i]);
    }
  }

  searcher.close();
  reader.close();
}
 
Example 15
public void testEncode() throws Exception {
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, indexAnalyzer);

  Field body = new Field("body", "", fieldType);
  Document doc = new Document();
  doc.add(body);

  body.setStringValue("This is a test. Just a test highlighting from <i>postings</i>. Feel free to ignore.");
  iw.addDocument(doc);

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

  IndexSearcher searcher = newSearcher(ir);
  UnifiedHighlighter highlighter = new UnifiedHighlighter(searcher, indexAnalyzer) {
    @Override
    protected PassageFormatter getFormatter(String field) {
      return new DefaultPassageFormatter("<b>", "</b>", "... ", true);
    }
  };
  Query query = new TermQuery(new Term("body", "highlighting"));
  TopDocs topDocs = searcher.search(query, 10, Sort.INDEXORDER);
  assertEquals(1, topDocs.totalHits.value);
  String snippets[] = highlighter.highlight("body", query, topDocs);
  assertEquals(1, snippets.length);
  assertEquals("Just a test <b>highlighting</b> from &lt;i&gt;postings&lt;&#x2F;i&gt;. ", snippets[0]);

  ir.close();
}
 
Example 16
public void testCrossesDateLine() throws IOException {
  Directory dir = newDirectory();
  RandomIndexWriter w = new RandomIndexWriter(random(), dir, newIndexWriterConfig()
      .setMergePolicy(newLogMergePolicy(random().nextBoolean())));
  Document doc = new Document();
  LatLonPoint point = new LatLonPoint("foo", 0.0, 0.0);
  doc.add(point);
  LatLonDocValuesField docValue = new LatLonDocValuesField("foo",0.0, 0.0);
  doc.add(docValue);

  double pivotDistance = 5000;//5k

  point.setLocationValue(0, -179);
  docValue.setLocationValue(0, -179);
  w.addDocument(doc);

  point.setLocationValue(0, 176);
  docValue.setLocationValue(0, 176);
  w.addDocument(doc);

  point.setLocationValue(0, -150);
  docValue.setLocationValue(0, -150);
  w.addDocument(doc);

  point.setLocationValue(0, -140);
  docValue.setLocationValue(0, -140);
  w.addDocument(doc);

  point.setLocationValue(0, 140);
  docValue.setLocationValue(01, 140);
  w.addDocument(doc);

  DirectoryReader reader = w.getReader();
  IndexSearcher searcher = newSearcher(reader);

  Query q = LatLonPoint.newDistanceFeatureQuery("foo", 3, 0, 179, pivotDistance);
  TopScoreDocCollector collector = TopScoreDocCollector.create(2, null, 1);
  searcher.search(q, collector);
  TopDocs topHits = collector.topDocs();
  assertEquals(2, topHits.scoreDocs.length);

  double distance1 = SloppyMath.haversinMeters(GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(0)) , GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(-179)), 0,179);
  double distance2 = SloppyMath.haversinMeters(GeoEncodingUtils.decodeLatitude(GeoEncodingUtils.encodeLatitude(0)) , GeoEncodingUtils.decodeLongitude(GeoEncodingUtils.encodeLongitude(176)), 0,179);

  CheckHits.checkEqual(q,
      new ScoreDoc[] {
          new ScoreDoc(0, (float) (3f * (pivotDistance / (pivotDistance + distance1)))),
          new ScoreDoc(1, (float) (3f * (pivotDistance / (pivotDistance + distance2))))
      },
      topHits.scoreDocs);

  reader.close();
  w.close();
  dir.close();
}
 
Example 17
Source Project: modernmt   File: TLuceneAnalyzer.java    License: Apache License 2.0 4 votes vote down vote up
public Entry getEntry(long memory, LanguageDirection direction) throws IOException {
    ContextAnalyzerIndex index = getIndex();
    TCorporaStorage storage = getStorage();

    String docId = DocumentBuilder.makeId(memory, direction);

    // Bucket for content

    String content = null;

    Bucket bucket = storage.getBucket(memory, direction);
    if (bucket != null) {
        InputStream stream = null;

        try {
            stream = bucket.getContentStream();
            content = IOUtils.toString(stream, UTF8Charset.get());
        } finally {
            IOUtils.closeQuietly(stream);
        }
    }

    // Index for terms

    Set<String> terms = null;

    IndexSearcher searcher = index.getIndexSearcher();
    TermQuery query = new TermQuery(DocumentBuilder.makeIdTerm(docId));
    TopDocs docs = searcher.search(query, 1);

    if (docs.scoreDocs.length > 0) {
        String filedName = DocumentBuilder.makeContentFieldName(direction);
        terms = LuceneUtils.getTermFrequencies(searcher.getIndexReader(), docs.scoreDocs[0].doc, filedName).keySet();
    }

    // Creating result

    if (terms == null && content == null)
        return null;

    return new Entry(memory, direction, terms, content);
}
 
Example 18
public void testWrongIndexFieldName() throws Exception {
  Directory dir = newDirectory();
  Directory taxoDir = newDirectory();

  // Writes facet ords to a separate directory from the
  // main index:
  DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);

  FacetsConfig config = new FacetsConfig();
  config.setIndexFieldName("a", "$facets2");
  RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

  Document doc = new Document();
  doc.add(new FacetField("a", "foo1"));
  writer.addDocument(config.build(taxoWriter, doc));

  // NRT open
  IndexSearcher searcher = newSearcher(writer.getReader());

  // NRT open
  TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);

  FacetsCollector c = new FacetsCollector();
  searcher.search(new MatchAllDocsQuery(), c);

  // Uses default $facets field:
  Facets facets;
  if (random().nextBoolean()) {
    facets = new FastTaxonomyFacetCounts(taxoReader, config, c);
  } else {
    OrdinalsReader ordsReader = new DocValuesOrdinalsReader();
    if (random().nextBoolean()) {
      ordsReader = new CachedOrdinalsReader(ordsReader);
    }
    facets = new TaxonomyFacetCounts(ordsReader, taxoReader, config, c);
  }

  // Ask for top 10 labels for any dims that have counts:
  List<FacetResult> results = facets.getAllDims(10);
  assertTrue(results.isEmpty());

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getSpecificValue("a");
  });

  expectThrows(IllegalArgumentException.class, () -> {
    facets.getTopChildren(10, "a");
  });

  writer.close();
  IOUtils.close(taxoWriter, searcher.getIndexReader(), taxoReader, taxoDir, dir);
}
 
Example 19
public void testWrongIndexFieldName() throws Exception {

    Directory dir = newDirectory();
    Directory taxoDir = newDirectory();

    // Writes facet ords to a separate directory from the
    // main index:
    DirectoryTaxonomyWriter taxoWriter = new DirectoryTaxonomyWriter(taxoDir, IndexWriterConfig.OpenMode.CREATE);

    FacetsConfig config = new FacetsConfig();
    config.setIndexFieldName("a", "$facets2");

    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);

    Document doc = new Document();
    doc.add(new NumericDocValuesField("num", 10));
    doc.add(new FacetField("a", "foo1"));
    writer.addDocument(config.build(taxoWriter, doc));

    // NRT open
    IndexSearcher searcher = newSearcher(writer.getReader());
    writer.close();

    // NRT open
    TaxonomyReader taxoReader = new DirectoryTaxonomyReader(taxoWriter);
    taxoWriter.close();

    FacetsCollector c = new FacetsCollector();
    searcher.search(new MatchAllDocsQuery(), c);    

    TaxonomyFacetSumValueSource facets = new TaxonomyFacetSumValueSource(taxoReader, config, c, DoubleValuesSource.fromIntField("num"));

    // Ask for top 10 labels for any dims that have counts:
    List<FacetResult> results = facets.getAllDims(10);
    assertTrue(results.isEmpty());

    expectThrows(IllegalArgumentException.class, () -> {
      facets.getSpecificValue("a");
    });

    expectThrows(IllegalArgumentException.class, () -> {
      facets.getTopChildren(10, "a");
    });

    IOUtils.close(searcher.getIndexReader(), taxoReader, dir, taxoDir);
  }
 
Example 20
public void testGroupHeadsWithSort() throws IOException {

    Shard shard = new Shard();
    indexRandomDocs(shard.writer);
    IndexSearcher searcher = shard.getIndexSearcher();

    String[] query = new String[]{ "foo", "bar", "baz" };
    Query topLevel = new TermQuery(new Term("text", query[random().nextInt(query.length)]));

    Sort sort = new Sort(new SortField("sort1", SortField.Type.STRING), new SortField("sort2", SortField.Type.LONG));
    GroupSelector<T> groupSelector = getGroupSelector();
    GroupingSearch grouping = new GroupingSearch(groupSelector);
    grouping.setAllGroups(true);
    grouping.setAllGroupHeads(true);
    grouping.setSortWithinGroup(sort);

    grouping.search(searcher, topLevel, 0, 1);
    Collection<T> matchingGroups = grouping.getAllMatchingGroups();

    Bits groupHeads = grouping.getAllGroupHeads();
    int cardinality = 0;
    for (int i = 0; i < groupHeads.length(); i++) {
      if (groupHeads.get(i)) {
        cardinality++;
      }
    }
    assertEquals(matchingGroups.size(), cardinality);   // We should have one set bit per matching group

    // Each group head should correspond to the topdoc of a search filtered by
    // that group using the same within-group sort
    for (T groupValue : matchingGroups) {
      Query filtered = new BooleanQuery.Builder()
          .add(topLevel, BooleanClause.Occur.MUST)
          .add(filterQuery(groupValue), BooleanClause.Occur.FILTER)
          .build();
      TopDocs td = searcher.search(filtered, 1, sort);
      assertTrue(groupHeads.get(td.scoreDocs[0].doc));
    }

    shard.close();
  }