我想使用ElasticSearch从我的域类中进行搜索,但这是一个嵌套查询,而我想要的匹配项是三个层次。
仅将我要搜索的域类设置为root,将所有其他域类设置为组件,而不将root类设置为可搜索配置。
当我进行搜索时,它给了我匹配项,但是计数是错误的,并且里面完全是错误的匹配项。
我应该得到4000场比赛,但是我得到20000场比赛,我不知道为什么它不能按预期工作。
当我开始摆弄时,我更改了域类之间的映射,然后为我提供了正确的匹配项,但事实是,我不想在生产环境中进行此类更改。
我将举两个例子。第一个显示了我当前域类的关系(一个不起作用),另一个显示了与更改的关系,使搜索正确进行。
无法正常工作的示例
class A {
String id
String name
B b
static searchable = {
b component: true
}
}
class B {
String id
String name
C c
static searchable = {
root false
c component: true
}
}
class C {
String id
String name
static belongsTo = [d: D]
static searchable = {
root false
d component: true
}
}
class D {
String id
String name
static searchable = {
root false
}
}
正确工作的示例
class A {
String id
String name
B b
static searchable = {
b component: true
}
}
class B {
String id
String name
C c
static searchable = {
root false
c component: true
}
}
class C {
String id
String name
D d
static searchable = {
root false
d component: true
}
}
class D {
String id
String name
static searchable = {
root false
}
}
如您所见,关系的唯一区别是,在第一个示例中,类D属于类C,在第二个示例中,类D只是类C中的一个字段。在两种情况下,数据库C中对D的引用完全相同。
我创建的搜索关闭看起来像:
bool {
must {
nested {
path = "b"
query {
nested {
path = "b.c"
query {
path = "b.c.d"
query {
bool {
must {
match("b.c.d.id": "142342342342")
}
}
}
}
}
}
}
}
}
我是否真的必须更改与域类的关系才能进行搜索,还是我做错了搜索?
一般情况下会导致什么问题?
编辑
在这两种情况下,字段“d”在“c”内的ElasticSearch映射完全相同:
"c":{
"type":"nested",
"properties":{
"class":{
"type":"string"
},
"dateCreated":{
"type":"date",
"format":"strict_date_optional_time||epoch_millis",
"include_in_all":true
},
"d":{
"type":"nested",
"properties":{
"class":{
"type":"string"
},
"id":{
"type":"string"
},
"name":{
"type":"string",
"term_vector":"with_positions_offsets",
"include_in_all":true
}
}
}
编辑2
因此看来问题似乎不在于映射,而是当搜索至少三个级别的嵌套对象时,ElasticSearch无法正确找到单词 match 的匹配项。我通过 match_phrase 使用它。
如此重要的一课,当搜索例如ID时,您的查询具有多层嵌套,并且您希望精确匹配,那么应该使用match_phrase!
最佳答案
我建议在比赛中使用match_phrase。
如果为查询字段定义了分析器,match_phrase查询将分析输入,并查找符合以下条件的文档:
关于grails - Grails 3 ElasticSearch嵌套对象查询,带有belongsTo约束,给出错误的匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43682008/