elasticsearch,Java,elasticsearch" /> elasticsearch,Java,elasticsearch" />

Java elasticsearch:嵌套文档映射不起作用

Java elasticsearch:嵌套文档映射不起作用,java,elasticsearch,Java,elasticsearch,我在弹性搜索中有以下json结构,它存储@http://localhost:9200/mongoindex/documents/: { "text" : "OTesting1" "otag" : "otag1" "pages" : [{ "text" : "1" "name" : "itag1" }, { "text" : "2" "name" : "itag2" } ] } 我创建了嵌套映射,如下所示,以在其上启用嵌套搜索和嵌套过滤器:

我在弹性搜索中有以下json结构,它存储@
http://localhost:9200/mongoindex/documents/

{
"text" : "OTesting1"
"otag" : "otag1"
"pages" : [{
      "text" : "1"
  "name" : "itag1"

    }, {
     "text" : "2"
     "name" : "itag2"
    }
]
}
我创建了嵌套映射,如下所示,以在其上启用嵌套搜索和嵌套过滤器:

http://localhost:9200/mongoindex/documents/_mapping [PUT]

{
  "documents": {
    "properties": {
      "pages": {
        "type": "nested"
      }
    }
  }
}
现在执行以下java代码:

Settings settings = ImmutableSettings.settingsBuilder()
                .put("cluster.name", "xyz").build();
        Client client = new TransportClient(settings).addTransportAddress(new InetSocketTransportAddress("localhost", 9300));
        SearchResponse response = client.prepareSearch("mongoindex")
                .setTypes("documents")
                .setQuery(QueryBuilders.nestedQuery("documents", QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("pages.text", "1"))).scoreMode("avg"))
                .execute()
                .actionGet();
但这给了我以下例外:

> Exception in thread "main"
> org.elasticsearch.action.search.SearchPhaseExecutionException: Failed
> to execute phase [query], total failure; shardFailures
> {[kSKaBxjGTMSS352kukrYVw][mongoindex][0]:
> SearchParseException[[mongoindex][0]: from[-1],size[-1]: Parse Failure
> [Failed to parse source
> [{"query":{"nested":{"query":{"bool":{"must":{"match":{"pages.text":{"query":"1","type":"boolean"}}}}},"path":"documents","score_mode":"avg"}}}]]];
> nested: QueryParsingException[[mongoindex] [nested] nested object
> under path [documents] is not of nested type];
> }{[kSKaBxjGTMSS352kukrYVw][mongoindex][4]:
> SearchParseException[[mongoindex][4]: from[-1],size[-1]: Parse Failure
> [Failed to parse source
> [{"query":{"nested":{"query":{"bool":{"must":{"match":{"pages.text":{"query":"1","type":"boolean"}}}}},"path":"documents","score_mode":"avg"}}}]]];
> nested: QueryParsingException[[mongoindex] [nested] nested object
> under path [documents] is not of nested type]; }

您必须在“路径”:“页面”中指定嵌套对象的路径。我不熟悉Java语法,但等效的REST请求如下所示:

{
   "nested" : {
       "path" : "pages",
       "score_mode" : "avg",
       "query" : {
           "bool" : {
               "must" : [
                   {
                       "match" : {"pages.text" : "1"}
                   }
               ]
           }
       }
   }
}

与此相关的是,ElasticSearch错误消息的最后一行通常包含您需要调试的信息(而其余的信息非常无用和/或重复)。所以这是关键部分:

QueryParsingException[[mongoindex][nested]路径下的嵌套对象 [文档]不是嵌套类型];}


嗨,扎克,谢谢你的回复。请验证我创建的嵌套映射是否正确?我也尝试过用pages path搜索,但它给了我同样的错误。@user1660340您是否将查询更改为:
QueryBuilders.nestedQuery(“pages”,yourQUery)?这似乎是正确的解决方案,映射乍一看很好。