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

The following examples show how to use org.apache.lucene.search.IndexSearcher#setSimilarity() . 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: TestSimilarity2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** make sure we can retrieve when norms are disabled */
public void testNoNorms() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
  ft.setOmitNorms(true);
  ft.freeze();
  doc.add(newField("foo", "bar", ft));
  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);
    assertEquals(1, is.search(query.build(), 10).totalHits.value);
  }
  ir.close();
  dir.close();
}
 
Example 2
Source File: RetrievalApp.java    From lucene4ir with Apache License 2.0 6 votes vote down vote up
public RetrievalApp(String retrievalParamFile){
    System.out.println("Retrieval App");
    System.out.println("Param File: " + retrievalParamFile);
    readParamsFromFile(retrievalParamFile);
    try {
        reader = DirectoryReader.open(FSDirectory.open( new File(p.indexName).toPath()) );
        searcher = new IndexSearcher(reader);

        // create similarity function and parameter
        selectSimilarityFunction(sim);
        searcher.setSimilarity(simfn);

        parser = new QueryParser(Lucene4IRConstants.FIELD_ALL, analyzer);

    } catch (Exception e){
        System.out.println(" caught a " + e.getClass() +
                "\n with message: " + e.getMessage());
    }
}
 
Example 3
Source File: FieldedRetrievalApp.java    From lucene4ir with Apache License 2.0 6 votes vote down vote up
/**
 * Instantiates Fielded Retrieval.
 * Supers the RetrievalApp to set up all the retrieval variables.
 * @param retrievalParamFile
 */
public FieldedRetrievalApp(String retrievalParamFile) {
    super(retrievalParamFile);
    System.out.println("Fielded Querying");
    this.readFieldedParamsFromFile(fieldsFile);
    for (Field f : fl.fields)
        System.out.println("Field: " + f.fieldName + " Boost: " + f.fieldBoost);

    try {
        reader = DirectoryReader.open(FSDirectory.open( new File(p.indexName).toPath()) );
        searcher = new IndexSearcher(reader);

        // create similarity function and parameter
        selectSimilarityFunction(sim);
        searcher.setSimilarity(simfn);

    } catch (Exception e){
        System.out.println(" caught a " + e.getClass() +
                "\n with message: " + e.getMessage());
    }
}
 
Example 4
Source File: TestSweetSpotSimilarityFactory.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static float computeNorm(Similarity sim, int length) throws IOException {
  String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" "));
  Directory dir = newDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));
  w.addDocument(Collections.singleton(newTextField("foo", value, Store.NO)));
  DirectoryReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setSimilarity(sim);
  Explanation expl = searcher.explain(new TermQuery(new Term("foo", "a")), 0);
  reader.close();
  dir.close();
  Explanation norm = findExplanation(expl, "fieldNorm");
  assertNotNull(norm);
  return norm.getValue().floatValue();
}
 
Example 5
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 6
Source File: TestSimilarity2.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/** similar to the above, but ORs the query with a real field */
public void testEmptyField() 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("bar", "baz")), BooleanClause.Occur.SHOULD);
    assertEquals(1, is.search(query.build(), 10).totalHits.value);
  }
  ir.close();
  dir.close();
}
 
Example 7
Source File: SweetSpotSimilarityTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private static float computeNorm(Similarity sim, String field, int length) throws IOException {
  String value = IntStream.range(0, length).mapToObj(i -> "a").collect(Collectors.joining(" "));
  Directory dir = new ByteBuffersDirectory();
  IndexWriter w = new IndexWriter(dir, newIndexWriterConfig().setSimilarity(sim));
  w.addDocument(Collections.singleton(newTextField(field, value, Store.NO)));
  DirectoryReader reader = DirectoryReader.open(w);
  w.close();
  IndexSearcher searcher = new IndexSearcher(reader);
  searcher.setSimilarity(sim);
  Explanation expl = searcher.explain(new TermQuery(new Term(field, "a")), 0);
  reader.close();
  dir.close();
  Explanation norm = findExplanation(expl, "fieldNorm");
  assertNotNull(norm);
  return norm.getValue().floatValue();
}
 
