org.elasticsearch.common.text.Text Java Examples

The following examples show how to use org.elasticsearch.common.text.Text. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: EsQuery.java    From AsuraFramework with Apache License 2.0 6 votes vote down vote up
/**
 * 对搜索命中结果做高亮
 *
 * @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);
    }
}
 
Example #2
Source File: HashJoinElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void createKeyToResultsAndFillOptimizationStructure(Map<String,Map<String, List<Object>>> optimizationTermsFilterStructure, TableInJoinRequestBuilder firstTableRequest) {
    List<SearchHit> firstTableHits = fetchAllHits(firstTableRequest);

    int resultIds = 1;
    for (SearchHit hit : firstTableHits) {
        HashMap<String, List<Map.Entry<Field, Field>>> comparisons = this.hashJoinComparisonStructure.getComparisons();
        for (Map.Entry<String, List<Map.Entry<Field, Field>>> comparison : comparisons.entrySet()) {
            String comparisonID = comparison.getKey();
            List<Map.Entry<Field, Field>> t1ToT2FieldsComparison = comparison.getValue();

            String key = getComparisonKey(t1ToT2FieldsComparison, hit, true, optimizationTermsFilterStructure.get(comparisonID));

            //int docid , id
            SearchHit searchHit = new SearchHit(resultIds, hit.getId(), new Text(hit.getType()), hit.getFields(), null);
            searchHit.sourceRef(hit.getSourceRef());

            onlyReturnedFields(searchHit.getSourceAsMap(), firstTableRequest.getReturnedFields(),firstTableRequest.getOriginalSelect().isSelectAll());
            resultIds++;
            this.hashJoinComparisonStructure.insertIntoComparisonHash(comparisonID, key, searchHit);
        }
    }
}
 
Example #3
Source File: IntersectExecutor.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void fillIntersectHitsFromResults(Set<ComperableHitResult> comparableHitResults) {
    int currentId = 1;
    List<SearchHit> intersectHitsList = new ArrayList<>(comparableHitResults.size());
    Set<Map.Entry<String, String>> firstTableFieldToAlias = this.builder.getFirstTableFieldToAlias().entrySet();
    for (ComperableHitResult result : comparableHitResults) {
        SearchHit originalHit = result.getOriginalHit();
        SearchHit searchHit = new SearchHit(currentId, originalHit.getId(), new Text(originalHit.getType()), originalHit.getFields(), null);
        searchHit.sourceRef(originalHit.getSourceRef());
        searchHit.getSourceAsMap().clear();
        Map<String, Object> sourceAsMap = result.getFlattenMap();
        for (Map.Entry<String, String> entry : firstTableFieldToAlias) {
            if (sourceAsMap.containsKey(entry.getKey())) {
                Object value = sourceAsMap.get(entry.getKey());
                sourceAsMap.remove(entry.getKey());
                sourceAsMap.put(entry.getValue(), value);
            }
        }

        searchHit.getSourceAsMap().putAll(sourceAsMap);
        currentId++;
        intersectHitsList.add(searchHit);
    }
    int totalSize = currentId - 1;
    SearchHit[] unionHitsArr = intersectHitsList.toArray(new SearchHit[totalSize]);
    this.intersectHits = new SearchHits(unionHitsArr, new TotalHits(totalSize, TotalHits.Relation.EQUAL_TO), 1.0f);
}
 
Example #4
Source File: EsHighLight.java    From es-service-parent with Apache License 2.0 6 votes vote down vote up
/**
 * 
 * @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;

}
 
Example #5
Source File: InternalHistogram.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    if (formatter != ValueFormatter.RAW) {
        Text keyTxt = new Text(formatter.format(key));
        if (keyed) {
            builder.startObject(keyTxt.string());
        } else {
            builder.startObject();
        }
        builder.field(CommonFields.KEY_AS_STRING, keyTxt);
    } else {
        if (keyed) {
            builder.startObject(String.valueOf(getKey()));
        } else {
            builder.startObject();
        }
    }
    builder.field(CommonFields.KEY, key);
    builder.field(CommonFields.DOC_COUNT, docCount);
    aggregations.toXContentInternal(builder, params);
    builder.endObject();
    return builder;
}
 
Example #6
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void initialize(Engine.Searcher docSearcher, ParsedDocument parsedDocument) {
    this.docSearcher = docSearcher;

    IndexReader indexReader = docSearcher.reader();
    LeafReaderContext atomicReaderContext = indexReader.leaves().get(0);
    LeafSearchLookup leafLookup = lookup().getLeafSearchLookup(atomicReaderContext);
    leafLookup.setDocument(0);
    leafLookup.source().setSource(parsedDocument.source());

    Map<String, SearchHitField> fields = new HashMap<>();
    for (IndexableField field : parsedDocument.rootDoc().getFields()) {
        fields.put(field.name(), new InternalSearchHitField(field.name(), Collections.emptyList()));
    }
    hitContext().reset(
            new InternalSearchHit(0, "unknown", new Text(parsedDocument.type()), fields),
            atomicReaderContext, 0, docSearcher.searcher()
    );
}
 
Example #7
Source File: TermSuggester.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
    DirectSpellChecker directSpellChecker = SuggestUtils.getDirectSpellChecker(suggestion.getDirectSpellCheckerSettings());
    final IndexReader indexReader = searcher.getIndexReader();
    TermSuggestion response = new TermSuggestion(
            name, suggestion.getSize(), suggestion.getDirectSpellCheckerSettings().sort()
    );
    List<Token> tokens = queryTerms(suggestion, spare);
    for (Token token : tokens) {
        // TODO: Extend DirectSpellChecker in 4.1, to get the raw suggested words as BytesRef
        SuggestWord[] suggestedWords = directSpellChecker.suggestSimilar(
                token.term, suggestion.getShardSize(), indexReader, suggestion.getDirectSpellCheckerSettings().suggestMode()
        );
        Text key = new Text(new BytesArray(token.term.bytes()));
        TermSuggestion.Entry resultEntry = new TermSuggestion.Entry(key, token.startOffset, token.endOffset - token.startOffset);
        for (SuggestWord suggestWord : suggestedWords) {
            Text word = new Text(suggestWord.string);
            resultEntry.addOption(new TermSuggestion.Entry.Option(word, suggestWord.freq, suggestWord.score));
        }
        response.addTerm(resultEntry);
    }
    return response;
}
 
Example #8
Source File: MinusExecutor.java    From elasticsearch-sql with Apache License 2.0 6 votes vote down vote up
private void fillMinusHitsFromOneField(String fieldName, Set<Object> fieldValues, SearchHit someHit) {
    List<SearchHit> minusHitsList = new ArrayList<>();
    int currentId = 1;
    for(Object result : fieldValues){
        Map<String,DocumentField> fields = new HashMap<>();
        ArrayList<Object> values = new ArrayList<Object>();
        values.add(result);
        fields.put(fieldName,new DocumentField(fieldName, values));
        SearchHit searchHit = new SearchHit(currentId,currentId+"", new Text(someHit.getType()), fields, null);
        searchHit.sourceRef(someHit.getSourceRef());
        searchHit.getSourceAsMap().clear();
        Map<String, Object> sourceAsMap = new HashMap<>();
        sourceAsMap.put(fieldName,result);
        searchHit.getSourceAsMap().putAll(sourceAsMap);
        currentId++;
        minusHitsList.add(searchHit);
    }
    int totalSize = currentId - 1;
    SearchHit[] unionHitsArr = minusHitsList.toArray(new SearchHit[totalSize]);
    this.minusHits = new SearchHits(unionHitsArr, new TotalHits(totalSize, TotalHits.Relation.EQUAL_TO), 1.0f);
}
 
Example #9
Source File: ElasticsearchUtil.java    From SpringBootLearn with Apache License 2.0 6 votes vote down vote up
/**
 * 获取高亮结果集
 * @Author lihaodong
 * @Description
 * @Date 20:09 2018/12/21
 * @Param [hit, highlightField]
 * @return java.util.Map<java.lang.String,java.lang.Object>
 **/
