Lucene查询语法 [英] Lucene Query Syntax
问题描述
我正在尝试使用Lucene查询具有以下结构的域
I'm trying to use Lucene to query a domain that has the following structure
Student 1-------* Attendance *---------1 Course
域中的数据汇总如下
Course.name Attendance.mandatory Student.name
-------------------------------------------------
cooking N Bob
art Y Bob
如果我执行查询"courseName:cooking AND mandatory:Y"
,它将返回Bob,因为Bob正在参加烹饪课程,而Bob也正在参加必修课程.但是,我真正要查询的是参加强制性烹饪课程的学生",在这种情况下,这将不会返回任何人.
If I execute the query "courseName:cooking AND mandatory:Y"
it returns Bob, because Bob is attending the cooking course, and Bob is also attending a mandatory course. However, what I really want to query for is "students attending a mandatory cooking course", which in this case would return nobody.
是否可以将其表达为Lucene查询?我实际上是在使用Compass,而不是直接使用Lucene,因此可以使用
Is it possible to formulate this as a Lucene query? I'm actually using Compass, rather than Lucene directly, so I can use either CompassQueryBuilder or Lucene's query language.
为完整起见,域类本身如下所示.这些类是Grails域类,但是我使用的是标准的Compass批注和Lucene查询语法.
For the sake of completeness, the domain classes themselves are shown below. These classes are Grails domain classes, but I'm using the standard Compass annotations and Lucene query syntax.
@Searchable
class Student {
@SearchableProperty(accessor = 'property')
String name
static hasMany = [attendances: Attendance]
@SearchableId(accessor = 'property')
Long id
@SearchableComponent
Set<Attendance> getAttendances() {
return attendances
}
}
@Searchable(root = false)
class Attendance {
static belongsTo = [student: Student, course: Course]
@SearchableProperty(accessor = 'property')
String mandatory = "Y"
@SearchableId(accessor = 'property')
Long id
@SearchableComponent
Course getCourse() {
return course
}
}
@Searchable(root = false)
class Course {
@SearchableProperty(accessor = 'property', name = "courseName")
String name
@SearchableId(accessor = 'property')
Long id
}
推荐答案
您尝试做的事情有时被称为范围搜索"或"xml搜索"-基于一组相关子元素进行搜索的能力. Lucene本身不支持此功能,但是您可以采取一些技巧使其正常工作.
What you are trying to do is sometimes known as "scoped search" or "xml search" - the ability to search based on a set of related sub-elements. Lucene does not support this natively but there are some tricks you can do to get it to work.
您可以将与学生相关的所有课程数据放在单个字段中.然后,将每个课程的学期位置增加一定的数量(如100).然后,您可以使用短语查询或跨度查询进行邻近搜索,以强制匹配单个课程的属性.这就是Solr支持多值字段的方式.
You can put all of the course data associated with a student in a single field. Then bump the term position by a fixed amount (like 100) between the terms for each course. You can then do a proximity search with phrase queries or span queries to force a match for attributes of a single course. This is how Solr supports multi-valued fields.
这篇关于Lucene查询语法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!