我正在使用以下搜索查询:
SearchRequestBuilder searchRequestBuilder = client.getClient() .prepareSearch("index_5589b14f3004fb6be70e4724") .setSearchType(SearchType.SCAN) .setScroll(new TimeValue(60000)) .setQuery(QueryStem(stem)) .addFields("field1", "field2") .setHighlighterPostTags("</em>") .setHighlighterPreTags("<em>") .addHighlightedField("field1", 20, 150) .addHighlightedField("field2", 20, 150) .setFrom(1) .setSize(1);
DSL的等效版本为:
GET index_5589b14f3004fb6be70e4724/document_set/_search { "query": { // some query }, "fields": ["field1", "field2"], "highlight": { "pre_tags" : ["<em>"], "post_tags" : ["</em>"], "fields": { "field1": { "number_of_fragments": 10, "fragment_size": 20 }, "field2": { "number_of_fragments": 10, "fragment_size": 20 } } } }
我.getSource()在“搜索”响应中进行了尝试,但它给了我NullPointerException。然后,我检查了Sense,发现它没有任何_source字段。但是,如果我将其添加"_source": [...],到查询DSL中,则会得到所需的所需数据。
.getSource()
NullPointerException
_source
"_source": [...],
我想添加"_source": ["docset_id", "doc_id"],到中SearchRequestBuilder。我该怎么办?我当时在考虑使用.setSource()它,但它还有其他作用。
"_source": ["docset_id", "doc_id"],
SearchRequestBuilder
.setSource()
您可以使用该.setFetchSource(@Nullable String[] include, @Nullable String[] exclude)方法来表示要返回的字段,因此在您的情况下:
.setFetchSource(@Nullable String[] include, @Nullable String[] exclude)
.setFetchSource(new String[]{"docset_id", "doc_id"}, null)