private  static   Map<String, Object>  getResultMap(SearchHit hit,String highlightField){
    hit.getSourceAsMap().put("id", hit.getId());
    if (StringUtils.isNotEmpty(highlightField)) {
        Text[] text = hit.getHighlightFields().get(highlightField).getFragments();
        String hightStr = null;
        if (text != null) {
            for (Text str : text) {
                hightStr = str.string();
            }
            //遍历 高亮结果集,覆盖 正常结果集
            hit.getSourceAsMap().put(highlightField, hightStr);
        }
    }
    return  hit.getSourceAsMap();
}
 
Example #10
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> highLightResultSet(ESBasicInfo esBasicInfo, QueryCondition queryCondition,
    HighLight highLight) {
    SearchResponse response = esClient.prepareSearch(esBasicInfo.getIndex()).setTypes(esBasicInfo.getType())
        .setSearchType(queryCondition.getSearchType()).setScroll(new TimeValue(queryCondition.getMillis()))
        .setQuery(queryCondition.getQueryBuilder()).setSize(queryCondition.getSize())
        .highlighter(highLight.getBuilder()).execute().actionGet();

    String highlightField = highLight.getField();
    List<Map<String, Object>> sourceList = new ArrayList<>();

    for (SearchHit searchHit : response.getHits()) {
        Map<String, Object> element = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isNotEmpty(highlightField)) {
            Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();

            if (text != null) {
                for (Text str : text) {
                    stringBuilder.append(str.string());
                }

                log.info("遍历 高亮结果集{}", stringBuilder.toString());
                element.put(highlightField, stringBuilder.toString());
            }
        }
        sourceList.add(element);
    }

    return sourceList;
}
 
