組合關係表明的意思以下:java
一、MUST和MUST表示「與」的關係,即「並集」。apache
二、MUST和MUST_NOT前者包含後者不包含。
三、MUST_NOT和MUST_NOT沒意義
四、SHOULD與MUST表示MUST,SHOULD失去意義;
五、SHOUlD與MUST_NOT至關於MUST與MUST_NOT。eclipse
六、SHOULD與SHOULD表示「或」的概念。maven
1.pom.xml:
ui
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>hui</groupId> <artifactId>Lucene</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>Lucene</name> <url>http://maven.apache.org</url> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> </properties> <dependencies> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-core</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-analyzers-common</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-queryparser</artifactId> <version>6.0.0</version> </dependency> <dependency> <groupId>org.apache.lucene</groupId> <artifactId>lucene-highlighter</artifactId> <version>6.0.0</version> </dependency> </dependencies> </project>
package luceneTest; import java.io.File; import java.util.ArrayList; import java.util.List; public class ListFiles { private static List<File> filesResult = new ArrayList<>(); public static void main(String[] args) { List<File> files = listFiles("e:/eclipse/OOP"); System.out.println(files.size()); for (File f : files) { System.out.println(f.getName()); } } public static List<File> listFiles(String srcPath) { File file = new File(srcPath); File[] files = file.listFiles(); for (File f : files) { if (f.isDirectory()) { listFiles(f.getAbsolutePath()); } else { filesResult.add(f); } } return filesResult; } }
package extend; import java.io.IOException; import java.nio.file.Paths; import org.apache.lucene.document.Document; import org.apache.lucene.index.DirectoryReader; import org.apache.lucene.index.Term; import org.apache.lucene.search.BooleanClause; import org.apache.lucene.search.BooleanClause.Occur; import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.SimpleFSDirectory; public class BooleanTest { public static void main(String[] args) { String srcPath = "E:/eclipse/OOP"; String destPath = "e:/test/index"; // try { // FilesTest.createIndexTo(srcPath, destPath); // } catch (IOException e) { // e.printStackTrace(); // } testBooleanQuery(destPath, "fileContent", "java", "string"); } private static void testBooleanQuery(String indexPath, String filedName, String str1, String str2) { DirectoryReader reader = null; try { Directory dir = new SimpleFSDirectory(Paths.get(indexPath)); reader = DirectoryReader.open(dir); IndexSearcher searcher = new IndexSearcher(reader); Term term = new Term(filedName, str1); TermQuery tQuery = new TermQuery(term); BooleanClause clause = new BooleanClause(tQuery, Occur.MUST); Term term2 = new Term(filedName, str2); TermQuery tQuery2 = new TermQuery(term2); BooleanClause clause2 = new BooleanClause(tQuery2, Occur.MUST_NOT); BooleanQuery bQuery = new BooleanQuery.Builder().add(clause).add(clause2) .build(); TopDocs topDocs = searcher.search(bQuery, 100); System.out.println(topDocs.scoreDocs.length); for (ScoreDoc scoreDoc : topDocs.scoreDocs) { Document hitDoc = searcher.doc(scoreDoc.doc); System.out.println(hitDoc.get("filePath")); } } catch (IOException e) { e.printStackTrace(); } finally { if (reader != null) { try { reader.close(); } catch (IOException e) { e.printStackTrace(); } } } } }