使用java api获取父ID的子项

时间:2014-01-30 16:36:21

标签: elasticsearch

我有“组织”索引结构:

{
 "organization" : {
    "properties" : {
        "id" : { "type" : "long" },
        "name" : { "type" : "string" }
    }
 }
}

和另一个索引“广告系列”:

{
 "campaign" : {
    "properties" : {
        "id" : { "type" : "long" },
        "name" : { "type" : "string" }
    },  
    "_parent" : {
         "type" : "organization"
    }
 }
}

当我搜索具有特定组织ID的广告系列时:

QueryBuilder qb = QueryBuilders.boolQuery()
                .must(QueryBuilders.hasParentQuery(SearchEntityEnum.ORGANIZATION.getType(), QueryBuilders.queryString("*" + keyword.replaceAll(" ", "* *") + "*").field(SearchEntityFieldNames.CommonFieldNames.NAME)
                .analyzeWildcard(true)));
        SearchResponse response = client.prepareSearch(SearchEntityEnum.CAMPAIGN.getIndex())
                .setTypes(SearchEntityEnum.CAMPAIGN.getType())
                .setSearchType(SearchType.QUERY_AND_FETCH)
                .setQuery(qb)
                .execute().actionGet();

发生此错误:

  

org.elasticsearch.action.search.SearchPhaseExecutionException: Failed to execute phase [query_fetch], all shards failed; shardFailures {[-RCGH_JbTmeTGDSvWp9_wA][campaigns][2]: SearchParseException[[campaigns][2]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][1]: SearchParseException[[campaigns][1]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][4]: SearchParseException[[campaigns][4]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][0]: SearchParseException[[campaigns][0]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }{[-RCGH_JbTmeTGDSvWp9_wA][campaigns][3]: SearchParseException[[campaigns][3]: from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"bool":{"must":{"has_parent":{"query":{"query_string":{"query":"**","fields":["name"],"analyze_wildcard":true}},"parent_type":"organization"}}}}}]]]; nested: QueryParsingException[[campaigns] [has_parent] query configured 'parent_type' [organization] is not a valid type]; }

2 个答案:

答案 0 :(得分:2)

昨天我遇到了同样的问题。我发现父类型和子类型都需要在同一个索引中,以便父级对孩子可见。

在此示例中,organizationcampaign是不同索引中的不同类型。您需要创建一个索引,并在其中定义这两种类型。我认为其他一切都很好,所以如果你有任何进一步的问题,请告诉我。

答案 1 :(得分:-1)

我认为(可能是错误的)您应该在campaigns索引中将type定义为organizations。另外,您应该将routing设置为true以使其有效。