我有三个表,它们的结构像-
public class RcItem{
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL, mappedBy = "rcItem")
@JsonManagedReference
private Set<RcItemRegulation> rcItemRegulations = new HashSet<>();
}
public class RcItemRegulation{
@ManyToOne
@JoinColumn(name = "rc_item_id")
@Field(type = FieldType.Nested, index = FieldIndex.analyzed, analyzer = "lowercase_keyword", store = true)
@JsonBackReference
private RcItem rcItem;
@ManyToOne
@JoinColumn(name = "rgltn_id")
@Field(type = FieldType.Nested, index = FieldIndex.analyzed, analyzer = "lowercase_keyword", store = true)
private Regulation regulation;
}
public class Regulation{
@OneToMany(cascade = CascadeType.ALL, mappedBy = "regulation")
@JsonManagedReference
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
private Set<RcItemRegulation> rcItemRegulations = new HashSet<>();
@Column(name = "rgltn_full_name")
@Field(type = FieldType.String, index = FieldIndex.analyzed, analyzer = "lowercase_keyword", store = true)
private String rgltnFullName;
}
我的数据结构中的上述数据索引如下-
"rcItemRegulations": [
{
"id": 1,
"rcItemRgltnType": "primary",
"regulation": {
"rgltnFullName": "17 ABC § 1.12(f)(5)(i)(B)"
}
}]
为此,我尝试了 flex 搜索查询-
{"query":{
"bool" : {
"must" : {
"bool" : {
"must" : [ {
"term" : {
"rcItemRegulations.rcItemRgltnType" : "primary"
}
}, {
"term" : {
"rcItemRegulations.regulation.rgltnFullName" : "17 ABC § 1.12(f)(5)(i)(B)"
}
} ]
}
}
}
}
}
即使存在,这也会给我空结果数组。请帮助我从 flex 搜索中获取数据。
最佳答案
我一直在看你的问题。我有两个建议给你。
第一
由于rcItemRegulations
是对象数组,因此您尝试根据rcItemRegulations
内部的值进行搜索。因此,我建议您将它们映射为嵌套数据类型。您可以使用以下映射。另外,由于您正在执行精确值匹配,因此我添加了关键字分析器,它将在反向索引中保留相同的精确值。
映射
{
"settings": {
"analysis": {
"analyzer": {
"index_analyzer_v1": {
"tokenizer": "keyword",
"filter": ["lowercase"]
}
}
}
},
"mappings": {
"type_1": {
"properties": {
"rcItemRegulations": {
"type": "nested",
"properties": {
"regulation": {
"type": "object",
"properties": {
"rgltnFullName": {
"type": "text",
"analyzer": "index_analyzer_v1"
}
}
}
}
}
}
}
}
}
其次,您现在还需要更改查询,因为这次您正在查询嵌套数据类型。
您必须使用nested_query
{
"query": {
"bool": {
"must": [{
"nested": {
"path": "rcItemRegulations",
"query": {
"bool": {
"must": [{
"term": {
"rcItemRegulations.rcItemRgltnType": "primary"
}
}, {
"term": {
"rcItemRegulations.regulation.rgltnFullName": "17 abc § 1.12(f)(5)(i)(b)"
}
}]
}
}
}
}]
}
}
}
注意:将搜索查询中的所有文本小写。
关于elasticsearch - 通过应用弹性查询从嵌套字段中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44000119/