我想通过elasticsearch从我的域类搜索,但这是一个嵌套的查询,我想要的是三个级别。
我从我要搜索的域类仅设置为根,所有其他类别都设置为组件,而不是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类。数据库中的一个字段。在这两种情况下。
我创建的搜索封闭看起来像:
bool {
must {
nested {
path = "b"
query {
nested {
path = "b.c"
query {
path = "b.c.d"
query {
bool {
must {
match("b.c.d.id": "142342342342")
}
}
}
}
}
}
}
}
}
我真的必须更改域类的关系才能使搜索工作或我只是做错了吗?
什么可能导致问题?
编辑
eLasticsearch映射的字段" D"内部" C",在两种情况下都是完全相同的:
"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无法正确找到使用Word 匹配的匹配项。我可以使用 match_phrase 。
如此重要的课程,当搜索例如ID时,您的查询具有多级嵌套,并且您需要确切的匹配,则应使用Match_phrase!
我建议在 match 上使用 match_phrase 。Match_phrase查询将分析输入,如果为查询字段定义了分析仪,并找到匹配以下标准的文档:
- 所有术语必须出现在字段中
- 他们必须具有与输入值相同的顺序