具有两个指定单词的lucene查询语法
我是lucene的初学者。 我在文档中有一个字段名fstname。 如何检索在fstname字段中同时包含单词“vamshi”和“sai”的文档具有两个指定单词的lucene查询语法,lucene,Lucene,我是lucene的初学者。 我在文档中有一个字段名fstname。 如何检索在fstname字段中同时包含单词“vamshi”和“sai”的文档 public class Indexer { public Indexer() {} private IndexWriter indexWriter = null; public IndexWriter getIndexWriter(boolean create) throws IOException { if (indexWriter =
public class Indexer
{
public Indexer() {}
private IndexWriter indexWriter = null;
public IndexWriter getIndexWriter(boolean create) throws IOException
{
if (indexWriter == null)
{
File file=new File("D:/index-directory");
Path dirPath = file.toPath();
Directory indexDir = FSDirectory.open(file);
IndexWriterConfig config = new IndexWriterConfig(Version.LUCENE_4_10_2,new StandardAnalyzer());
indexWriter = new IndexWriter(indexDir, config);
}
return indexWriter;
}
public void closeIndexWriter() throws IOException
{
if (indexWriter != null)
{
indexWriter.close();
}
}
public void indexHotel(Hotel hotel) throws IOException
{
IndexWriter writer = getIndexWriter(false);
Document doc = new Document();
doc.add(new StringField("id", hotel.getId(), Field.Store.YES));
doc.add(new StringField("fstname", hotel.getFstname(), Field.Store.YES));
doc.add(new StringField("lastname", hotel.getLastname(), Field.Store.YES));
doc.add(new LongField("mobileno", hotel.getMobileno(), Field.Store.YES));
String fullSearchableText = hotel.getId()+" "+hotel.getFstname()+ " " + hotel.getLastname() + " " + hotel.getMobileno();
doc.add(new TextField("content", fullSearchableText, Field.Store.NO));
writer.addDocument(doc);
}
public void rebuildIndexes() throws IOException
{
getIndexWriter(true);
indexWriter.deleteAll();
Hotel[] hotels = HotelDatabase.getHotels();
for(Hotel hotel : hotels)
{
indexHotel(hotel);
}
closeIndexWriter();
}
}
public class SearchEngine
{
private IndexSearcher searcher = null;
private QueryParser parser = null;
/** Creates a new instance of SearchEngine */
public SearchEngine() throws IOException
{
File file=new File("D:/index-directory");
Path dirPath = file.toPath();
searcher = new IndexSearcher(DirectoryReader.open(FSDirectory.open(file)));
parser = new QueryParser("content", new StandardAnalyzer());
}
public TopDocs performSearch(String queryString, int n)
throws IOException, ParseException
{
Query query = parser.parse(queryString);
return searcher.search(query, n);
}
public Document getDocument(int docId)
throws IOException {
return searcher.doc(docId);
}
}
public class HotelDatabase
{
private static final Hotel[] HOTELS = {
new Hotel("1","vamshi","chinta",9158191135L),
new Hotel("2","vamshi krishna","chinta",9158191136L),
new Hotel("3","krishna","chinta",9158191137L),
new Hotel("4","vamshi","something",9158191138L),
new Hotel("5","venky","abc",123456789L),
new Hotel("6","churukoti","def",123456789L),
new Hotel("7","chinta","vamshi",9158191139L),
new Hotel("8","chinta","krishna vamshi",9158191139L),
};
public static Hotel[] getHotels() {
return HOTELS;
}
public static Hotel getHotel(String id) {
for(Hotel hotel : HOTELS) {
if (id.equals(hotel.getId())) {
return hotel;
}
}
return null;
}
}
public class Hotel
{
private String fstname;
private String lastname;
private long mobileno;
private String id;
public void setMobileno(long mobileno) {
this.mobileno = mobileno;
}
public Hotel()
{
}
public Hotel(String id,
String fstname,
String lastname,
Long mobileno) {
this.id = id;
this.fstname = fstname;
this.lastname = lastname;
this.mobileno = mobileno;
}
public String getFstname() {
return fstname;
}
public void setFstname(String fstname) {
this.fstname = fstname;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public long getMobileno() {
return mobileno;
}
public void setMobileno(int mobileno) {
this.mobileno = mobileno;
}
public String toString() {
return "Hotel "
+ getId()
+": "
+ getFstname()
+" ("
+ getLastname()
+")";
}
}
现在,当我使用查询进行搜索时
TopDocs TopDocs=新搜索引擎().performSearch(“fstname:vamshi和fstname:krishna”,100)
它没有返回fstname为“vamshi krishna”的文件 我的代码中有什么问题???请尝试使用以下方法:
fstname:vamshi*sai OR fstname:sai*vamshi
如您所见,这是在搜索文本模式。这可能会有性能问题。
尝试查找更多信息 这是一个简单的布尔AND查询: fstname:vamshi和fstname:sai StandardQueryParser将此转换为查询: +fstname:vamshi+fstname:sai 编辑: 您的代码中有一个问题。您正在使用
StringFields
存储酒店名称。但是,StringFields
只被索引,而没有标记。()这意味着它们没有被分解成单独的代币。如果你加上“瓦姆希克里希纳”,那么这不是被标记为“瓦姆希”和“克里希纳”,而是被存储为“瓦姆希克里希纳”
尝试使用常规的
TextField
,它应该可以工作。我的意思是它应该检索所有具有fstname字段值的文档,如“sai vamshi krishna”或“vamshi bcjhdsbcb sai jhsbxjhabxh vamshi”或“ghxvsdhcb sai jhsbxjhabxh vamshi”你能检查我的代码吗,我修改了我的问题你能检查我的代码吗,我修改了我的问题1。您是否检查了正确数量的酒店已编入索引?您可以使用indexWriter.maxDoc()
获取索引中的文档数。2.您是否检查了解析器是否构造了正确的查询?3.您可能想指定正在分析文档中的哪些字段,或者只是想知道问题出在哪里,请参阅上面我编辑的答案谢谢@dennis。更改为textfield解决了我的问题。你为我节省了很多时间:-)