本文整理汇总了Java中org.elasticsearch.search.highlight.HighlightField.fragments方法的典型用法代码示例。如果您正苦于以下问题:Java HighlightField.fragments方法的具体用法?Java HighlightField.fragments怎么用?Java HighlightField.fragments使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.elasticsearch.search.highlight.HighlightField
的用法示例。
在下文中一共展示了HighlightField.fragments方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: mapEntity
import org.elasticsearch.search.highlight.HighlightField; //导入方法依赖的package包/类
public <T> T mapEntity(String source , SearchHit hit , Class<T> clazz) {
T t = mapEntity(source , clazz) ;
Map<String, HighlightField> highlightFields = hit.getHighlightFields();
HighlightField highlightNameField = highlightFields.get("title");
HighlightField contentHightlightField = highlightFields.get("content");
try {
if(highlightNameField!=null&&highlightNameField.fragments()!=null){
PropertyUtils.setProperty(t, "title" , highlightNameField.fragments()[0].string());
}
if(contentHightlightField!=null){
PropertyUtils.setProperty(t, "content" , contentHightlightField.fragments()[0].string());
}
PropertyUtils.setProperty(t, "id" , hit.getId());
} catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
e.printStackTrace();
}
return t;
}
示例2: highLightResult
import org.elasticsearch.search.highlight.HighlightField; //导入方法依赖的package包/类
/**
* 对搜索命中结果做高亮
*
* @param json
* @param hit
* @param highLigthFieldName
*/
private void highLightResult (JSONObject json, SearchHit hit, String highLigthFieldName) {
//获取对应的高亮域
Map<String, HighlightField> result = hit.highlightFields();
//从设定的高亮域中取得指定域
HighlightField hlField = result.get(highLigthFieldName);
if (Check.NuNObj(hlField)) {
return;
}
//取得定义的高亮标签
Text[] hlTexts = hlField.fragments();
if (Check.NuNObject(hlTexts)) {
return;
}
//为title串值增加自定义的高亮标签
StringBuffer hlTextsFiled = new StringBuffer();
for (Text text : hlTexts) {
hlTextsFiled.append(text);
}
//如果高亮域内有fragments 反回的数据不为空字符串
if (!Check.NuNStrStrict(hlTextsFiled.toString())) {
json.put(highLigthFieldName, hlTextsFiled);
}
}
示例3: getHighlight
import org.elasticsearch.search.highlight.HighlightField; //导入方法依赖的package包/类
/**
*
* @param hit
* @param seach_fileds
* @return
*/
public static Map<String, Object> getHighlight(SearchHit hit, List<String> seach_fileds) {
Map<String, Object> result = new HashMap<String, Object>();
Map<String, HighlightField> highlights = hit.highlightFields();
for (String filed : seach_fileds) {
HighlightField highlight = highlights.get(filed);
if (null == highlight) {
continue;
}
StringBuffer sb = new StringBuffer();
Text[] fragments = highlight.fragments();
for (Text fragment : fragments) {
sb.append(fragment);
}
result.put(filed + "_HIGH", sb.toString());
}
return result;
}
示例4: getHighlightsFromResult
import org.elasticsearch.search.highlight.HighlightField; //导入方法依赖的package包/类
private static Map<String, HighlightResult> getHighlightsFromResult(SearchHits hits) {
final HashMap<String, HighlightResult> highlights = new HashMap<>();
for (final SearchHit hit : hits) {
final HighlightResult result = new HighlightResult(new HashMap<String, List<String>>());
final Collection<HighlightField> highlightFields = hit.getHighlightFields().values();
for (final HighlightField entry : highlightFields) {
final List<String> fragments = new ArrayList<>();
final Text[] frags = entry.fragments();
for (final Text t : frags) {
fragments.add(t.toString());
}
result.putToResults(entry.getName(), fragments);
}
highlights.put(hit.getId(), result);
}
return highlights;
}
示例5: mapResults
import org.elasticsearch.search.highlight.HighlightField; //导入方法依赖的package包/类
@Override
public <T> AggregatedPage<T> mapResults(SearchResponse response, Class<T> clazz, Pageable pageable) {
List<File> result = new ArrayList<>();
long totalHits = response.getHits().getTotalHits();
for (SearchHit searchHit : response.getHits()) {
if (response.getHits().getHits().length <= 0) {
return null;
}
//System.out.println(response.toString());
String summaryWithHighlight = null;
String pathWithHighlight = null;
HighlightField highlightFieldContent = searchHit.getHighlightFields().get("content");
HighlightField highlightFieldPath = searchHit.getHighlightFields().get("path");
if (highlightFieldContent != null) {
summaryWithHighlight = Arrays.stream(highlightFieldContent.fragments())
.map(text -> EncodingUtil.convertToUTF8(text.toString()))
.collect(Collectors.joining("\n[...]\n"));
}
if (highlightFieldPath != null && highlightFieldPath.fragments() != null) {
pathWithHighlight = EncodingUtil.unEscapeString(highlightFieldPath.fragments()[0].toString());
}
File oneFile = new File(
(String) searchHit.getSource().get("id"),
(String) searchHit.getSource().get("name"),
(String) searchHit.getSource().get("extension"),
pathWithHighlight != null ? pathWithHighlight : (String) searchHit.getSource().get("path"),
(String) searchHit.getSource().get("project"),
summaryWithHighlight,
(String) searchHit.getSource().get("version"),
//conversion en string puis en long, très bizarre, à l'origine, il était préférable de réaliser :
//(Long) searchHit.getSource().get("size")
//mais cela jette un classCastException Integer to Long
Long.valueOf(searchHit.getSource().get("size").toString())
);
oneFile.setScore(searchHit.getScore());
result.add(oneFile);
}
return new AggregatedPageImpl<>((List<T>) result, pageable, totalHits, response.getAggregations());
}