List of usage examples for org.apache.lucene.search BooleanQuery BooleanQuery
BooleanQuery
From source file:apm.common.core.DaoImpl.java
License:Open Source License
/** * ?//from w w w. j a v a 2 s . com */ public BooleanQuery getFullTextQuery(BooleanClause... booleanClauses) { BooleanQuery booleanQuery = new BooleanQuery(); for (BooleanClause booleanClause : booleanClauses) { booleanQuery.add(booleanClause); } return booleanQuery; }
From source file:apm.common.core.DaoImpl.java
License:Open Source License
/** * ?/*from www.ja v a2s .c om*/ * @param q * @param fields * @return */ public BooleanQuery getFullTextQuery(String q, String... fields) { Analyzer analyzer = new IKAnalyzer(); BooleanQuery query = new BooleanQuery(); try { if (StringUtils.isNotBlank(q)) { for (String field : fields) { QueryParser parser = new QueryParser(Version.LUCENE_36, field, analyzer); query.add(parser.parse(q), Occur.SHOULD); } } } catch (ParseException e) { e.printStackTrace(); } return query; }
From source file:at.ac.univie.mminf.luceneSKOS.AbstractTermExpansionTest.java
License:Apache License
/** * This test indexes a sample metadata record (=lucene document) having a * "title", "description", and "subject" field, which contains plain subject * terms.//from w w w . j a v a 2 s. co m * * A search for "arms" doesn't return that record because the term "arms" is * not explicitly contained in the record (document). * * @throws IOException * @throws LockObtainFailedException * @throws CorruptIndexException */ @Test public void noExpansion() throws CorruptIndexException, LockObtainFailedException, IOException { /* defining the document to be indexed */ Document doc = new Document(); doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("description", "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..." + "The spear was mainly a thrusting weapon, but could also be thrown. " + "It was the principal weapon of the auxiliary soldier... " + "(second - fourth century, Arbeia Roman Fort).", Field.Store.NO, Field.Index.ANALYZED)); doc.add(new Field("subject", "weapons", Field.Store.NO, Field.Index.ANALYZED)); /* setting up a writer with a default (simple) analyzer */ writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Version.LUCENE_36, new SimpleAnalyzer(Version.LUCENE_36))); /* adding the document to the index */ writer.addDocument(doc); /* defining a query that searches over all fields */ BooleanQuery query = new BooleanQuery(); query.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD); query.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD); /* creating a new searcher */ searcher = new IndexSearcher(IndexReader.open(writer, false)); TopDocs results = searcher.search(query, 10); /* no results are returned since there is no term match */ Assert.assertEquals(0, results.totalHits); }
From source file:at.ac.univie.mminf.luceneSKOS.LabelbasedTermExpansionTest.java
License:Apache License
/** * This test indexes a sample metadata record (=lucene document) having a * "title", "description", and "subject" field. * //from w ww. j a v a 2s . c o m * A search for "arms" returns that record as a result because "arms" is * defined as an alternative label for "weapons", the term which is contained * in the subject field. * * @throws IOException */ @Test public void labelBasedTermExpansion() throws IOException { /* defining the document to be indexed */ Document doc = new Document(); doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("description", "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..." + "The spear was mainly a thrusting weapon, but could also be thrown. " + "It was the principal weapon of the auxiliary soldier... " + "(second - fourth century, Arbeia Roman Fort).", Field.Store.NO, Field.Index.ANALYZED)); doc.add(new Field("subject", "weapons", Field.Store.NO, Field.Index.ANALYZED)); /* setting up the SKOS analyzer */ String skosFile = "src/test/resources/skos_samples/ukat_examples.n3"; /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */ Analyzer skosAnalyzer = new SKOSAnalyzer(matchVersion, skosFile, ExpansionType.LABEL); /* Define different analyzers for different fields */ Map<String, Analyzer> analyzerPerField = new HashMap<String, Analyzer>(); analyzerPerField.put("subject", skosAnalyzer); PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(matchVersion), analyzerPerField); /* setting up a writer with a default (simple) analyzer */ writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(matchVersion, indexAnalyzer)); /* adding the document to the index */ writer.addDocument(doc); /* defining a query that searches over all fields */ BooleanQuery query1 = new BooleanQuery(); query1.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD); query1.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD); query1.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD); /* creating a new searcher */ searcher = new IndexSearcher(IndexReader.open(writer, false)); TopDocs results = searcher.search(query1, 10); /* the document matches because "arms" is among the expanded terms */ Assert.assertEquals(1, results.totalHits); /* defining a query that searches for a broader concept */ Query query2 = new TermQuery(new Term("subject", "military equipment")); results = searcher.search(query2, 10); /* ... also returns the document as result */ Assert.assertEquals(1, results.totalHits); }
From source file:at.ac.univie.mminf.luceneSKOS.URIbasedTermExpansionTest.java
License:Apache License
/** * This test indexes a sample metadata record (=lucene document) having a * "title", "description", and "subject" field, which is semantically enriched * by a URI pointing to a SKOS concept "weapons". * /*from w w w . j a v a2 s.c o m*/ * A search for "arms" returns that record as a result because "arms" is * defined as an alternative label (altLabel) for the concept "weapons". * * @throws IOException */ @Test public void uriBasedTermExpansion() throws IOException { /* defining the document to be indexed */ Document doc = new Document(); doc.add(new Field("title", "Spearhead", Field.Store.YES, Field.Index.ANALYZED)); doc.add(new Field("description", "Roman iron spearhead. The spearhead was attached to one end of a wooden shaft..." + "The spear was mainly a thrusting weapon, but could also be thrown. " + "It was the principal weapon of the auxiliary soldier... " + "(second - fourth century, Arbeia Roman Fort).", Field.Store.NO, Field.Index.ANALYZED)); doc.add(new Field("subject", "http://www.ukat.org.uk/thesaurus/concept/859", Field.Store.NO, Field.Index.ANALYZED)); /* setting up the SKOS analyzer */ String skosFile = "src/test/resources/skos_samples/ukat_examples.n3"; /* ExpansionType.URI->the field to be analyzed (expanded) contains URIs */ Analyzer skosAnalyzer = new SKOSAnalyzer(matchVersion, skosFile, ExpansionType.URI); /* Define different analyzers for different fields */ Map<String, Analyzer> analyzerPerField = new HashMap<String, Analyzer>(); analyzerPerField.put("subject", skosAnalyzer); PerFieldAnalyzerWrapper indexAnalyzer = new PerFieldAnalyzerWrapper(new SimpleAnalyzer(matchVersion), analyzerPerField); /* setting up a writer with a default (simple) analyzer */ writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(matchVersion, indexAnalyzer)); /* adding the document to the index */ writer.addDocument(doc); /* defining a query that searches over all fields */ BooleanQuery query1 = new BooleanQuery(); query1.add(new TermQuery(new Term("title", "arms")), BooleanClause.Occur.SHOULD); query1.add(new TermQuery(new Term("description", "arms")), BooleanClause.Occur.SHOULD); query1.add(new TermQuery(new Term("subject", "arms")), BooleanClause.Occur.SHOULD); /* creating a new searcher */ searcher = new IndexSearcher(IndexReader.open(writer, false)); TopDocs results = searcher.search(query1, 10); /* the document matches because "arms" is among the expanded terms */ Assert.assertEquals(1, results.totalHits); /* defining a query that searches for a broader concept */ Query query2 = new TermQuery(new Term("subject", "military equipment")); results = searcher.search(query2, 10); /* ... also returns the document as result */ Assert.assertEquals(1, results.totalHits); }
From source file:au.org.ala.biocache.util.ALANameSearcherExt.java
License:Open Source License
private Query buildAutocompleteQuery(String field, String q, boolean allSearches) { //best match/*w w w. j av a 2 s . c o m*/ Query fq1 = new TermQuery(new Term(field, q)); //exact match fq1.setBoost(12f); //partial matches Query fq5 = new WildcardQuery(new Term(field, q + "*")); //begins with that begins with Query fq6 = new WildcardQuery(new Term(field, "* " + q + "*")); //contains word that begins with //any match Query fq7 = new WildcardQuery(new Term(field, "*" + q + "*")); //any match //join BooleanQuery o = new BooleanQuery(); o.add(fq1, BooleanClause.Occur.SHOULD); o.add(fq5, BooleanClause.Occur.SHOULD); o.add(fq6, BooleanClause.Occur.SHOULD); o.add(fq7, BooleanClause.Occur.SHOULD); return o; }
From source file:au.org.ala.biocache.util.ALANameSearcherExt.java
License:Open Source License
/** * Basic autocomplete. All matches are resolved to accepted LSID. * * @param q//www .ja v a 2 s . c om * @param max * @param includeSynonyms * @return */ public List<Map> autocomplete(String q, int max, boolean includeSynonyms) { try { if (false) { return null; } else { Map<String, Map> output = new HashMap<String, Map>(); //more queries for better scoring values String lq = q.toLowerCase(); String uq = q.toUpperCase(); //name search Query fq = buildAutocompleteQuery("name", lq, false); BooleanQuery b = new BooleanQuery(); b.add(fq, BooleanClause.Occur.MUST); b.add(new WildcardQuery(new Term("left", "*")), includeSynonyms ? BooleanClause.Occur.SHOULD : BooleanClause.Occur.MUST); TopDocs results = getIdentifierIdxSearcher().search(b, max); appendAutocompleteResults(output, results, includeSynonyms, false); //format search term for the current common name index uq = concatName(uq).toUpperCase(); //common name search fq = buildAutocompleteQuery("common", uq, true); results = getVernIdxSearcher().search(fq, max); appendAutocompleteResults(output, results, includeSynonyms, true); return new ArrayList(output.values()); } } catch (Exception e) { e.printStackTrace(); } return null; }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public Node retrieveFromIndex(String field, String key, final int mode) throws IOException, ObjectNotFoundException { IndexSearcher searcher = null;//w w w . j a v a 2 s . com Document doc = null; int layerInStorage = mode; BooleanQuery query = null; try { searcher = this.getIndexSearcher(); query = new BooleanQuery(); Query mainQuery = new TermQuery(new Term(field, key)); query.add(mainQuery, BooleanClause.Occur.MUST); for (int i = mode; i >= LIVE_MODE; i--) { if (i != LIVE_MODE && !isDraftNode(key, i)) { continue; } query = new BooleanQuery(); query.add(mainQuery, BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(LuceneManager.LAYER_OF_SAVE, i + "")), BooleanClause.Occur.MUST); Hits hits = searcher.search(query); if (hits.length() > 0) { doc = hits.doc(0); layerInStorage = i; break; } } } catch (IOException ioex) { if (!("_id".equals(field) && ("0".equals(key) || "1".equals(key) || "2".equals(key)))) { app.logError( ErrorReporter.errorMsg(this.getClass(), "retrieveFromIndex") + "Occured on query " + query, ioex); } doc = null; } catch (Exception ex) { if (!("_id".equals(field) && ("0".equals(key) || "1".equals(key) || "2".equals(key)))) { app.logError( ErrorReporter.errorMsg(this.getClass(), "retrieveFromIndex") + "Occured on query " + query, ex); } } finally { this.releaseIndexSearcher(searcher); } if (doc == null) { if (this.app.debug()) app.logEvent("LuceneManager.retrieveFromIndex(" + field + "," + key + "," + mode + ") executed query [" + query + "] and retrieved 0 documents"); throw new ObjectNotFoundException("No documents exist for key '" + key + "'"); } /*if (this.app.debug()) app.logEvent("LuceneManager.retrieveFromIndex(" + field + "," + key + "," + mode + ") executed query [" + query + "] and got document on layer " + layerInStorage);*/ Node node = null; try { node = docToNode(doc, mode, layerInStorage); } catch (IOException ioex) { throw ioex; } catch (ObjectNotFoundException onfe) { throw onfe; } catch (Exception ex) { throw new ObjectNotFoundException("Error searching for children of '" + key + "'"); } return node; }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public Key retrieveKeyFromIndex(String field, String key, String parentid, final int mode) throws IOException, ObjectNotFoundException { IndexSearcher searcher = null;// w w w . j ava2 s.c o m Document doc = null; BooleanQuery query = null; try { searcher = this.getIndexSearcher(); Query t1 = new TermQuery(new Term(field, key)); Query t2 = new TermQuery(new Term(LuceneManager.PARENTID, parentid)); for (int i = mode; i >= LIVE_MODE; i--) { query = new BooleanQuery(); query.add(t1, BooleanClause.Occur.MUST); query.add(t2, BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(LuceneManager.LAYER_OF_SAVE, i + "")), BooleanClause.Occur.MUST); Hits hits = searcher.search(query); int length = hits.length(); if (length > 0) { for (int j = 0; j < length; j++) { Document d = hits.doc(j); String id = d.get(LuceneManager.ID); if ((i == LIVE_MODE || isDraftNode(id, i)) && !idExistsOnHigherLayers(id, i, mode)) { doc = d; break; } } } if (doc != null) { break; } } } catch (Exception ex) { app.logError( ErrorReporter.errorMsg(this.getClass(), "retrieveKeyFromIndex") + "Occured on query " + query, ex); } finally { this.releaseIndexSearcher(searcher); } if (doc == null) { if (this.app.debug()) app.logEvent("LuceneManager.retrieveKeyFromIndex(" + field + "," + key + "," + parentid + "," + mode + ") executed query [" + query + "] and didnt find a key"); throw new ObjectNotFoundException("No documents exist for key '" + key + "'"); } DbMapping dbmap = this.app.getNodeManager().getDbMapping(doc.get(PROTOTYPE)); String id = doc.get(ID); DbKey dbkey = new DbKey(dbmap, id, mode); /*if (this.app.debug()) app.logEvent("LuceneManager.retrieveKeyFromIndex(" + field + "," + key + "," + parentid + "," + mode + ") executed query [" + query + "] and got key = " + dbkey);*/ return dbkey; }
From source file:axiom.objectmodel.dom.LuceneManager.java
License:Open Source License
public Node retrieveFromIndexFixedMode(String key, final int mode, final boolean ignoreDraftSettings) throws IOException, ObjectNotFoundException { IndexSearcher searcher = null;//from w w w.j av a2 s .c o m Document doc = null; int layerInStorage = mode; BooleanQuery query = null; try { final boolean isDraftOn = isDraftNode(key, mode); if (isDraftOn || ignoreDraftSettings) { query = new BooleanQuery(); Query mainQuery = new TermQuery(new Term(ID, key)); query.add(mainQuery, BooleanClause.Occur.MUST); query.add(new TermQuery(new Term(LuceneManager.LAYER_OF_SAVE, mode + "")), BooleanClause.Occur.MUST); searcher = this.getIndexSearcher(); Hits hits = searcher.search(query); if (hits.length() > 0) { doc = hits.doc(0); } } } catch (IOException ioex) { if (!("_id".equals(ID) && ("0".equals(key) || "1".equals(key) || "2".equals(key)))) { app.logError(ErrorReporter.errorMsg(this.getClass(), "retrieveNodeFromIndexFixedMode") + "Occured on query " + query, ioex); } doc = null; } catch (Exception ex) { if (!("_id".equals(ID) && ("0".equals(key) || "1".equals(key) || "2".equals(key)))) { app.logError(ErrorReporter.errorMsg(this.getClass(), "retrieveNodeFromIndexFixedMode") + "Occured on query " + query, ex); } } finally { this.releaseIndexSearcher(searcher); } if (doc == null) { if (this.app.debug()) app.logEvent("LuceneManager.retrieveFromIndexFixedMode(" + key + "," + mode + "," + ignoreDraftSettings + ") executed query [" + query + "] and retrieved 0 documents"); throw new ObjectNotFoundException("No documents exist for key '" + key + "'"); } /*if (this.app.debug()) app.logEvent("LuceneManager.retrieveFromIndexFixedMode(" + key + "," + mode + "," + ignoreDraftSettings + ") executed query [" + query + "] and got document on layer " + layerInStorage);*/ Node node = null; try { node = docToNode(doc, mode, layerInStorage); } catch (IOException ioex) { throw ioex; } catch (ObjectNotFoundException onfe) { throw onfe; } catch (Exception ex) { throw new ObjectNotFoundException("Error searching for children of '" + key + "'"); } return node; }