Example 8
Source File: PayloadHelper.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Sets up a RAM-resident Directory, and adds documents (using English.intToEnglish()) with two fields: field and multiField
 * and analyzes them using the PayloadAnalyzer
 * @param similarity The Similarity class to use in the Searcher
 * @param numDocs The num docs to add
 * @return An IndexSearcher
 */
// TODO: randomize
public IndexSearcher setUp(Random random, Similarity similarity, int numDocs) throws IOException {
  Directory directory = new MockDirectoryWrapper(random, new ByteBuffersDirectory());
  PayloadAnalyzer analyzer = new PayloadAnalyzer();

  // TODO randomize this
  IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(
      analyzer).setSimilarity(similarity));
  // writer.infoStream = System.out;
  for (int i = 0; i < numDocs; i++) {
    Document doc = new Document();
    doc.add(new TextField(FIELD, English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(MULTI_FIELD, English.intToEnglish(i) + "  " + English.intToEnglish(i), Field.Store.YES));
    doc.add(new TextField(NO_PAYLOAD_FIELD, English.intToEnglish(i), Field.Store.YES));
    writer.addDocument(doc);
  }
  writer.forceMerge(1);
  reader = DirectoryReader.open(writer);
  writer.close();

  IndexSearcher searcher = LuceneTestCase.newSearcher(LuceneTestCase.getOnlyLeafReader(reader));
  searcher.setSimilarity(similarity);
  return searcher;
}
 
Example 9
Source File: SORecommender.java    From scava with Eclipse Public License 2.0 6 votes vote down vote up
public TopDocs executeQuery(org.apache.lucene.search.Query query) throws IOException, ParseException {
	Directory indexDir = FSDirectory.open(Paths.get(INDEX_DIRECTORY));
	try {
		IndexReader reader = DirectoryReader.open(indexDir);
		IndexSearcher searcher = new IndexSearcher(reader);
		if (isBm25 == false) {
			ClassicSimilarity CS = new ClassicSimilarity();
			searcher.setSimilarity(CS);
		}
		TopDocs docs = searcher.search(query, hitsPerPage);
		return docs;
	} catch (Exception e) {
		logger.error(e.getMessage());
		return null;
	}
}
 
Example 10
Source File: TestSimilarity2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** because of stupid things like querynorm, it's possible we computeStats on a field that doesnt exist at all
 *  test this against a totally empty index, to make sure sims handle it
 */
public void testEmptyIndex() throws Exception {
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher is = newSearcher(ir);
  
  for (Similarity sim : sims) {
    is.setSimilarity(sim);
    assertEquals(0, is.search(new TermQuery(new Term("foo", "bar")), 10).totalHits.value);
  }
  ir.close();
  dir.close();
}
 
Example 11
Source File: MemoryIndex.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Creates and returns a searcher that can be used to execute arbitrary
 * Lucene queries and to collect the resulting query results as hits.
 * 
 * @return a searcher
 */
public IndexSearcher createSearcher() {
  MemoryIndexReader reader = new MemoryIndexReader();
  IndexSearcher searcher = new IndexSearcher(reader); // ensures no auto-close !!
  searcher.setSimilarity(normSimilarity);
  searcher.setQueryCache(null);
  return searcher;
}
 
Example 12
Source File: TestSimilarity2.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** make sure all sims work with spanOR(termX, termY) where termY does not exist */
public void testCrazySpans() throws Exception {
  // historically this was a problem, but sim's no longer have to score terms that dont exist
  Directory dir = newDirectory();
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
  Document doc = new Document();
  FieldType ft = new FieldType(TextField.TYPE_NOT_STORED);
  doc.add(newField("foo", "bar", ft));
  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  iw.close();
  IndexSearcher is = newSearcher(ir);
  
  for (Similarity sim : sims) {
    is.setSimilarity(sim);
    SpanTermQuery s1 = new SpanTermQuery(new Term("foo", "bar"));
    SpanTermQuery s2 = new SpanTermQuery(new Term("foo", "baz"));
    Query query = new SpanOrQuery(s1, s2);
    TopDocs td = is.search(query, 10);
    assertEquals(1, td.totalHits.value);
    float score = td.scoreDocs[0].score;
    assertFalse("negative score for " + sim, score < 0.0f);
    assertFalse("inf score for " + sim, Float.isInfinite(score));
    assertFalse("nan score for " + sim, Float.isNaN(score));
  }
  ir.close();
  dir.close();
}
 
