|
Lucene example source code file (TestFieldMaskingSpanQuery.java)
The Lucene TestFieldMaskingSpanQuery.java source code
package org.apache.lucene.search.spans;
/**
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import java.util.HashSet;
import java.util.Set;
import org.apache.lucene.analysis.MockAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.CheckHits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryUtils;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public class TestFieldMaskingSpanQuery extends LuceneTestCase {
protected static Document doc(Field[] fields) {
Document doc = new Document();
for (int i = 0; i < fields.length; i++) {
doc.add(fields[i]);
}
return doc;
}
protected static Field field(String name, String value) {
return newField(name, value, Field.Store.NO, Field.Index.ANALYZED);
}
protected static IndexSearcher searcher;
protected static Directory directory;
protected static IndexReader reader;
@BeforeClass
public static void beforeClass() throws Exception {
directory = newDirectory();
RandomIndexWriter writer= new RandomIndexWriter(random, directory, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
writer.addDocument(doc(new Field[] { field("id", "0")
,
field("gender", "male"),
field("first", "james"),
field("last", "jones") }));
writer.addDocument(doc(new Field[] { field("id", "1")
,
field("gender", "male"),
field("first", "james"),
field("last", "smith")
,
field("gender", "female"),
field("first", "sally"),
field("last", "jones") }));
writer.addDocument(doc(new Field[] { field("id", "2")
,
field("gender", "female"),
field("first", "greta"),
field("last", "jones")
,
field("gender", "female"),
field("first", "sally"),
field("last", "smith")
,
field("gender", "male"),
field("first", "james"),
field("last", "jones") }));
writer.addDocument(doc(new Field[] { field("id", "3")
,
field("gender", "female"),
field("first", "lisa"),
field("last", "jones")
,
field("gender", "male"),
field("first", "bob"),
field("last", "costas") }));
writer.addDocument(doc(new Field[] { field("id", "4")
,
field("gender", "female"),
field("first", "sally"),
field("last", "smith")
,
field("gender", "female"),
field("first", "linda"),
field("last", "dixit")
,
field("gender", "male"),
field("first", "bubba"),
field("last", "jones") }));
reader = writer.getReader();
writer.close();
searcher = newSearcher(reader);
}
@AfterClass
public static void afterClass() throws Exception {
searcher.close();
searcher = null;
reader.close();
reader = null;
directory.close();
directory = null;
}
protected void check(SpanQuery q, int[] docs) throws Exception {
CheckHits.checkHitCollector(random, q, null, searcher, docs);
}
public void testRewrite0() throws Exception {
SpanQuery q = new FieldMaskingSpanQuery
(new SpanTermQuery(new Term("last", "sally")) , "first");
q.setBoost(8.7654321f);
SpanQuery qr = (SpanQuery) searcher.rewrite(q);
QueryUtils.checkEqual(q, qr);
Set<Term> terms = new HashSet
Other Lucene examples (source code examples)Here is a short list of links related to this Lucene TestFieldMaskingSpanQuery.java source code file: |
| ... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2024 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.