作为背景,我在我的应用程序中使用 Grails v2.2.1 和 Searchable 插件 (v0.6.4),尽管在配置 Lucene 方面我是新手。
日志显示搜索需要 26 毫秒,但罗盘事务大约需要 15 秒才能返回:
2013-04-23 00:40:34,269 DEBUG grails.plugin.searchable.internal.
compass.search.DefaultSearchMethod - query: [+kind:band +name:snoop], [4] hits, took [26] millis
2013-04-23 00:40:49,965 DEBUG org.compass.core.transaction.LocalTransaction - Committing local transaction on thread [http-bio-8080-exec-10] Compass [1176020804] Session [2089649487]
与 Lucene 相比,这似乎是 Compass 的问题,因为查询执行速度很快,但 Compass 映射将我的 Java 进程固定在接近 100% CPU 并且挂起时间过长。
我索引了大约 3500 个域对象,我的域模型如下所示:我尝试仅索引字段名称和 id,但通过 Luke 看到它似乎映射了域中的所有内容。
package com.bandbot
class Band {
def slugGeneratorService
static searchable = {
mapping {
spellCheck "exclude"
only: ['name', 'id']
}
}
String name
String biography
String profileImage
String slug
String similarBands // this will store bands/url/pic in the form of Kanye West::url::img.png~Queen::url::img.png
boolean onTour // is this band currently touring? (Info from lastfm)
String mbid // This band's MusicBrainz ID see @ http://musicbrainz.org/doc/MusicBrainz_Identifier
String bandUrl
String lastFMUrl // stores the lastfm url
Date dateOfInception
Date dateDisbanded
Date lastUpdated
static belongsTo = [Genre, BandbotUser]
static hasMany = [ events : Event, genres : Genre ]
def beforeInsert() {
lastUpdated = new Date()
this.slug = slugGeneratorService.generateSlug(this.class, "slug", name)
}
def beforeUpdate() {
lastUpdated = new Date()
if (isDirty('name')) {
this.slug = slugGeneratorService.generateSlug(this.class, "slug", name)
}
}
static constraints = {
name(nullable: false, blank: false, unique: true)
slug(nullable: true)
bandUrl(nullable: true)
dateDisbanded(nullable: true)
mbid(nullable: true)
dateOfInception(nullable: true)
biography(nullable: true)
similarBands(nullable: true)
lastUpdated(nullable: true)
lastFMUrl(nullable: true)
kind( display: false )
}
static mapping = {
onTour defaultValue: false
biography type: 'text'
similarBands type: 'text'
}
String toString(){name}
}
我的控制器中的乐队搜索逻辑:
def search() {
if (!params.q?.trim()) {
return [:]
}
try {
def searchResult
if (params.sort) {
searchResult = searchableService.search(
params.q.trim(),
[offset: params.offset ? params.int('offset') : 0,
max: params.max ? params.int('max') : 10,
sort: params.sort, order: params.order? params.order : 'asc']
)
}
else {
searchResult = searchableService.search(
params.q.trim(),
[offset: params.offset ? params.int('offset') : 0,
max: params.max ? params.int('max') : 10]
)
}
return [searchResult: searchResult, params: params]
} catch (SearchEngineQueryParseException ex) {
return [parseException: true, params: params]
}
}
任何想法将不胜感激。这是我的一个自学项目,我真的很想以正确的方式进行搜索。:) 谢谢,凯文