本文整理汇总了Java中org.elasticsearch.search.highlight.HighlightField类的典型用法代码示例。如果您正苦于以下问题:Java HighlightField类的具体用法?Java HighlightField怎么用?Java HighlightField使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
HighlightField类属于org.elasticsearch.search.highlight包,在下文中一共展示了HighlightField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: PercolateShardResponse
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
public PercolateShardResponse(BytesRef[] matches, List<Map<String, HighlightField>> hls, long count, float[] scores, PercolateContext context, ShardId shardId) {
super(shardId);
this.matches = matches;
this.hls = hls;
this.count = count;
this.scores = scores;
this.percolatorTypeId = context.percolatorTypeId;
this.requestedSize = context.size();
QuerySearchResult result = context.queryResult();
if (result != null) {
if (result.aggregations() != null) {
this.aggregations = (InternalAggregations) result.aggregations();
}
this.pipelineAggregators = result.pipelineAggregators();
}
}
示例2: 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;
}
示例3: 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);
}
}
示例4: addIdIndexAndType
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
/**
* Adds _id, _index and/or _type to the current row
* @param idIndexTypeScore = String[]{_id, _index, _type}
* @param head
* @param row
*/
private void addIdIndexAndType(String id, String index, String type, Float score, Map<String, HighlightField> highlights, Heading head, List<Object> row){
if(id != null && head.hasAllCols() || head.hasLabel(Heading.ID)){
row.set( head.getColumnByLabel(Heading.ID).getIndex(), id);
}
if(index != null && head.hasAllCols() || head.hasLabel(Heading.INDEX)){
row.set( head.getColumnByLabel(Heading.INDEX).getIndex(), index);
}
if(type != null && head.hasAllCols() || head.hasLabel(Heading.TYPE)){
row.set( head.getColumnByLabel(Heading.TYPE).getIndex(), type);
}
if(score != null && head.hasLabel(Heading.SCORE)){
row.set( head.getColumnByLabel(Heading.SCORE).getIndex(), score);
}
if(highlights != null){
for(String field : highlights.keySet()){
Column col = head.getColumnByNameAndOp(field, Operation.HIGHLIGHT);
if(col == null) continue;
List<Object> fragments = new ArrayList<Object>();
for(Text fragment : highlights.get(field).getFragments()) fragments.add(fragment.toString());
row.set(col.getIndex(), new ESArray(fragments));
}
}
}
示例5: 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;
}
示例6: 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;
}
示例7: highlightFields
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
@Override
public Map<String, HighlightField> highlightFields() {
if (highlightFields == null) {
return ImmutableMap.of();
}
return this.highlightFields;
}
示例8: Match
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
/**
* Constructor only for internal usage.
*/
public Match(Text index, Text id, float score, Map<String, HighlightField> hl) {
this.id = id;
this.score = score;
this.index = index;
this.hl = hl;
}
示例9: readFrom
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
@Override
public void readFrom(StreamInput in) throws IOException {
id = in.readText();
index = in.readText();
score = in.readFloat();
int size = in.readVInt();
if (size > 0) {
hl = new HashMap<>(size);
for (int j = 0; j < size; j++) {
hl.put(in.readString(), HighlightField.readHighlightField(in));
}
}
}
示例10: writeTo
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeText(id);
out.writeText(index);
out.writeFloat(score);
if (hl != null) {
out.writeVInt(hl.size());
for (Map.Entry<String, HighlightField> entry : hl.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
}
} else {
out.writeVInt(0);
}
}
示例11: readFrom
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
@Override
public void readFrom(StreamInput in) throws IOException {
super.readFrom(in);
percolatorTypeId = in.readByte();
requestedSize = in.readVInt();
count = in.readVLong();
matches = new BytesRef[in.readVInt()];
for (int i = 0; i < matches.length; i++) {
matches[i] = in.readBytesRef();
}
scores = new float[in.readVInt()];
for (int i = 0; i < scores.length; i++) {
scores[i] = in.readFloat();
}
int size = in.readVInt();
for (int i = 0; i < size; i++) {
int mSize = in.readVInt();
Map<String, HighlightField> fields = new HashMap<>();
for (int j = 0; j < mSize; j++) {
fields.put(in.readString(), HighlightField.readHighlightField(in));
}
hls.add(fields);
}
aggregations = InternalAggregations.readOptionalAggregations(in);
if (in.readBoolean()) {
int pipelineAggregatorsSize = in.readVInt();
List<SiblingPipelineAggregator> pipelineAggregators = new ArrayList<>(pipelineAggregatorsSize);
for (int i = 0; i < pipelineAggregatorsSize; i++) {
BytesReference type = in.readBytesReference();
PipelineAggregator pipelineAggregator = PipelineAggregatorStreams.stream(type).readResult(in);
pipelineAggregators.add((SiblingPipelineAggregator) pipelineAggregator);
}
this.pipelineAggregators = pipelineAggregators;
}
}
示例12: writeTo
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
@Override
public void writeTo(StreamOutput out) throws IOException {
super.writeTo(out);
out.writeByte(percolatorTypeId);
out.writeVLong(requestedSize);
out.writeVLong(count);
out.writeVInt(matches.length);
for (BytesRef match : matches) {
out.writeBytesRef(match);
}
out.writeVLong(scores.length);
for (float score : scores) {
out.writeFloat(score);
}
out.writeVInt(hls.size());
for (Map<String, HighlightField> hl : hls) {
out.writeVInt(hl.size());
for (Map.Entry<String, HighlightField> entry : hl.entrySet()) {
out.writeString(entry.getKey());
entry.getValue().writeTo(out);
}
}
out.writeOptionalStreamable(aggregations);
if (pipelineAggregators == null) {
out.writeBoolean(false);
} else {
out.writeBoolean(true);
out.writeVInt(pipelineAggregators.size());
for (PipelineAggregator pipelineAggregator : pipelineAggregators) {
out.writeBytesReference(pipelineAggregator.type().stream());
pipelineAggregator.writeTo(out);
}
}
}
示例13: termQuery
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
public static void termQuery(Client client ) {
SearchResponse res = null;
QueryBuilder qb = QueryBuilders
.termQuery("title","article");
// QueryBuilder qb = QueryBuilders
// .termsQuery("title","article","relevence");
res = client.prepareSearch("search_test")
.setTypes("article")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(qb)
.addHighlightedField("title")
.setHighlighterPreTags("<span class='searchKey'>")
.setHighlighterPostTags("</san>")
.setFrom(0)
.setSize(10)
.execute().actionGet();
for (SearchHit hit: res.getHits()) {
Map<String, HighlightField> result = hit.highlightFields();
HighlightField highlightedSummary = result.get("title");
Text[] titleTexts = highlightedSummary.getFragments();
String allFragments = "";
for (Text text: titleTexts){
allFragments += text;
}
System.out.println(result);
System.out.println(allFragments);
}
// on shutdown
client.close();
//common terms query
}
示例14: getHighlightedFragments
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
/**
* 検索キーワードにマッチした箇所をハイライトした値へのStreamを返す.
* @see Stream
* @param fieldName フィールド名
* @return {@link Stream}
*/
public Stream<String> getHighlightedFragments(String fieldName) {
HighlightField field = hit.getHighlightFields().get(fieldName);
if (field != null) {
Stream<Text> s = Arrays.stream(field.getFragments());
return s.map(t -> t.string());
}
return new ArrayList<String>().stream();
}
示例15: getHighlight
import org.elasticsearch.search.highlight.HighlightField; //导入依赖的package包/类
private Map<String, List<String>> getHighlight(Set<Map.Entry<String, HighlightField>> entrySet) {
Map<String, List<String>> highlight = new HashMap<>();
for (Map.Entry<String, HighlightField> highlightField : entrySet) {
List<String> fragments = new ArrayList<>();
for (Text text : highlightField.getValue().fragments()) {
fragments.add(text.string());
}
highlight.put(highlightField.getKey(), fragments);
}
return highlight;
}