Example 13
Source File: LuceneTermQueryBuilderTest.java    From querqy with Apache License 2.0 5 votes vote down vote up
@Test
public void testThatQueryUsesTermButNoFieldBoost() throws Exception {

    Analyzer analyzer = new StandardAnalyzer();

    Directory directory = new ByteBuffersDirectory();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    config.setSimilarity(new ClassicSimilarity());
    IndexWriter indexWriter = new IndexWriter(directory, config);


    TestUtil.addNumDocsWithTextField("f1", "v1 v1", indexWriter, 4);
    TestUtil.addNumDocsWithTextField("f1", "v2", indexWriter, 1);

    indexWriter.close();

    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    indexSearcher.setSimilarity(new ClassicSimilarity());


    final TermQuery termQuery = new LuceneTermQueryBuilder()
            .createTermQuery(new Term("f1", "v1"), new ConstantFieldBoost(3f));
    final Term term = termQuery.getTerm();
    assertEquals("f1", term.field());
    assertEquals("v1", term.text());

    TopDocs topDocs = indexSearcher.search(termQuery, 10);

    final Weight weight = termQuery.createWeight(indexSearcher, ScoreMode.COMPLETE, 4.5f);
    final Explanation explain = weight.explain(indexReader.getContext().leaves().get(0), topDocs.scoreDocs[0].doc);

    String explainText = explain.toString();

    assertTrue(explainText.contains("4.5 = boost")); // 4.5 (query) but ignore field boost
    assertTrue(explainText.contains("4 = docFreq")); // 4 * v1
    assertTrue(explainText.contains("2.0 = freq")); // 2 * v1 in field
}
 
Example 14
Source File: IndexSearcherWrapper.java    From crate with Apache License 2.0 5 votes vote down vote up
/**
 * If there are configured {@link IndexSearcherWrapper} instances, the {@link IndexSearcher} of the provided engine searcher
 * gets wrapped and a new {@link Engine.Searcher} instances is returned, otherwise the provided {@link Engine.Searcher} is returned.
 *
 * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
 */
public final Engine.Searcher wrap(Engine.Searcher engineSearcher) throws IOException {
    final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
    if (elasticsearchDirectoryReader == null) {
        throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
    }
    NonClosingReaderWrapper nonClosingReaderWrapper = new NonClosingReaderWrapper(engineSearcher.getDirectoryReader());
    DirectoryReader reader = wrap(nonClosingReaderWrapper);
    if (reader != nonClosingReaderWrapper) {
        if (reader.getReaderCacheHelper() != elasticsearchDirectoryReader.getReaderCacheHelper()) {
            throw new IllegalStateException("wrapped directory reader doesn't delegate IndexReader#getCoreCacheKey, wrappers must override this method and delegate" +
                    " to the original readers core cache key. Wrapped readers can't be used as cache keys since their are used only per request which would lead to subtle bugs");
        }
        if (ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(reader) != elasticsearchDirectoryReader) {
            // prevent that somebody wraps with a non-filter reader
            throw new IllegalStateException("wrapped directory reader hides actual ElasticsearchDirectoryReader but shouldn't");
        }
    }

    final IndexSearcher origIndexSearcher = engineSearcher.searcher();
    final IndexSearcher innerIndexSearcher = new IndexSearcher(reader);
    innerIndexSearcher.setQueryCache(origIndexSearcher.getQueryCache());
    innerIndexSearcher.setQueryCachingPolicy(origIndexSearcher.getQueryCachingPolicy());
    innerIndexSearcher.setSimilarity(origIndexSearcher.getSimilarity());
    // TODO: Right now IndexSearcher isn't wrapper friendly, when it becomes wrapper friendly we should revise this extension point
    // For example if IndexSearcher#rewrite() is overwritten than also IndexSearcher#createNormalizedWeight needs to be overwritten
    // This needs to be fixed before we can allow the IndexSearcher from Engine to be wrapped multiple times
    final IndexSearcher indexSearcher = wrap(innerIndexSearcher);
    if (reader == nonClosingReaderWrapper && indexSearcher == innerIndexSearcher) {
        return engineSearcher;
    } else {
        // we close the reader to make sure wrappers can release resources if needed....
        // our NonClosingReaderWrapper makes sure that our reader is not closed
        return new Engine.Searcher(engineSearcher.source(), indexSearcher, () ->
            IOUtils.close(indexSearcher.getIndexReader(), // this will close the wrappers excluding the NonClosingReaderWrapper
            engineSearcher)); // this will run the closeable on the wrapped engine searcher
    }
}
 
