本文整理汇总了Java中org.elasticsearch.common.text.Text类的典型用法代码示例。如果您正苦于以下问题:Java Text类的具体用法?Java Text怎么用?Java Text使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
Text类属于org.elasticsearch.common.text包,在下文中一共展示了Text类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setSortValues
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public SearchAfterBuilder setSortValues(Object[] values) {
if (values == null) {
throw new NullPointerException("Values cannot be null.");
}
if (values.length == 0) {
throw new IllegalArgumentException("Values must contains at least one value.");
}
for (int i = 0; i < values.length; i++) {
if (values[i] == null) continue;
if (values[i] instanceof String) continue;
if (values[i] instanceof Text) continue;
if (values[i] instanceof Long) continue;
if (values[i] instanceof Integer) continue;
if (values[i] instanceof Short) continue;
if (values[i] instanceof Byte) continue;
if (values[i] instanceof Double) continue;
if (values[i] instanceof Float) continue;
if (values[i] instanceof Boolean) continue;
if (values[i] instanceof Boolean) continue;
throw new IllegalArgumentException("Can't handle " + SEARCH_AFTER + " field value of type [" + values[i].getClass() + "]");
}
sortValues = new Object[values.length];
System.arraycopy(values, 0, sortValues, 0, values.length);
return this;
}
示例2: fromXContent
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public static Option fromXContent(XContentParser parser) {
Map<String, Object> values = PARSER.apply(parser, null);
Text text = new Text((String) values.get(Suggestion.Entry.Option.TEXT.getPreferredName()));
Float score = (Float) values.get(Suggestion.Entry.Option.SCORE.getPreferredName());
@SuppressWarnings("unchecked")
Map<String, Set<CharSequence>> contexts = (Map<String, Set<CharSequence>>) values
.get(CompletionSuggestion.Entry.Option.CONTEXTS.getPreferredName());
if (contexts == null) {
contexts = Collections.emptyMap();
}
SearchHit hit = null;
// the option either prints SCORE or inlines the search hit
if (score == null) {
hit = SearchHit.createFromMap(values);
score = hit.getScore();
}
CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(-1, text, score, contexts);
option.setHit(hit);
return option;
}
示例3: innerExecute
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@Override
public TermSuggestion innerExecute(String name, TermSuggestionContext suggestion, IndexSearcher searcher, CharsRefBuilder spare)
throws IOException {
DirectSpellChecker directSpellChecker = suggestion.getDirectSpellCheckerSettings().createDirectSpellChecker();
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;
}
示例4: fromXContent
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public static HighlightField fromXContent(XContentParser parser) throws IOException {
ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.currentToken(), parser::getTokenLocation);
String fieldName = parser.currentName();
Text[] fragments = null;
XContentParser.Token token = parser.nextToken();
if (token == XContentParser.Token.START_ARRAY) {
List<Text> values = new ArrayList<>();
while (parser.nextToken() != XContentParser.Token.END_ARRAY) {
values.add(new Text(parser.text()));
}
fragments = values.toArray(new Text[values.size()]);
} else if (token == XContentParser.Token.VALUE_NULL) {
fragments = null;
} else {
throw new ParsingException(parser.getTokenLocation(),
"unexpected token type [" + token + "]");
}
return new HighlightField(fieldName, fragments);
}
示例5: createTestItem
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
/**
* Create a randomized Suggestion.Entry
*/
@SuppressWarnings("unchecked")
public static <O extends Option> Entry<O> createTestItem(Class<? extends Entry> entryType) {
Text entryText = new Text(randomAsciiOfLengthBetween(5, 15));
int offset = randomInt();
int length = randomInt();
Entry entry;
Supplier<Option> supplier;
if (entryType == TermSuggestion.Entry.class) {
entry = new TermSuggestion.Entry(entryText, offset, length);
supplier = TermSuggestionOptionTests::createTestItem;
} else if (entryType == PhraseSuggestion.Entry.class) {
entry = new PhraseSuggestion.Entry(entryText, offset, length, randomDouble());
supplier = SuggestionOptionTests::createTestItem;
} else if (entryType == CompletionSuggestion.Entry.class) {
entry = new CompletionSuggestion.Entry(entryText, offset, length);
supplier = CompletionSuggestionOptionTests::createTestItem;
} else {
throw new UnsupportedOperationException("entryType not supported [" + entryType + "]");
}
int numOptions = randomIntBetween(0, 5);
for (int i = 0; i < numOptions; i++) {
entry.addOption(supplier.get());
}
return entry;
}
示例6: innerExecute
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@Override
public Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> innerExecute(String name, CustomSuggestionsContext suggestion, IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
// Get the suggestion context
String text = suggestion.getText().utf8ToString();
// create two suggestions with 12 and 123 appended
Suggest.Suggestion<Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option>> response = new Suggest.Suggestion<>(name, suggestion.getSize());
String firstSuggestion = String.format(Locale.ROOT, "%s-%s-%s-%s", text, suggestion.getField(), suggestion.options.get("suffix"), "12");
Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option> resultEntry12 = new Suggest.Suggestion.Entry<>(new Text(firstSuggestion), 0, text.length() + 2);
response.addTerm(resultEntry12);
String secondSuggestion = String.format(Locale.ROOT, "%s-%s-%s-%s", text, suggestion.getField(), suggestion.options.get("suffix"), "123");
Suggest.Suggestion.Entry<Suggest.Suggestion.Entry.Option> resultEntry123 = new Suggest.Suggestion.Entry<>(new Text(secondSuggestion), 0, text.length() + 3);
response.addTerm(resultEntry123);
return response;
}
示例7: createTestItem
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public static Option createTestItem() {
Text text = new Text(randomAsciiOfLengthBetween(5, 15));
int docId = randomInt();
int numberOfContexts = randomIntBetween(0, 3);
Map<String, Set<CharSequence>> contexts = new HashMap<>();
for (int i = 0; i < numberOfContexts; i++) {
int numberOfValues = randomIntBetween(0, 3);
Set<CharSequence> values = new HashSet<>();
for (int v = 0; v < numberOfValues; v++) {
values.add(randomAsciiOfLengthBetween(5, 15));
}
contexts.put(randomAsciiOfLengthBetween(5, 15), values);
}
SearchHit hit = null;
float score = randomFloat();
if (randomBoolean()) {
hit = SearchHitTests.createTestItem(false);
score = hit.getScore();
}
Option option = new CompletionSuggestion.Entry.Option(docId, text, score, contexts);
option.setHit(hit);
return option;
}
示例8: testToXContent
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public void testToXContent() throws IOException {
SearchHit[] hits = new SearchHit[] {
new SearchHit(1, "id1", new Text("type"), Collections.emptyMap()),
new SearchHit(2, "id2", new Text("type"), Collections.emptyMap()) };
long totalHits = 1000;
float maxScore = 1.5f;
SearchHits searchHits = new SearchHits(hits, totalHits, maxScore);
XContentBuilder builder = JsonXContent.contentBuilder();
builder.startObject();
searchHits.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
assertEquals("{\"hits\":{\"total\":1000,\"max_score\":1.5," +
"\"hits\":[{\"_type\":\"type\",\"_id\":\"id1\",\"_score\":\"-Infinity\"},"+
"{\"_type\":\"type\",\"_id\":\"id2\",\"_score\":\"-Infinity\"}]}}", builder.string());
}
示例9: suggest
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
public List<String> suggest(String prefix) {
CompletionSuggestionBuilder suggestionBuilder = new CompletionSuggestionBuilder("name_suggest")
.prefix(prefix);
SearchResponse response = getConnection().getClient().prepareSearch(getIndex())
.setTypes(getType())
.suggest(new SuggestBuilder().addSuggestion("suggestion", suggestionBuilder))
.setSize(100)
.setFetchSource(true)
.setExplain(false)
.execute()
.actionGet();
return response.getSuggest().filter(CompletionSuggestion.class).stream()
.flatMap(s -> s.getOptions().stream())
.sorted(Comparator.comparingDouble(Suggest.Suggestion.Entry.Option::getScore))
.map(Suggest.Suggestion.Entry.Option::getText)
.map(Text::toString)
.collect(Collectors.toList());
}
示例10: initialize
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
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()
);
}
示例11: toXContent
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@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;
}
示例12: innerExecute
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@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;
}
示例13: highLightResult
import org.elasticsearch.common.text.Text; //导入依赖的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);
}
}
示例14: innerExecute
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@Override
protected Suggest.Suggestion<? extends Suggest.Suggestion.Entry<? extends Suggest.Suggestion.Entry.Option>> innerExecute(String name,
final CompletionSuggestionContext suggestionContext, final IndexSearcher searcher, CharsRefBuilder spare) throws IOException {
if (suggestionContext.getFieldType() != null) {
final CompletionFieldMapper.CompletionFieldType fieldType = suggestionContext.getFieldType();
CompletionSuggestion completionSuggestion = new CompletionSuggestion(name, suggestionContext.getSize());
spare.copyUTF8Bytes(suggestionContext.getText());
CompletionSuggestion.Entry completionSuggestEntry = new CompletionSuggestion.Entry(
new Text(spare.toString()), 0, spare.length());
completionSuggestion.addTerm(completionSuggestEntry);
TopSuggestDocsCollector collector = new TopDocumentsCollector(suggestionContext.getSize());
suggest(searcher, suggestionContext.toQuery(), collector);
int numResult = 0;
for (TopSuggestDocs.SuggestScoreDoc suggestScoreDoc : collector.get().scoreLookupDocs()) {
TopDocumentsCollector.SuggestDoc suggestDoc = (TopDocumentsCollector.SuggestDoc) suggestScoreDoc;
// collect contexts
Map<String, Set<CharSequence>> contexts = Collections.emptyMap();
if (fieldType.hasContextMappings() && suggestDoc.getContexts().isEmpty() == false) {
contexts = fieldType.getContextMappings().getNamedContexts(suggestDoc.getContexts());
}
if (numResult++ < suggestionContext.getSize()) {
CompletionSuggestion.Entry.Option option = new CompletionSuggestion.Entry.Option(suggestDoc.doc,
new Text(suggestDoc.key.toString()), suggestDoc.score, contexts);
completionSuggestEntry.addOption(option);
} else {
break;
}
}
return completionSuggestion;
}
return null;
}
示例15: readFrom
import org.elasticsearch.common.text.Text; //导入依赖的package包/类
@Override
public void readFrom(StreamInput in) throws IOException {
name = in.readString();
if (in.readBoolean()) {
int size = in.readVInt();
if (size == 0) {
fragments = Text.EMPTY_ARRAY;
} else {
fragments = new Text[size];
for (int i = 0; i < size; i++) {
fragments[i] = in.readText();
}
}
}
}