Example #11
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public InternalSearchHit(int nestedTopDocId, String id, Text type, InternalNestedIdentity nestedIdentity, Map<String, SearchHitField> fields) {
    this.docId = nestedTopDocId;
    this.id = new Text(id);
    this.type = type;
    this.nestedIdentity = nestedIdentity;
    this.fields = fields;
}
 
Example #12
Source File: HighlightResultHelper.java    From mogu_blog_v2 with Apache License 2.0 5 votes vote down vote up
private String concat(Text[] texts) {
    StringBuffer sb = new StringBuffer();
    for (Text text : texts) {
        sb.append(text.toString());
    }
    return sb.toString();
}
 
Example #13
Source File: PendingClusterTask.java    From crate with Apache License 2.0 5 votes vote down vote up
public PendingClusterTask(long insertOrder, Priority priority, Text source, long timeInQueue, boolean executing) {
    assert timeInQueue >= 0 : "got a negative timeInQueue [" + timeInQueue + "]";
    assert insertOrder >= 0 : "got a negative insertOrder [" + insertOrder + "]";
    this.insertOrder = insertOrder;
    this.priority = priority;
    this.source = source;
    this.timeInQueue = timeInQueue;
    this.executing = executing;
}
 
Example #14
Source File: NestedLoopsElasticExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private SearchHit getMergedHit(int currentCombinedResults, String t1Alias, String t2Alias, SearchHit hitFromFirstTable, SearchHit matchedHit) {
    onlyReturnedFields(matchedHit.getSourceAsMap(), nestedLoopsRequest.getSecondTable().getReturnedFields(),nestedLoopsRequest.getSecondTable().getOriginalSelect().isSelectAll());
    SearchHit searchHit = new SearchHit(currentCombinedResults, hitFromFirstTable.getId() + "|" + matchedHit.getId(), new Text(hitFromFirstTable.getType() + "|" + matchedHit.getType()), hitFromFirstTable.getFields(), null);
    searchHit.sourceRef(hitFromFirstTable.getSourceRef());
    searchHit.getSourceAsMap().clear();
    searchHit.getSourceAsMap().putAll(hitFromFirstTable.getSourceAsMap());

    mergeSourceAndAddAliases(matchedHit.getSourceAsMap(), searchHit, t1Alias, t2Alias);
    return searchHit;
}
 
