【发布时间】:2015-02-19 10:11:45
【问题描述】:
我希望使用以下字段开发一个简单的搜索
- 标题
- 总结
- 人气
如果有人搜索“ga”,我会搜索标题中的部分匹配项(例如“The Game”),然后按受欢迎程度对这些结果进行排序。
如果有
例如搜索“ga*”
"The Game" | "About stuff" | popularity = 3 | (title match)
"Gant charts" | "great stats" | popularity = 7 | (title match)
"Some Title" | "mind the gap" | popularity = 1 | (summary match)
"Another Title" | "blah games" | popularity = 5 | (summary match)
我编写了一个简单的实现,它执行 1 次 Lucene 搜索,如果有
这可能吗?如果可以,怎么做?
(我目前正在使用 Java Lucene jar 进行开发)
这是我目前的尝试(用 Scala 编写)
// Creating the indexes
private def addDoc(w:IndexWriter , clientContent: ClientContent, contentType:String):Unit ={
val doc:Document = new Document()
doc.add(new TextField("title", clientContent.title, Field.Store.YES))
doc.add(new TextField("synopsis", clientContent.synopsis, Field.Store.YES))
doc.add(new StringField("id", clientContent.id, Field.Store.YES))
doc.add(new IntField("popularity", 100000 - clientContent.popularity.day, Field.Store.YES))
doc.add(new StringField("contentType", contentType, Field.Store.YES))
w.addDocument(doc);
}
def createIndex: Unit = {
index = new RAMDirectory()
val analyzer = new StandardAnalyzer(Version.LUCENE_43)
val config = new IndexWriterConfig(Version.LUCENE_43, analyzer)
val w = new IndexWriter(index, config)
clientApplication.shows.list.map {addDoc(w, _, "Show")}
w.close()
reader = IndexReader.open(index)
searcher = new IndexSearcher(reader)
}
// Searching by one field
def dataSearch(queryString: String, section:String, limit:Int):Array[ScoreDoc] = {
val collector = TopFieldCollector.create(
new Sort(new SortField("popularity", SortField.Type.INT, true)),
limit,false,true, true, false);
val analyzer = new StandardAnalyzer(Version.LUCENE_43)
val q = new QueryParser(Version.LUCENE_43, section, analyzer).parse(queryString+"*")
searcher.search(q, collector)
collector.topDocs().scoreDocs
}
// Searching for a query
def search(queryString:String) = {
println(s"search $queryString")
val titleResults = dataSearch(queryString, "title", limit)
if (titleResults.length < limit) {
val synopsisResults = dataSearch(queryString, "synopsis", limit - titleResults.length)
createModel(titleResults ++ synopsisResults)
}
else
createModel(titleResults)
}
【问题讨论】:
-
那么,让我们看看到目前为止的尝试
-
我的第一次尝试添加了 - 它只是原型代码,所以它可能不是最好的方法
标签: lucene