Example 15
Source File: LuceneIndex.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
public synchronized IndexSearcher getIndexSearcher() throws IOException {
	if (closed.get()) {
		throw new SailException("Index has been closed");
	}
	IndexSearcher indexSearcher = getCurrentMonitor().getIndexSearcher();
	indexSearcher.setSimilarity(similarity);
	return indexSearcher;
}
 
Example 16
Source File: IndexSearcherWrappingService.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
/**
 * If there are configured {@link IndexSearcherWrapper} instances, the {@link IndexSearcher} of the provided engine searcher
 * gets wrapped and a new {@link Engine.Searcher} instances is returned, otherwise the provided {@link Engine.Searcher} is returned.
 *
 * This is invoked each time a {@link Engine.Searcher} is requested to do an operation. (for example search)
 */
public final Engine.Searcher wrap(EngineConfig engineConfig, final Engine.Searcher engineSearcher) throws IOException {
    final ElasticsearchDirectoryReader elasticsearchDirectoryReader = ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(engineSearcher.getDirectoryReader());
    if (elasticsearchDirectoryReader == null) {
        throw new IllegalStateException("Can't wrap non elasticsearch directory reader");
    }
    if (wrapper == null) {
        return engineSearcher;
    }
    NonClosingReaderWrapper nonClosingReaderWrapper = new NonClosingReaderWrapper(engineSearcher.getDirectoryReader());
    DirectoryReader reader = wrapper.wrap(nonClosingReaderWrapper);
    if (reader != nonClosingReaderWrapper) {
        if (reader.getCoreCacheKey() != elasticsearchDirectoryReader.getCoreCacheKey()) {
            throw new IllegalStateException("wrapped directory reader doesn't delegate IndexReader#getCoreCacheKey, wrappers must override this method and delegate" +
                    " to the original readers core cache key. Wrapped readers can't be used as cache keys since their are used only per request which would lead to subtle bugs");
        }
        if (ElasticsearchDirectoryReader.getElasticsearchDirectoryReader(reader) != elasticsearchDirectoryReader) {
            // prevent that somebody wraps with a non-filter reader
            throw new IllegalStateException("wrapped directory reader hides actual ElasticsearchDirectoryReader but shouldn't");
        }
    }

    final IndexSearcher innerIndexSearcher = new IndexSearcher(reader);
    innerIndexSearcher.setQueryCache(engineConfig.getQueryCache());
    innerIndexSearcher.setQueryCachingPolicy(engineConfig.getQueryCachingPolicy());
    innerIndexSearcher.setSimilarity(engineConfig.getSimilarity());
    // TODO: Right now IndexSearcher isn't wrapper friendly, when it becomes wrapper friendly we should revise this extension point
    // For example if IndexSearcher#rewrite() is overwritten than also IndexSearcher#createNormalizedWeight needs to be overwritten
    // This needs to be fixed before we can allow the IndexSearcher from Engine to be wrapped multiple times
    final IndexSearcher indexSearcher = wrapper.wrap(engineConfig, innerIndexSearcher);
    if (reader == nonClosingReaderWrapper && indexSearcher == innerIndexSearcher) {
        return engineSearcher;
    } else {
        return new Engine.Searcher(engineSearcher.source(), indexSearcher) {
            @Override
            public void close() throws ElasticsearchException {
                try {
                    reader().close();
                    // we close the reader to make sure wrappers can release resources if needed....
                    // our NonClosingReaderWrapper makes sure that our reader is not closed
                } catch (IOException e) {
                    throw new ElasticsearchException("failed to close reader", e);
                } finally {
                    engineSearcher.close();
                }

            }
        };
    }
}
 
