public Result Search(string term, int count, int start) { try { term = term.ToLower(); Term htTerm = new Term("hottext", term); Query qq1 = new FuzzyQuery(htTerm); Query qq2 = new TermQuery(htTerm); qq2.Boost = 10f; Query qq3 = new PrefixQuery(htTerm); qq3.Boost = 10f; DisjunctionMaxQuery q1 = new DisjunctionMaxQuery(0f); q1.Add(qq1); q1.Add(qq2); q1.Add(qq3); Query q2 = new TermQuery(new Term("text", term)); q2.Boost = 3f; Query q3 = new TermQuery(new Term("examples", term)); q3.Boost = 3f; DisjunctionMaxQuery q = new DisjunctionMaxQuery(0f); q.Add(q1); q.Add(q2); q.Add(q3); TopDocs top = SearchInternal(q, count, start); Result r = new Result(term, searcher, top.ScoreDocs); Results.Add(r); return(r); } catch (IOException) { Console.WriteLine("No index in {0}", dir); return(null); } }
public void TestOverlapWithOffset() { String TEXT = "the fox did not jump"; Directory directory = NewDirectory(); IndexWriter indexWriter = new IndexWriter(directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, null)); try { Document document = new Document(); FieldType customType = new FieldType(TextField.TYPE_NOT_STORED); customType.StoreTermVectors = (true); customType.StoreTermVectorOffsets = (true); document.Add(new Field(FIELD, new OverlappingTokenStream(), customType)); indexWriter.AddDocument(document); } finally { indexWriter.Dispose(); } IndexReader indexReader = DirectoryReader.Open(directory); assertEquals(1, indexReader.NumDocs); IndexSearcher indexSearcher = NewSearcher(indexReader); try { DisjunctionMaxQuery query = new DisjunctionMaxQuery(1); query.Add(new SpanTermQuery(new Term(FIELD, "{fox}"))); query.Add(new SpanTermQuery(new Term(FIELD, "fox"))); // final Query phraseQuery = new SpanNearQuery(new SpanQuery[] { // new SpanTermQuery(new Term(FIELD, "{fox}")), // new SpanTermQuery(new Term(FIELD, "fox")) }, 0, true); TopDocs hits = indexSearcher.Search(query, 1); assertEquals(1, hits.TotalHits); Highlighter highlighter = new Highlighter( new SimpleHTMLFormatter(), new SimpleHTMLEncoder(), new QueryScorer(query)); TokenStream tokenStream = TokenSources .GetTokenStream( indexReader.GetTermVector(0, FIELD), false); assertEquals("<B>the fox</B> did not jump", highlighter.GetBestFragment(tokenStream, TEXT)); } finally { indexReader.Dispose(); directory.Dispose(); } }
public Result FastSearch(string term, int number) { try { term = term.ToLower(); Query q1 = new TermQuery(new Term("hottext", term)); Query q2 = new PrefixQuery(new Term("hottext", term)); q2.Boost = 0.5f; DisjunctionMaxQuery q = new DisjunctionMaxQuery(0f); q.Add(q1); q.Add(q2); TopDocs top = searcher.Search(q, number); return(new Result(term, searcher, top.ScoreDocs)); } catch (IOException) { Console.WriteLine("No index in {0}", dir); return(null); } }
private Query getMoreLikeQuery(string termImage, Token slopToken) { string unescaped = StringEscaper.Unescape(termImage); float slop = parseSlop(slopToken); if (string.IsNullOrEmpty(unescaped)) { return(MatchNothingQuery); } if (!_repository.IsLoadingComplete.Signaled) { return(MatchNothingQuery); } string name = unescaped.RemoveDiacritics(); var result = new DisjunctionMaxQuery(0.1f); addClauses(isToken: false); addClauses(isToken: true); return(result.Disjuncts.Count == 0 ? MatchNothingQuery : result); void addClauses(bool isToken) { if (!_repository.MapByName(isToken).TryGetValue(name, out var list)) { return; } Card c = list[0]; if (!string.IsNullOrEmpty(c.TextEn)) { result.Add(createMoreLikeThisQuery(slop, c.TextEn, nameof(Card.TextEn))); } if (!string.IsNullOrEmpty(c.GeneratedMana)) { result.Add(createMoreLikeThisQuery(slop, c.GeneratedMana, nameof(Card.GeneratedMana))); } } }
private Query getMoreLikeQuery(string termImage, Token slopToken) { string unescaped = StringEscaper.Unescape(termImage); float slop = parseSlop(slopToken); if (string.IsNullOrEmpty(unescaped)) { return(_matchNothingQuery); } if (!_repository.IsLoadingComplete) { return(_matchNothingQuery); } string cardName = unescaped.RemoveDiacritics(); if (!_repository.CardsByName.TryGetValue(cardName, out var cards)) { return(_matchNothingQuery); } var card = cards[0]; var result = new DisjunctionMaxQuery(0.1f); if (!string.IsNullOrEmpty(card.TextEn)) { result.Add(createMoreLikeThisQuery(slop, card.TextEn, nameof(card.TextEn))); } if (!string.IsNullOrEmpty(card.GeneratedMana)) { result.Add(createMoreLikeThisQuery(slop, card.GeneratedMana, nameof(card.GeneratedMana))); } if (result.Disjuncts.Count == 0) { return(_matchNothingQuery); } return(result); }
protected Query dmq(float tieBreakerMultiplier, params Query[] queries) { DisjunctionMaxQuery query = new DisjunctionMaxQuery(tieBreakerMultiplier); foreach (Query q in queries) { query.Add(q); } return(query); }
public void TestWildcardInDisjunctionMax() { Directory dir = NewDirectory(); // use simpleanalyzer for more natural tokenization (else "test." is a token) Analyzer analyzer = new MockAnalyzer(Random(), MockTokenizer.SIMPLE, true); IndexWriterConfig iwc = NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer); iwc.SetMergePolicy(NewLogMergePolicy()); RandomIndexWriter iw = new RandomIndexWriter(Random(), dir, iwc); FieldType offsetsType = new FieldType(TextField.TYPE_STORED); offsetsType.IndexOptions = (IndexOptions.DOCS_AND_FREQS_AND_POSITIONS_AND_OFFSETS); Field body = new Field("body", "", offsetsType); Document doc = new Document(); doc.Add(body); body.SetStringValue("This is a test."); iw.AddDocument(doc); body.SetStringValue("Test a one sentence document."); iw.AddDocument(doc); IndexReader ir = iw.Reader; iw.Dispose(); IndexSearcher searcher = NewSearcher(ir); PostingsHighlighter highlighter = new PostingsHighlighterAnalyzerHelper(analyzer); //PostingsHighlighter highlighter = new PostingsHighlighter() { // @Override // protected Analyzer getIndexAnalyzer(String field) //{ // return analyzer; //} // }; DisjunctionMaxQuery query = new DisjunctionMaxQuery(0); query.Add(new WildcardQuery(new Term("body", "te*"))); TopDocs topDocs = searcher.Search(query, null, 10, Sort.INDEXORDER); assertEquals(2, topDocs.TotalHits); String[] snippets = highlighter.Highlight("body", query, searcher, topDocs); assertEquals(2, snippets.Length); assertEquals("This is a <b>test</b>.", snippets[0]); assertEquals("<b>Test</b> a one sentence document.", snippets[1]); ir.Dispose(); dir.Dispose(); }
/// <summary> /// (non-Javadoc) /// @see org.apache.lucene.xmlparser.QueryObjectBuilder#process(org.w3c.dom.Element) /// </summary> public virtual Query GetQuery(XmlElement e) { float tieBreaker = DOMUtils.GetAttribute(e, "tieBreaker", 0.0f); DisjunctionMaxQuery dq = new DisjunctionMaxQuery(tieBreaker); dq.Boost = DOMUtils.GetAttribute(e, "boost", 1.0f); XmlNodeList nl = e.ChildNodes; for (int i = 0; i < nl.Count; i++) { XmlNode node = nl.Item(i); if (node is XmlElement queryElem) { // all elements are disjuncts. Query q = factory.GetQuery(queryElem); dq.Add(q); } } return(dq); }