public void testPlay() throws Exception { SpanOrQuery or = new SpanOrQuery(new SpanQuery[]{quick, fox}); dumpSpans(or); SpanNearQuery quick_fox = new SpanNearQuery(new SpanQuery[]{quick, fox}, 1, true); SpanFirstQuery sfq = new SpanFirstQuery(quick_fox, 4); dumpSpans(sfq); dumpSpans(new SpanTermQuery(new Term("f", "the"))); SpanNearQuery quick_brown = new SpanNearQuery(new SpanQuery[]{quick, brown}, 0, false); dumpSpans(quick_brown); }
public void testExtractQueryMetadata_spanFirstQuery() { SpanTermQuery spanTermQuery1 = new SpanTermQuery(new Term("_field", "_short_term")); SpanFirstQuery spanFirstQuery = new SpanFirstQuery(spanTermQuery1, 20); Result result = analyze(spanFirstQuery); assertThat(result.verified, is(false)); assertTermsEqual(result.terms, spanTermQuery1.getTerm()); }
@Override public SpanQuery getSpanQuery(Element e) throws ParserException { int end = DOMUtils.getAttribute(e, "end", 1); Element child = DOMUtils.getFirstChildElement(e); SpanQuery q = factory.getSpanQuery(child); SpanFirstQuery sfq = new SpanFirstQuery(q, end); sfq.setBoost(DOMUtils.getAttribute(e, "boost", 1.0f)); return sfq; }
private static Query[] getPrebuiltQueries(String field) { // be wary of unanalyzed text return new Query[] { new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5), new SpanNearQuery(new SpanQuery[]{new SpanTermQuery(new Term(field, "night")), new SpanTermQuery(new Term(field, "trading"))}, 4, false), new SpanNearQuery(new SpanQuery[]{new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10), new SpanTermQuery(new Term(field, "credit"))}, 10, false), new WildcardQuery(new Term(field, "fo*")), }; }
private static Query[] getPrebuiltQueries(String field) { WildcardQuery wcq = new WildcardQuery(new Term(field, "fo*")); wcq .setRewriteMethod(MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); // be wary of unanalyzed text return new Query[] { new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 5), new SpanNearQuery(new SpanQuery[] { new SpanTermQuery(new Term(field, "night")), new SpanTermQuery(new Term(field, "trading")) }, 4, false), new SpanNearQuery(new SpanQuery[] { new SpanFirstQuery(new SpanTermQuery(new Term(field, "ford")), 10), new SpanTermQuery(new Term(field, "credit")) }, 10, false), wcq, }; }
public void testSpanRegex() throws Exception { Directory directory = newDirectory(); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig(new MockAnalyzer(random()))); Document doc = new Document(); // doc.add(newField("field", "the quick brown fox jumps over the lazy dog", // Field.Store.NO, Field.Index.ANALYZED)); // writer.addDocument(doc); // doc = new Document(); doc.add(newTextField("field", "auto update", Field.Store.NO)); writer.addDocument(doc); doc = new Document(); doc.add(newTextField("field", "first auto update", Field.Store.NO)); writer.addDocument(doc); writer.forceMerge(1); writer.close(); IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = newSearcher(reader); SpanQuery srq = new SpanMultiTermQueryWrapper<>(new RegexQuery(new Term("field", "aut.*"))); SpanFirstQuery sfq = new SpanFirstQuery(srq, 1); // SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, // true); int numHits = searcher.search(sfq, null, 1000).totalHits; assertEquals(1, numHits); reader.close(); directory.close(); }
public void testSpanRegex() throws Exception { Directory directory = newDirectory(); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))); Document doc = new Document(); // doc.add(newField("field", "the quick brown fox jumps over the lazy dog", // Field.Store.NO, Field.Index.ANALYZED)); // writer.addDocument(doc); // doc = new Document(); doc.add(newTextField("field", "auto update", Field.Store.NO)); writer.addDocument(doc); doc = new Document(); doc.add(newTextField("field", "first auto update", Field.Store.NO)); writer.addDocument(doc); writer.forceMerge(1); writer.close(); IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = new IndexSearcher(reader); SpanQuery srq = new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("field", "aut.*"))); SpanFirstQuery sfq = new SpanFirstQuery(srq, 1); // SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, // true); int numHits = searcher.search(sfq, null, 1000).totalHits; assertEquals(1, numHits); reader.close(); directory.close(); }
public void testSpanRegex() throws Exception { Directory directory = newDirectory(); IndexWriter writer = new IndexWriter(directory, newIndexWriterConfig( TEST_VERSION_CURRENT, new MockAnalyzer(random()))); Document doc = new Document(); // doc.add(newField("field", "the quick brown fox jumps over the lazy dog", // Field.Store.NO, Field.Index.ANALYZED)); // writer.addDocument(doc); // doc = new Document(); doc.add(newTextField("field", "auto update", Field.Store.NO)); writer.addDocument(doc); doc = new Document(); doc.add(newTextField("field", "first auto update", Field.Store.NO)); writer.addDocument(doc); writer.forceMerge(1); writer.close(); IndexReader reader = DirectoryReader.open(directory); IndexSearcher searcher = newSearcher(reader); SpanQuery srq = new SpanMultiTermQueryWrapper<RegexQuery>(new RegexQuery(new Term("field", "aut.*"))); SpanFirstQuery sfq = new SpanFirstQuery(srq, 1); // SpanNearQuery query = new SpanNearQuery(new SpanQuery[] {srq, stq}, 6, // true); int numHits = searcher.search(sfq, null, 1000).totalHits; assertEquals(1, numHits); reader.close(); directory.close(); }
static Function<Query, Result> spanFirstQuery() { return query -> { Result result = analyze(((SpanFirstQuery) query).getMatch()); return new Result(false, result.terms); }; }
@Override protected void doAssertLuceneQuery(SpanFirstQueryBuilder queryBuilder, Query query, SearchContext context) throws IOException { assertThat(query, instanceOf(SpanFirstQuery.class)); }
public void testSpanPositionCheck() throws Exception { Directory dir = newDirectory(); // use simpleanalyzer for more natural tokenization (else "test." is a token) final Analyzer analyzer = new MockAnalyzer(random(), MockTokenizer.SIMPLE, true); IndexWriterConfig iwc = newIndexWriterConfig(analyzer); iwc.setMergePolicy(newLogMergePolicy()); RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwc); FieldType offsetsType = new FieldType(TextField.TYPE_STORED); offsetsType.setIndexOptions(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.getReader(); iw.close(); IndexSearcher searcher = newSearcher(ir); PostingsHighlighter highlighter = new PostingsHighlighter() { @Override protected Analyzer getIndexAnalyzer(String field) { return analyzer; } }; SpanQuery childQuery = new SpanMultiTermQueryWrapper<>(new WildcardQuery(new Term("body", "te*"))); Query query = new SpanFirstQuery(childQuery, 1000000); 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.close(); dir.close(); }
/** MACRO for SpanFirst(SpanTermQuery) */ public SpanFirstQuery sf(String s, int b) { return new SpanFirstQuery(st(s), b); }