Elasticsearch内部命中响应

huangapple go评论60阅读模式
英文:

Elasticsearch inner hits reponse

问题

这是我的查询函数:

public List<feed> search(String id) throws IOException {    
    Query nestedQuery = NestedQuery.of(nq -> nq.path("comment").innerHits(InnerHits.of(ih -> ih)).query(MatchQuery
                    .of(mq -> mq.field("comment.c_text").query(id))._toQuery()))._toQuery();
    Query termQueryTitle = TermQuery.of(tq -> tq.field("title").value(id))._toQuery();
    Query termQueryBody = TermQuery.of(tq -> tq.field("body").value(id))._toQuery();
    Query boolQuery = BoolQuery.of(bq -> bq.should(nestedQuery, termQueryBody, termQueryTitle))._toQuery();
    SearchRequest searchRequest = SearchRequest.of(s -> s.index(indexName).query(boolQuery));
    var response = elasticsearchClient.search(searchRequest, feed.class);
    for (var hit : response.hits().hits()){
        System.out.println("这是内部命中响应: " + (hit.innerHits().get("comment").hits().hits()));  }
    List<Hit<feed>> hits = response.hits().hits();
    List<feed> feeds = new ArrayList<>();
    feed f = null;
    for (Hit object : hits){
        f = (feed) object.source();
        feeds.add(f); }   
    return feeds;
}

我已经添加了这段代码:

for (var hit : response.hits().hits()){
    System.out.println("这是内部命中响应: " + (hit.innerHits().get("comment").hits().hits()));  }

如果它找到2条记录,它会给我返回2条记录的引用,但不会显示实际记录,就像输出如下,如果在内部命中找到2条记录:

这是内部命中响应 [co.elastic.clients.elasticsearch.core.search.Hit@75679b1a]
这是内部命中响应 [co.elastic.clients.elasticsearch.core.search.Hit@1916d9c6]

有人能帮我弹出实际记录吗?

英文:

This is my query function :

public  List&lt;feed&gt; search(String id) throws IOException {    
    Query nestedQuery = NestedQuery.of(nq -&gt;nq.path(&quot;comment&quot;).innerHits(InnerHits.of(ih -&gt; ih)).query(MatchQuery
    		            .of(mq -&gt; mq.field(&quot;comment.c_text&quot;).query(id))._toQuery()))._toQuery();
    Query termQueryTitle = TermQuery.of(tq -&gt; tq.field(&quot;title&quot;).value(id))._toQuery();
    Query termQueryBody = TermQuery.of(tq -&gt; tq.field(&quot;body&quot;).value(id))._toQuery();
    Query boolQuery = BoolQuery.of(bq -&gt; bq.should(nestedQuery, termQueryBody, termQueryTitle))._toQuery();
    SearchRequest searchRequest = SearchRequest.of(s -&gt; s.index(indexName).query(boolQuery));
    var response = elasticsearchClient.search(searchRequest, feed.class);
    for (var hit : response.hits().hits()){
    	System.out.println(&quot;this is inner hit response: &quot; + (hit.innerHits().get(&quot;comment&quot;).hits().hits()));  }
           List&lt;Hit&lt;feed&gt;&gt; hits = response.hits().hits();
           List&lt;feed&gt; feeds = new ArrayList&lt;&gt;();
           feed f=null;
           for(Hit object : hits){
        	   f = (feed) object.source();
        	 feeds.add(f); }   
           return feeds;
           }

i have add this code

 for (var hit : response.hits().hits()){
    	System.out.println(&quot;this is inner hit response: &quot; + (hit.innerHits().get(&quot;comment&quot;).hits().hits()));  }

if it founds 2 records it gives me the refrence of 2 records but dont show me the actual records like its outpout is as follow if it founds 2 records in inner hit :

this is inner hit response [co.elastic.clients.elasticsearch.core.search.Hit@75679b1a]
this is inner hit response [co.elastic.clients.elasticsearch.core.search.Hit@1916d9c6]

can anyone help me to poput the actual records

答案1

得分: 1

这在控制台中对我来说有效:

for (var hit : response.hits().hits()) {
    var innerHits = hit.innerHits().get("comment").hits().hits();
    for (var innerHit : innerHits) {
        JsonData source = innerHit.source();
        String jsonDataString = source.toString();
        System.out.println("匹配的评论" + jsonDataString);
    }
}
英文:

This properly works for me in console :

 for (var hit : response.hits().hits()) {
        var innerHits = hit.innerHits().get(&quot;comment&quot;).hits().hits();
        for (var innerHit : innerHits) {
            JsonData source = innerHit.source();
            String jsonDataString = source.toString();
            System.out.println(&quot;Matched comments&quot;+jsonDataString);
        }
    }

答案2

得分: 0

我创建了一个具有属性 "c_text" 的 Comment 类,并在将其添加到评论列表之前进行了转换。

var comments = new ArrayList<Comment>();
for (var hit : response.hits().hits()) {
  comments.addAll(hit.innerHits().get("comment").hits().hits().stream().map(
      h -> h.source().to(Comment.class)
  ).collect(Collectors.toList()));
}
System.out.println(comments);
英文:

I created a class Comment with property "c_text" and did a cast before adding inside a lists comments.

 var comments = new ArrayList&lt;Comment&gt;();
    for (var hit : response.hits().hits()) {
      comments.addAll(hit.innerHits().get(&quot;comment&quot;).hits().hits().stream().map(
          h -&gt; h.source().to(Comment.class)
      ).collect(Collectors.toList()));
    }
    System.out.println(comments);

huangapple
  • 本文由 发表于 2023年1月9日 15:22:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/75054183.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定