通过apply必须在弹性搜索中查询来从嵌套字段中获取数据

时间:2017-05-16 11:22:24

标签: elasticsearch spring-boot sense

我有三张桌子。他们的结构如 -

class Foo[A](implicit ordering: Ordering[A]) {
  def create: Unit = ()
  case class Test(x: Unit = create)
}

我的数据结构中的上述数据索引如 -

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)"

                     }
                  }]

这给了我空白的结果数组,即使它存在。请帮我从弹性搜索中获取数据。

1 个答案:

答案 0 :(得分:0)

我低头看着你的问题。我有两个建议给你。

<强>第一

由于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)"
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}

注意:小写搜索查询中的所有文本。