首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在弹性搜索中通过应用必须查询从嵌套字段中获取数据

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

Stack Overflow用户
提问于 2017-05-16 19:22:24
回答 1查看 314关注 0票数 1

我有三个tables.Their结构,比如-

代码语言:javascript
复制
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;
}

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

代码语言:javascript
复制
"rcItemRegulations": [
                  {
                     "id": 1,
                     "rcItemRgltnType": "primary",
                     "regulation": {

                        "rgltnFullName": "17 ABC § 1.12(f)(5)(i)(B)"

                     }
                  }]

为此,我尝试使用弹性搜索查询-

代码语言:javascript
复制
{"query":{
  "bool" : {
    "must" : {
      "bool" : {
        "must" : [ {
          "term" : {
            "rcItemRegulations.rcItemRgltnType" : "primary"
          }
        }, {
          "term" : {
           "rcItemRegulations.regulation.rgltnFullName" : "17 ABC § 1.12(f)(5)(i)(B)"
          }
        } ]
      }
    }
  }
}
}

这给了我空白结果数组,即使这是exist.Please,也可以帮助我从elastic search中获取数据。

EN

回答 1

Stack Overflow用户

发布于 2017-05-16 19:59:24

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

First

因为rcItemRegulations是一个对象数组,而您试图根据rcItemRegulations中的值进行搜索。因此,我建议您将它们映射为嵌套数据类型。您可以使用以下映射。另外,由于您正在进行精确值匹配,我添加了一个关键字分析器,它将在倒排索引中保留相同的精确值。

映射

代码语言:javascript
复制
{
    "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

代码语言:javascript
复制
{
    "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)"
                                }
                            }]
                        }
                    }
                }
            }]
        }
    }
}

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44000119

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档