Example #15
Source File: ElasticJoinExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
protected SearchHit createUnmachedResult( List<Field> secondTableReturnedFields, int docId, String t1Alias, String t2Alias, SearchHit hit) {
    String unmatchedId = hit.getId() + "|0";
    Text unamatchedType = new Text(hit.getType() + "|null");

    SearchHit searchHit = new SearchHit(docId, unmatchedId, unamatchedType, hit.getFields(), null);

    searchHit.sourceRef(hit.getSourceRef());
    searchHit.getSourceAsMap().clear();
    searchHit.getSourceAsMap().putAll(hit.getSourceAsMap());
    Map<String,Object> emptySecondTableHitSource = createNullsSource(secondTableReturnedFields);

    mergeSourceAndAddAliases(emptySecondTableHitSource, searchHit,t1Alias,t2Alias);

    return searchHit;
}
 
Example #16
Source File: ElasticsearchTemplate.java    From summerframework with Apache License 2.0 5 votes vote down vote up
public List<Map<String, Object>> highLightResultSet(ESBasicInfo esBasicInfo, QueryCondition queryCondition,
    HighLight highLight) {
    SearchResponse response = esClient.prepareSearch(esBasicInfo.getIndex()).setTypes(esBasicInfo.getType())
        .setSearchType(queryCondition.getSearchType()).setScroll(new TimeValue(queryCondition.getMillis()))
        .setQuery(queryCondition.getQueryBuilder()).setSize(queryCondition.getSize())
        .highlighter(highLight.getBuilder()).execute().actionGet();

    String highlightField = highLight.getField();
    List<Map<String, Object>> sourceList = new ArrayList<>();

    for (SearchHit searchHit : response.getHits()) {
        Map<String, Object> element = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        if (StringUtils.isNotEmpty(highlightField)) {
            Text[] text = searchHit.getHighlightFields().get(highlightField).getFragments();

            if (text != null) {
                for (Text str : text) {
                    stringBuilder.append(str.string());
                }

                log.info("遍历 高亮结果集{}", stringBuilder.toString());
                element.put(highlightField, stringBuilder.toString());
            }
        }
        sourceList.add(element);
    }

    return sourceList;
}
 
Example #17
Source File: UnionExecutor.java    From elasticsearch-sql with Apache License 2.0 5 votes vote down vote up
private void fillInternalSearchHits(List<SearchHit> unionHits, SearchHit[] hits, Map<String, String> fieldNameToAlias) {
    for(SearchHit hit : hits){
        SearchHit searchHit = new SearchHit(currentId, hit.getId(), new Text(hit.getType()), hit.getFields(), null);
        searchHit.sourceRef(hit.getSourceRef());
        searchHit.getSourceAsMap().clear();
        Map<String, Object> sourceAsMap = hit.getSourceAsMap();
        if(!fieldNameToAlias.isEmpty()){
            updateFieldNamesToAlias(sourceAsMap, fieldNameToAlias);
        }
        searchHit.getSourceAsMap().putAll(sourceAsMap);
        currentId++;
        unionHits.add(searchHit);
    }
}
 
Example #18
Source File: ResultMapperExt.java    From roncoo-education with MIT License 5 votes vote down vote up
private String concat(Text[] texts) {
	StringBuffer sb = new StringBuffer();
	for (Text text : texts) {
		sb.append(text.toString());
	}
	return sb.toString();
}
 
Example #19
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
public XmlXContentBuilder field(XContentBuilderString name, Text value) throws IOException {
    field(name);
    if (value.hasBytes() && value.bytes().hasArray()) {
        generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
        return this;
    }
    if (value.hasString()) {
        generator.writeString(value.string());
        return this;
    }
    // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
    BytesArray bytesArray = value.bytes().toBytesArray();
    generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
    return this;
}
 