Example 17
Source File: TestSimilarity2.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/** make sure scores are not skewed by docs not containing the field */
public void testNoFieldSkew() throws Exception {
  Directory dir = newDirectory();
  // an evil merge policy could reorder our docs for no reason
  IndexWriterConfig iwConfig = newIndexWriterConfig().setMergePolicy(newLogMergePolicy());
  RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwConfig);
  Document doc = new Document();
  doc.add(newTextField("foo", "bar baz somethingelse", Field.Store.NO));
  iw.addDocument(doc);
  IndexReader ir = iw.getReader();
  IndexSearcher is = newSearcher(ir);
  
  BooleanQuery.Builder queryBuilder = new BooleanQuery.Builder();
  queryBuilder.add(new TermQuery(new Term("foo", "bar")), BooleanClause.Occur.SHOULD);
  queryBuilder.add(new TermQuery(new Term("foo", "baz")), BooleanClause.Occur.SHOULD);
  Query query = queryBuilder.build();
  
  // collect scores
  List<Explanation> scores = new ArrayList<>();
  for (Similarity sim : sims) {
    is.setSimilarity(sim);
    scores.add(is.explain(query, 0));
  }
  ir.close();
  
  // add some additional docs without the field
  int numExtraDocs = TestUtil.nextInt(random(), 1, 1000);
  for (int i = 0; i < numExtraDocs; i++) {
    iw.addDocument(new Document());
  }
  
  // check scores are the same
  ir = iw.getReader();
  is = newSearcher(ir);
  for (int i = 0; i < sims.size(); i++) {
    is.setSimilarity(sims.get(i));
    Explanation expected = scores.get(i);
    Explanation actual = is.explain(query, 0);
    assertEquals(sims.get(i).toString() + ": actual=" + actual + ",expected=" + expected, expected.getValue(), actual.getValue());
  }
  
  iw.close();
  ir.close();
  dir.close();
}
 
Example 18
Source File: DependentTermQueryBuilderTest.java    From querqy with Apache License 2.0 4 votes vote down vote up
@Test
public void testCreateWeight() throws Exception {

    Analyzer analyzer = new StandardAnalyzer();

    Directory directory = new ByteBuffersDirectory();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    config.setSimilarity(new ClassicSimilarity());
    IndexWriter indexWriter = new IndexWriter(directory, config);

    TestUtil.addNumDocsWithTextField("f1", "v1", indexWriter, 4);
    TestUtil.addNumDocsWithTextField("f2", "v1 v1", indexWriter, 1);

    indexWriter.close();

    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    indexSearcher.setSimilarity(new ClassicSimilarity());


    DocumentFrequencyCorrection dfc = new DocumentFrequencyCorrection();

    Term qTerm1 = new Term("f1", "v1");
    Term qTerm2 = new Term("f2", "v1");
    dfc.newClause();
    dfc.prepareTerm(qTerm1);
    dfc.prepareTerm(qTerm2);
    dfc.finishedUserQuery();

    DependentTermQueryBuilder.DependentTermQuery query1 = new DependentTermQueryBuilder(dfc)
            .createTermQuery(qTerm1, fieldBoost1);
    DependentTermQueryBuilder.DependentTermQuery query2 = new DependentTermQueryBuilder(dfc)
            .createTermQuery(qTerm2, fieldBoost2);


    TopDocs topDocs = indexSearcher.search(query2, 10);

    final Weight weight2 = query2.createWeight(indexSearcher, ScoreMode.COMPLETE, 4.5f);
    final Explanation explain = weight2.explain(indexReader.leaves().get(0), topDocs.scoreDocs[0].doc);

    String explainText = explain.toString();
    assertTrue(explainText.contains("9.0 = boost")); // 4.5 (query) * 2.0 (field)
    assertTrue(explainText.contains("4 = docFreq")); // 4 * df of f1:v1
    assertTrue(explainText.contains("2.0 = freq")); // don't use tf

    indexReader.close();
    directory.close();
    analyzer.close();

}
 
