Java Code Examples for org.elasticsearch.common.text.Text

The following examples show how to use org.elasticsearch.common.text.Text. These examples are extracted from open source projects. 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 Project: SpringBootLearn   Source File: ElasticsearchUtil.java    License: 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 2
Source Project: Elasticsearch   Source File: PercolateContext.java    License: 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 3
Source Project: Elasticsearch   Source File: InternalHistogram.java    License: 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 4
Source Project: Elasticsearch   Source File: TermSuggester.java    License: 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 5
Source Project: AsuraFramework   Source File: EsQuery.java    License: 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 6
Source Project: es-service-parent   Source File: EsHighLight.java    License: 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 7
Source Project: elasticsearch-sql   Source File: HashJoinElasticExecutor.java    License: 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 8
Source Project: elasticsearch-sql   Source File: MinusExecutor.java    License: 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 Project: elasticsearch-sql   Source File: IntersectExecutor.java    License: 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 10
Source Project: microservices-platform   Source File: SearchBuilder.java    License: 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 11
Source Project: mogu_blog_v2   Source File: HighlightResultHelper.java    License: 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 12
Source Project: summerframework   Source File: ElasticsearchTemplate.java    License: 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 13
Source Project: summerframework   Source File: ElasticsearchTemplate.java    License: 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 14
Source Project: roncoo-education   Source File: ResultMapperExt.java    License: 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 15
Source Project: Elasticsearch   Source File: InternalSearchHit.java    License: 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 16
Source Project: crate   Source File: StreamOutput.java    License: 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 17
Source Project: Elasticsearch   Source File: HighlightField.java    License: 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 18
Source Project: Elasticsearch   Source File: PendingClusterTask.java    License: 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 19
Source Project: Elasticsearch   Source File: XContentBuilder.java    License: 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 20
Source Project: Elasticsearch   Source File: XContentBuilder.java    License: 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 21
Source Project: Elasticsearch   Source File: StreamOutput.java    License: 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 22
Source Project: Elasticsearch   Source File: StreamInput.java    License: 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 Project: Elasticsearch   Source File: PercolateResponse.java    License: 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 24
@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 25
Source Project: crate   Source File: StreamInput.java    License: 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 26
Source Project: elasticsearch-xml   Source File: XmlXContentBuilder.java    License: 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 27
Source Project: elasticsearch-xml   Source File: XmlXContentBuilder.java    License: 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 28
Source Project: elasticsearch-sql   Source File: UnionExecutor.java    License: 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 29
Source Project: elasticsearch-sql   Source File: ElasticJoinExecutor.java    License: 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 30
Source Project: elasticsearch-sql   Source File: MinusExecutor.java    License: Apache License 2.0 5 votes vote down vote up
private void fillMinusHitsFromResults(Set<ComperableHitResult> comperableHitResults) {
    int currentId = 1;
    List<SearchHit> minusHitsList = new ArrayList<>();
    for(ComperableHitResult result : comperableHitResults){
        ArrayList<Object> values = new ArrayList<Object>();
        values.add(result);
        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 : this.builder.getFirstTableFieldToAlias().entrySet()){
            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++;
        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);
}