Java 将Lucene中的查询与BooleanQuery相结合
我已经创建了一个示例Lucene代码段,它为一个小文件编制索引。我能够正确执行索引并搜索单个字段值。但是,我想查询多个字段。我正在使用Java 将Lucene中的查询与BooleanQuery相结合,java,solr,lucene,Java,Solr,Lucene,我已经创建了一个示例Lucene代码段,它为一个小文件编制索引。我能够正确执行索引并搜索单个字段值。但是,我想查询多个字段。我正在使用布尔查询,但它不起作用 有人能建议吗?这是我的代码片段 import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.util.HashMap; import java.util.List; import java.util.Map; impor
布尔查询
,但它不起作用
有人能建议吗?这是我的代码片段
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Fieldable;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
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.TopScoreDocCollector;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
public class LocalFSLucene {
private final Version version = Version.LUCENE_36;
private final String indexDirectory = "/Work/Lucene/LocalFSIndex";
private final String dataFile = "/Work/Lucene/data.txt";
private final String fields[] = {"date", "time", "cs-method", "cs-uri",
"sc-status", "time-taken"};
private IndexWriterConfig config = null;
public void setConfig() {
/* Check if the IndexWriterConfiguration is available or not.
* If not, we will create one and save it for any further references.
*/
if (config == null) {
config = new IndexWriterConfig(version, new StandardAnalyzer(version));
}
}
private final String rowDelimiter = " ";
public void buildIndex() throws Exception {
/* Create the Configuration object for writing index files */
setConfig();
/* Get the handle to the directory where indexes will be created */
Directory dir = new SimpleFSDirectory(new File(indexDirectory));
/* Initialize the index writer object */
IndexWriter indexWriter = new IndexWriter(dir, config);
/* Reader object to read the data file */
BufferedReader reader = new BufferedReader(new FileReader(dataFile));
/* Read each line of the data and build the index on the fields */
String row = null;
while ((row = reader.readLine()) != null) {
/* Get each field in the current row */
String fieldValues[] = row.split(rowDelimiter);
/* Create a document for each row to store the index information */
Document doc = new Document();
for (int i = 0; i < fields.length; i++) {
doc.add(new Field(fields[i], fieldValues[i], Field.Store.YES, Field.Index.ANALYZED));
}
/* Add the document to index */
indexWriter.addDocument(doc);
}
/* Push the index files on the File System */
indexWriter.commit();
/* Close the reader object */
reader.close();
/* Close the index writer object */
indexWriter.close();
System.out.println("Indexing is complete");
}
public void search(Map<String, String> params) throws Exception {
/* Get the handle to the directory where indexes are be created */
Directory dir = new SimpleFSDirectory(new File(indexDirectory));
/* Create the Index Reader object to read the indexes created */
IndexReader reader = IndexReader.open(dir);
/* Create the detective object which will perform search operation */
IndexSearcher detective = new IndexSearcher(reader);
System.out.println("Total Number of Documents - " + detective.maxDoc());
/* Build the query containing the clues which the detective will use
* to solve the case.
*/
//Query q = new QueryParser(version, field, new StandardAnalyzer(version)).parse(value);
BooleanQuery q = new BooleanQuery();
Set<String> fields = params.keySet();
for (String field : fields) {
q.add(new TermQuery(new Term(field, params.get(field))), BooleanClause.Occur.SHOULD);
}
/* The TopScoreDocCollector will create the bag where the detective will
* put all the found clues to solve the case.
*/
TopScoreDocCollector clueBag = TopScoreDocCollector.create(10, true);
/* Ask the detective to start */
detective.search(q, clueBag);
/* Get all the clues which the detective found during investigation
* and display them.
*/
ScoreDoc clues[] = clueBag.topDocs().scoreDocs;
System.out.println("Total Clues Found - " + clues.length);
System.out.println();
for (int i = 0; i < clues.length; i++) {
/* Get the pointer to the clue */
int clueId = clues[i].doc;
/* Get the actual clue from the clue bag */
Document clue = detective.doc(clueId);
/* Print the document */
List<Fieldable> lstFields = clue.getFields();
System.out.print((i + 1) + " --> ");
for (Fieldable fld : lstFields) {
String strField = fld.name();
String strValue = clue.get(strField);
System.out.print(strField + ":" + strValue + " ");
}
System.out.println();
}
}
public static void main(String args[]) throws Exception {
LocalFSLucene obj = new LocalFSLucene();
//obj.buildIndex();
Map<String, String> searchParams = new HashMap<String, String>();
searchParams.put("cs-method", "GET");
searchParams.put("cs-uri", "/blank");
obj.search(searchParams);
}
}
下面是上面代码段下方的main()
方法:
public static void main(String args[]) throws Exception {
LocalFSLucene obj = new LocalFSLucene();
//obj.buildIndex();
Map<String, String> searchParams = new HashMap<String, String>();
searchParams.put("cs-method", "GET");
searchParams.put("cs-uri", "/blank");
obj.search(searchParams);
}
对于
QueryParser
和BooleanQuery
,查询是不同的。我在QueryParser
版本中看到一个+
符号,而另一个版本中没有该符号。检查以下各项
使用QueryParser
Query q = new QueryParser(version, "cs-method", new StandardAnalyzer(version)).parse("cs-method:GET AND cs-uri:/blank");
Total Number of Documents - 11
Query --> +cs-method:get +cs-uri:blank
Total Clues Found - 5
QueryParser的输出
Query q = new QueryParser(version, "cs-method", new StandardAnalyzer(version)).parse("cs-method:GET AND cs-uri:/blank");
Total Number of Documents - 11
Query --> +cs-method:get +cs-uri:blank
Total Clues Found - 5
使用BooleanQuery
Map<String, String> searchParams = new HashMap<String, String>();
searchParams.put("cs-method", "GET");
searchParams.put("cs-uri", "/blank");
BooleanQuery q = new BooleanQuery();
Set<String> fields = params.keySet();
for (String field : fields) {
q.add(new TermQuery(new Term(field, params.get(field))), BooleanClause.Occur.SHOULD);
}
下面是上面代码段下方的main()
方法:
public static void main(String args[]) throws Exception {
LocalFSLucene obj = new LocalFSLucene();
//obj.buildIndex();
Map<String, String> searchParams = new HashMap<String, String>();
searchParams.put("cs-method", "GET");
searchParams.put("cs-uri", "/blank");
obj.search(searchParams);
}
对于QueryParser
和BooleanQuery
,查询是不同的。我在QueryParser
版本中看到一个+
符号,而另一个版本中没有该符号。检查以下各项
使用QueryParser
Query q = new QueryParser(version, "cs-method", new StandardAnalyzer(version)).parse("cs-method:GET AND cs-uri:/blank");
Total Number of Documents - 11
Query --> +cs-method:get +cs-uri:blank
Total Clues Found - 5
QueryParser的输出
Query q = new QueryParser(version, "cs-method", new StandardAnalyzer(version)).parse("cs-method:GET AND cs-uri:/blank");
Total Number of Documents - 11
Query --> +cs-method:get +cs-uri:blank
Total Clues Found - 5
使用BooleanQuery
Map<String, String> searchParams = new HashMap<String, String>();
searchParams.put("cs-method", "GET");
searchParams.put("cs-uri", "/blank");
BooleanQuery q = new BooleanQuery();
Set<String> fields = params.keySet();
for (String field : fields) {
q.add(new TermQuery(new Term(field, params.get(field))), BooleanClause.Occur.SHOULD);
}
终于让那东西开始工作了。下面是你应该如何使用它
使用字段和参数在BooleanQuery
中构建查询
使用QueryParser
传递要分析的BooleanQuery
字符串
下面是相同的代码片段
BooleanQuery b = new BooleanQuery();
Set<String> fields = params.keySet();
StandardAnalyzer analyzer = new StandardAnalyzer(version);
b.add(new TermQuery(new Term("cs-method", "GET"), BooleanClause.Occur.SHOULD);
b.add(new TermQuery(new Term("cs-uri", "/blank"), BooleanClause.Occur.SHOULD);
Query q = new QueryParser(version, "cs-method", analyzer).parse(b.toString());
BooleanQuery b=新的BooleanQuery();
Set fields=params.keySet();
StandardAnalyzer=新的StandardAnalyzer(版本);
b、 添加(新术语查询(新术语(“cs方法”、“GET”)、BooleanClause.occure.SHOULD);
b、 添加(newtermquery(newterm(“cs-uri”,“/blank”)、BooleanClause.occure.SHOULD);
QueryQ=newQueryParser(版本,“cs方法”,analyzer).parse(b.toString());
终于让它工作了。下面是你应该如何使用它
使用字段和参数在BooleanQuery
中构建查询
使用QueryParser
传递要分析的BooleanQuery
字符串
下面是相同的代码片段
BooleanQuery b = new BooleanQuery();
Set<String> fields = params.keySet();
StandardAnalyzer analyzer = new StandardAnalyzer(version);
b.add(new TermQuery(new Term("cs-method", "GET"), BooleanClause.Occur.SHOULD);
b.add(new TermQuery(new Term("cs-uri", "/blank"), BooleanClause.Occur.SHOULD);
Query q = new QueryParser(version, "cs-method", analyzer).parse(b.toString());
BooleanQuery b=新的BooleanQuery();
Set fields=params.keySet();
StandardAnalyzer=新的StandardAnalyzer(版本);
b、 添加(新术语查询(新术语(“cs方法”、“GET”)、BooleanClause.occure.SHOULD);
b、 添加(newtermquery(newterm(“cs-uri”,“/blank”)、BooleanClause.occure.SHOULD);
QueryQ=newQueryParser(版本,“cs方法”,analyzer).parse(b.toString());
您还可以更新您正在传递的参数和搜索输出吗?您还可以更新您正在传递的参数和搜索输出吗?您是否使用luke检查了索引?查看索引是否如您所愿。我没有,但让我也检查一下。有趣的是,当我使用以下查询时,文档是f蚀刻正确。Query q=new QueryParser(版本,“cs方法”,new StandardAnalyzer(版本)).parse(“cs方法:GET和cs uri:/blank”)
打印您使用API生成的查询,并查看它与上面的查询的不同之处您要求我打印QueryParser
或BooleanQuery
的查询?您是否使用luke检查了索引?查看索引是否如您所愿。我没有,但让我检查一下。有趣的是,当我使用查询之后,文档被正确地提取。query q=new QueryParser(版本,“cs方法”,new StandardAnalyzer(版本)).parse(“cs方法:GET和cs uri:/blank”)
打印您使用API生成的查询,并查看它与上面的不同之处您要求我打印QueryParser
或BooleanQuery
的查询?