Example 19
Source File: FieldBoostTermQueryBuilderTest.java    From querqy with Apache License 2.0 3 votes vote down vote up
@Test
public void testCreateWeight() throws Exception {

    Analyzer analyzer = new KeywordAnalyzer();

    Directory directory = newDirectory();
    RandomIndexWriter indexWriter = new RandomIndexWriter(random(), directory, analyzer);

    TestUtil.addNumDocsWithStringField("f1", "v1", indexWriter, 4);
    TestUtil.addNumDocsWithStringField("f1", "v2", indexWriter, 1);

    indexWriter.close();

    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = newSearcher(indexReader);
    indexSearcher.setSimilarity(new ClassicSimilarity());


    Term term = new Term("f1", "v1");

    FieldBoostTermQueryBuilder.FieldBoostTermQuery query = new FieldBoostTermQueryBuilder().createTermQuery(term, fieldBoost2);

    TopDocs topDocs = indexSearcher.search(query, 10);
    final Weight weight = query.createWeight(indexSearcher, ScoreMode.COMPLETE, 4.5f);
    final Explanation explain = weight.explain(indexReader.getContext().leaves().get(0), topDocs.scoreDocs[0].doc);

    String explainText = explain.toString();

    assertTrue(explainText.contains("4.5 = queryBoost")); // 4.5 (query)
    assertTrue(explainText.contains("2.0 = fieldBoost")); // 2.0 (field)
    assertTrue(explainText.contains("ConstantFieldBoost(f1^2.0)")); // 2.0 (field)
    assertFalse(explainText.toLowerCase().contains("freq")); // no doc freq

    indexReader.close();
    directory.close();
    analyzer.close();

}
 
Example 20
Source File: SimilarityTermQueryBuilderTest.java    From querqy with Apache License 2.0 2 votes vote down vote up
@Test
public void testCreateWeight() throws Exception {

    Analyzer analyzer = new StandardAnalyzer();

    Directory directory = new ByteBuffersDirectory();
    IndexWriterConfig config = new IndexWriterConfig(analyzer);
    config.setSimilarity(new ClassicSimilarity());
    IndexWriter indexWriter = new IndexWriter(directory, config);


    TestUtil.addNumDocsWithTextField("f1", "v1 v1", indexWriter, 4);
    TestUtil.addNumDocsWithTextField("f1", "v2", indexWriter, 1);

    indexWriter.close();

    IndexReader indexReader = DirectoryReader.open(directory);
    IndexSearcher indexSearcher = new IndexSearcher(indexReader);
    indexSearcher.setSimilarity(new ClassicSimilarity());


    Term term = new Term("f1", "v1");

    SimilarityTermQuery query = new SimilarityTermQueryBuilder().createTermQuery(term, fieldBoost2);

    TopDocs topDocs = indexSearcher.search(query, 10);

    final Weight weight = query.createWeight(indexSearcher, ScoreMode.COMPLETE, 4.5f);
    final Explanation explain = weight.explain(indexReader.getContext().leaves().get(0), topDocs.scoreDocs[0].doc);

    String explainText = explain.toString();

    assertTrue(explainText.contains("9.0 = boost")); // 4.5 (query) * 2.0 (field)
    assertTrue(explainText.contains("4 = docFreq")); // 4 * v1
    assertTrue(explainText.contains("2.0 = freq")); // 2 * v1 in field

    indexReader.close();
    directory.close();
    analyzer.close();

}