Example #20
Source File: XmlXContentBuilder.java    From elasticsearch-xml with Apache License 2.0 5 votes vote down vote up
public XmlXContentBuilder field(String name, Text value) throws IOException {
    field(name);
    if (value.hasBytes() && value.bytes().hasArray()) {
        generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
        return this;
    }
    if (value.hasString()) {
        generator.writeString(value.string());
        return this;
    }
    // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
    BytesArray bytesArray = value.bytes().toBytesArray();
    generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
    return this;
}
 
Example #21
Source File: PercolateResponse.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
}
 
Example #22
Source File: StreamInput.java    From crate with Apache License 2.0 5 votes vote down vote up
@Nullable
public Text readOptionalText() throws IOException {
    int length = readInt();
    if (length == -1) {
        return null;
    }
    return new Text(readBytesReference(length));
}
 
Example #23
Source File: ElasticsearchDocumentScore.java    From rdf4j with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public Iterable<String> getSnippets(String property) {
	HighlightField highlightField = hit.getHighlightFields().get(ElasticsearchIndex.toPropertyFieldName(property));
	if (highlightField == null) {
		return null;
	}
	return Iterables.transform(Arrays.asList(highlightField.getFragments()),
			(Text fragment) -> SearchFields.getSnippet(fragment.string()));
}
 
Example #24
Source File: StreamOutput.java    From crate with Apache License 2.0 5 votes vote down vote up
public void writeOptionalText(@Nullable Text text) throws IOException {
    if (text == null) {
        writeInt(-1);
    } else {
        writeText(text);
    }
}
 
Example #25
Source File: HighlightField.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void writeTo(StreamOutput out) throws IOException {
    out.writeString(name);
    if (fragments == null) {
        out.writeBoolean(false);
    } else {
        out.writeBoolean(true);
        out.writeVInt(fragments.length);
        for (Text fragment : fragments) {
            out.writeText(fragment);
        }
    }
}
 
Example #26
Source File: PendingClusterTask.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public PendingClusterTask(long insertOrder, Priority priority, Text source, long timeInQueue, boolean executing) {
    assert timeInQueue >= 0 : "got a negative timeInQueue [" + timeInQueue + "]";
    assert insertOrder >= 0 : "got a negative insertOrder [" + insertOrder + "]";
    this.insertOrder = insertOrder;
    this.priority = priority;
    this.source = source;
    this.timeInQueue = timeInQueue;
    this.executing = executing;
}
 
Example #27
Source File: XContentBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public XContentBuilder field(String name, Text value) throws IOException {
    field(name);
    if (value.hasBytes() && value.bytes().hasArray()) {
        generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
        return this;
    }
    if (value.hasString()) {
        generator.writeString(value.string());
        return this;
    }
    // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
    BytesArray bytesArray = value.bytes().toBytesArray();
    generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
    return this;
}
 
Example #28
Source File: XContentBuilder.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public XContentBuilder field(XContentBuilderString name, Text value) throws IOException {
    field(name);
    if (value.hasBytes() && value.bytes().hasArray()) {
        generator.writeUTF8String(value.bytes().array(), value.bytes().arrayOffset(), value.bytes().length());
        return this;
    }
    if (value.hasString()) {
        generator.writeString(value.string());
        return this;
    }
    // TODO: TextBytesOptimization we can use a buffer here to convert it? maybe add a request to jackson to support InputStream as well?
    BytesArray bytesArray = value.bytes().toBytesArray();
    generator.writeUTF8String(bytesArray.array(), bytesArray.arrayOffset(), bytesArray.length());
    return this;
}
 
Example #29
Source File: StreamOutput.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
public void writeOptionalText(@Nullable Text text) throws IOException {
    if (text == null) {
        writeInt(-1);
    } else {
        writeText(text);
    }
}
 
Example #30
Source File: StreamInput.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Nullable
public Text readOptionalText() throws IOException {
    int length = readInt();
    if (length == -1) {
        return null;
    }
    return new Text(readBytesReference(length));
}