public void TestMultiValuedSortByScore() { Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random))); Document doc = new Document(); FieldType type = new FieldType(TextField.TYPE_STORED); type.StoreTermVectorOffsets = (true); type.StoreTermVectorPositions = (true); type.StoreTermVectors = (true); type.Freeze(); doc.Add(new Field("field", "zero if naught", type)); // The first two fields contain the best match doc.Add(new Field("field", "hero of legend", type)); // but total a lower score (3) than the bottom doc.Add(new Field("field", "naught of hero", type)); // two fields (4) doc.Add(new Field("field", "naught of hero", type)); writer.AddDocument(doc); FastVectorHighlighter highlighter = new FastVectorHighlighter(); ScoreOrderFragmentsBuilder fragmentsBuilder = new ScoreOrderFragmentsBuilder(); fragmentsBuilder.IsDiscreteMultiValueHighlighting = (true); IndexReader reader = DirectoryReader.Open(writer, true); String[] preTags = new String[] { "<b>" }; String[] postTags = new String[] { "</b>" }; IEncoder encoder = new DefaultEncoder(); int docId = 0; BooleanQuery query = new BooleanQuery(); query.Add(clause("field", "hero"), Occur.SHOULD); query.Add(clause("field", "of"), Occur.SHOULD); query.Add(clause("field", "legend"), Occur.SHOULD); FieldQuery fieldQuery = highlighter.GetFieldQuery(query, reader); foreach (IFragListBuilder fragListBuilder in new IFragListBuilder[] { new SimpleFragListBuilder(), new WeightedFragListBuilder() }) { String[] bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 20, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 28, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 30000, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); assertEquals("<b>hero</b> <b>of</b> <b>legend</b>", bestFragments[0]); } reader.Dispose(); writer.Dispose(); dir.Dispose(); }
public void Test3Frags() { BooleanQuery query = new BooleanQuery(); query.Add(new TermQuery(new Term(F, "a")), Occur.SHOULD); query.Add(new TermQuery(new Term(F, "c")), Occur.SHOULD); FieldFragList ffl = Ffl(query, "a b b b b b b b b b b b a b a b b b b b c a a b b"); ScoreOrderFragmentsBuilder sofb = new ScoreOrderFragmentsBuilder(); String[] f = sofb.CreateFragments(reader, 0, F, ffl, 3); assertEquals(3, f.Length); // check score order assertEquals("<b>c</b> <b>a</b> <b>a</b> b b", f[0]); assertEquals("b b <b>a</b> b <b>a</b> b b b b b c", f[1]); assertEquals("<b>a</b> b b b b b b b b b b", f[2]); }
private void matchedFieldsTestCase(bool useMatchedFields, bool fieldMatch, String fieldValue, String expected, params Query[] queryClauses) { Document doc = new Document(); FieldType stored = new FieldType(TextField.TYPE_STORED); stored.StoreTermVectorOffsets = (true); stored.StoreTermVectorPositions = (true); stored.StoreTermVectors = (true); stored.Freeze(); FieldType matched = new FieldType(TextField.TYPE_NOT_STORED); matched.StoreTermVectorOffsets = (true); matched.StoreTermVectorPositions = (true); matched.StoreTermVectors = (true); matched.Freeze(); doc.Add(new Field("field", fieldValue, stored)); // Whitespace tokenized with English stop words doc.Add(new Field("field_exact", fieldValue, matched)); // Whitespace tokenized without stop words doc.Add(new Field("field_super_exact", fieldValue, matched)); // Whitespace tokenized without toLower doc.Add(new Field("field_characters", fieldValue, matched)); // Each letter is a token doc.Add(new Field("field_tripples", fieldValue, matched)); // Every three letters is a token doc.Add(new Field("field_sliced", fieldValue.Substring(0, // Sliced at 10 chars then analyzed just like field Math.Min(fieldValue.Length - 1, 10) - 0), matched)); doc.Add(new Field("field_der_red", new CannedTokenStream( // Hacky field containing "der" and "red" at pos = 0 token("der", 1, 0, 3), token("red", 0, 0, 3) ), matched)); Analyzer analyzer = new AnalyzerWrapperAnonymousHelper(); Directory dir = NewDirectory(); IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)); writer.AddDocument(doc); FastVectorHighlighter highlighter = new FastVectorHighlighter(); IFragListBuilder fragListBuilder = new SimpleFragListBuilder(); IFragmentsBuilder fragmentsBuilder = new ScoreOrderFragmentsBuilder(); IndexReader reader = DirectoryReader.Open(writer, true); String[] preTags = new String[] { "<b>" }; String[] postTags = new String[] { "</b>" }; IEncoder encoder = new DefaultEncoder(); int docId = 0; BooleanQuery query = new BooleanQuery(); foreach (Query clause in queryClauses) { query.Add(clause, Occur.MUST); } FieldQuery fieldQuery = new FieldQuery(query, reader, true, fieldMatch); String[] bestFragments; if (useMatchedFields) { ISet <String> matchedFields = new HashSet <String>(); matchedFields.Add("field"); matchedFields.Add("field_exact"); matchedFields.Add("field_super_exact"); matchedFields.Add("field_characters"); matchedFields.Add("field_tripples"); matchedFields.Add("field_sliced"); matchedFields.Add("field_der_red"); bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", matchedFields, 25, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); } else { bestFragments = highlighter.GetBestFragments(fieldQuery, reader, docId, "field", 25, 1, fragListBuilder, fragmentsBuilder, preTags, postTags, encoder); } assertEquals(expected, bestFragments[0]); reader.Dispose(); writer.Dispose(); dir.Dispose(); }