本文整理汇总了Java中org.elasticsearch.search.SearchHitField类的典型用法代码示例。如果您正苦于以下问题:Java SearchHitField类的具体用法?Java SearchHitField怎么用?Java SearchHitField使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SearchHitField类属于org.elasticsearch.search包,在下文中一共展示了SearchHitField类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: assertStoredBinaryFields
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
/**
* Make sure we can load stored binary fields.
*/
void assertStoredBinaryFields(String indexName, Version version) throws Exception {
SearchRequestBuilder builder = client().prepareSearch(indexName);
builder.setQuery(QueryBuilders.matchAllQuery());
builder.setSize(100);
builder.addStoredField("binary");
SearchHits hits = builder.get().getHits();
assertEquals(100, hits.getHits().length);
for(SearchHit hit : hits) {
SearchHitField field = hit.field("binary");
assertNotNull(field);
Object value = field.getValue();
assertTrue(value instanceof BytesArray);
assertEquals(16, ((BytesArray) value).length());
}
}
示例2: getSearchFields
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
private Map<String, SearchHitField> getSearchFields(SearchContext context, int nestedSubDocId, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) {
Map<String, SearchHitField> searchFields = null;
if (context.hasStoredFields() && !context.storedFieldsContext().fieldNames().isEmpty()) {
FieldsVisitor nestedFieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.emptySet() : fieldNames,
fieldNamePatterns == null ? Collections.emptyList() : fieldNamePatterns, false);
if (nestedFieldsVisitor != null) {
loadStoredFields(context, subReaderContext, nestedFieldsVisitor, nestedSubDocId);
nestedFieldsVisitor.postProcess(context.mapperService());
if (!nestedFieldsVisitor.fields().isEmpty()) {
searchFields = new HashMap<>(nestedFieldsVisitor.fields().size());
for (Map.Entry<String, List<Object>> entry : nestedFieldsVisitor.fields().entrySet()) {
searchFields.put(entry.getKey(), new SearchHitField(entry.getKey(), entry.getValue()));
}
}
}
}
return searchFields;
}
示例3: hitExecute
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
if (context.storedFieldsContext() != null && context.storedFieldsContext().fetchFields() == false) {
return ;
}
ParentFieldMapper parentFieldMapper = context.mapperService().documentMapper(hitContext.hit().getType()).parentFieldMapper();
if (parentFieldMapper.active() == false) {
return;
}
String parentId = getParentId(parentFieldMapper, hitContext.reader(), hitContext.docId());
if (parentId == null) {
// hit has no _parent field. Can happen for nested inner hits if parent hit is a p/c document.
return;
}
Map<String, SearchHitField> fields = hitContext.hit().fieldsOrNull();
if (fields == null) {
fields = new HashMap<>();
hitContext.hit().fields(fields);
}
fields.put(ParentFieldMapper.NAME, new SearchHitField(ParentFieldMapper.NAME, Collections.singletonList(parentId)));
}
示例4: testScriptFieldsForNullReturn
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public void testScriptFieldsForNullReturn() throws Exception {
client().prepareIndex("test", "type1", "1")
.setSource("foo", "bar")
.setRefreshPolicy("true").get();
SearchResponse response = client().prepareSearch()
.setQuery(matchAllQuery())
.addScriptField("test_script_1",
new Script(ScriptType.INLINE, CustomScriptPlugin.NAME, "return null", Collections.emptyMap()))
.get();
assertNoFailures(response);
SearchHitField fieldObj = response.getHits().getAt(0).field("test_script_1");
assertThat(fieldObj, notNullValue());
List<?> fieldValues = fieldObj.getValues();
assertThat(fieldValues, hasSize(1));
assertThat(fieldValues.get(0), nullValue());
}
示例5: testLoadMetadata
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public void testLoadMetadata() throws Exception {
assertAcked(prepareCreate("test")
.addMapping("parent")
.addMapping("my-type1", "_parent", "type=parent"));
indexRandom(true,
client().prepareIndex("test", "my-type1", "1")
.setRouting("1")
.setParent("parent_1")
.setSource(jsonBuilder().startObject().field("field1", "value").endObject()));
SearchResponse response = client().prepareSearch("test").addStoredField("field1").get();
assertSearchResponse(response);
assertHitCount(response, 1);
Map<String, SearchHitField> fields = response.getHits().getAt(0).getFields();
assertThat(fields.get("field1"), nullValue());
assertThat(fields.get("_routing").isMetadataField(), equalTo(true));
assertThat(fields.get("_routing").getValue().toString(), equalTo("1"));
assertThat(fields.get("_parent").isMetadataField(), equalTo(true));
assertThat(fields.get("_parent").getValue().toString(), equalTo("parent_1"));
}
示例6: initialize
import org.elasticsearch.search.SearchHitField; //导入依赖的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()
);
}
示例7: buildJSONFromFields
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
private String buildJSONFromFields(Collection<SearchHitField> values) {
JsonFactory nodeFactory = new JsonFactory();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
generator.writeStartObject();
for (SearchHitField value : values) {
if (value.getValues().size() > 1) {
generator.writeArrayFieldStart(value.getName());
for (Object val : value.getValues()) {
generator.writeObject(val);
}
generator.writeEndArray();
} else {
generator.writeObjectField(value.getName(), value.getValue());
}
}
generator.writeEndObject();
generator.flush();
return new String(stream.toByteArray(), Charset.forName("UTF-8"));
} catch (IOException e) {
return null;
}
}
示例8: SearchResult
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public SearchResult(SearchResponse resp) {
SearchHits hits = resp.getHits();
this.total = hits.getTotalHits();
results = new ArrayList<>(hits.getHits().length);
for (SearchHit searchHit : hits.getHits()) {
if (searchHit.getSource() != null) {
results.add(searchHit.getSource());
} else if (searchHit.getFields() != null) {
Map<String, SearchHitField> fields = searchHit.getFields();
results.add(toFieldsMap(fields));
}
}
}
示例9: buildJSONFromFields
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
private String buildJSONFromFields(Collection<SearchHitField> values) {
JsonFactory nodeFactory = new JsonFactory();
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
JsonGenerator generator = nodeFactory.createGenerator(stream, JsonEncoding.UTF8);
generator.writeStartObject();
for (SearchHitField value : values) {
if (value.getValues().size() > 1) {
generator.writeArrayFieldStart(value.getName());
for (Object val : value.getValues()) {
generator.writeObject(val);
}
generator.writeEndArray();
} else {
generator.writeObjectField(value.getName(), value.getValue());
}
}
generator.writeEndObject();
generator.flush();
return new String(stream.toByteArray(), Charset.forName("UTF-8"));
} catch (IOException e) {
LOG.error("IOException in buildJSONFromFields ", e);
return null;
}
}
示例10: searchFieldsByKeys
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public Map<Map<String, String>, SearchHit> searchFieldsByKeys(String index, String type,
List<String> keys,
List<String> fields,
JsonNode data) {
Map<Map<String, String>, SearchHit> keyVals = new HashMap<>();
SearchHits hits = searchHitsByKeys(index, type, keys, fields, data);
for (SearchHit hit : hits) {
Map<String, SearchHitField> hitFields = hit.getFields();
Map<String, String> keyVal = new HashMap<>(keys.size());
for (String key : keys) {
if (hitFields.containsKey(key)) {
//for some reason, this (String) is necessary for some environments/compilers
keyVal.put(key, (String) hitFields.get(key).getValue());
}
}
keyVals.put(keyVal, hit);
}
return keyVals;
}
示例11: testSimpleFetchAnalyzedText
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public void testSimpleFetchAnalyzedText() throws IOException {
client().index(
indexRequest("test").type("type").id("1")
.source(jsonBuilder().startObject().field("test", "I am sam i am").endObject())).actionGet();
client().admin().indices().prepareRefresh().execute().actionGet();
ensureGreen();
SearchSourceBuilder searchSource = SearchSourceBuilder.searchSource().ext(
Collections.singletonList(new AnalyzedTextFetchBuilder().field("test")));
SearchResponse response = client().prepareSearch().setSource(searchSource).get();
assertSearchResponse(response);
logger.info(response.toString());
SearchHit hit = response.getHits().getAt(0);
// get the fields from the response
SearchHitField fields = hit.field(AnalyzedTextFetchSubPhase.NAME);
List<String> termVectors = fields.getValue();
assertArrayEquals(termVectors.toArray(new String[termVectors.size()]), new String[]{"i", "am", "sam", "i", "am"});
logger.info("{}", termVectors);
}
示例12: getContributionsByCandName
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
public void getContributionsByCandName(Client client, String candName, int i)
{
QueryBuilder matchQuery = QueryBuilders.matchQuery("candNm", candName);
FilterBuilder contribRangeFilter = FilterBuilders.rangeFilter("contbReceiptAmt").gte(i);
StatisticalFacetBuilder facet = FacetBuilders.statisticalFacet("stat1").field("contbReceiptAmt");
SearchRequestBuilder request = client.prepareSearch("contributions")
.addSort(SortBuilders.fieldSort("contbReceiptAmt").order(SortOrder.DESC))
.setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(matchQuery).setPostFilter(contribRangeFilter)
.addFacet(facet).setFrom(0).setSize(100)
.addFields("contbrNm", "candNm", "contbrEmployer", "contbReceiptAmt");
System.out.println("SEARCH QUERY: " + request.toString());
SearchResponse response = request.execute().actionGet();
SearchHits searchHits = response.getHits();
SearchHit[] hits = searchHits.getHits();
for (SearchHit hit : hits)
{
Map<String, SearchHitField> fields = hit.getFields();
System.out.println(hit.getId() + ", contbrEmployer=" + fields.get("contbrEmployer").getValue().toString());
}
}
示例13: buildDataLayout
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
private void buildDataLayout(SearchHits hits) {
logger.info("buildDataLayout");
// For each row
for (int i = 0; i < hits.getHits().length; i++) {
// Row n
Map<String, SearchHitField> responseFields = hits.getAt(i).getFields();
row = sheet.createRow(rownumber);
for (int j = 0; j < configObj.length(); j++) {
cell = row.createCell(j);
JSONObject headerJSON = (JSONObject) configObj.get(j);
String content = null;
content = headerJSON.getString(GlobalData.formatStr).trim();
content = getExprValue(responseFields, content);
writeToCell(j, content, cell);
}
rownumber++;
}
}
示例14: shouldReindexTTL
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
@Test
public void shouldReindexTTL() throws ExecutionException, InterruptedException {
//given
embeddedElasticsearchCluster.createIndex(SOURCE_INDEX, DATA_TYPE, mappingWithTTL());
embeddedElasticsearchCluster.createIndex(TARGET_INDEX, DATA_TYPE, mappingWithTTL());
ElasticSearchQuery elasticSearchQuery = embeddedElasticsearchCluster.createInitialQuery("");
indexSampleDataWithTTL();
ElasticDataPointer sourceDataPointer = embeddedElasticsearchCluster.createDataPointer(SOURCE_INDEX);
ElasticDataPointer targetDataPointer = embeddedElasticsearchCluster.createDataPointer(TARGET_INDEX);
//when
ReindexInvoker.invokeReindexing(sourceDataPointer, targetDataPointer, EmptySegmentation.createEmptySegmentation(elasticSearchQuery));
SearchResponse targetResponse = embeddedElasticsearchCluster.client().prepareSearch(TARGET_INDEX).addFields("_ttl").get();
//then
assertThat(embeddedElasticsearchCluster.count(SOURCE_INDEX)).isEqualTo(1L);
assertThat(embeddedElasticsearchCluster.count(TARGET_INDEX)).isEqualTo(1L);
Map<String, SearchHitField> resultFields = targetResponse.getHits().getAt(0).getFields();
assertThat(resultFields.containsKey("_ttl"));
assertThat((Long) resultFields.get("_ttl").value() > 0L);
}
示例15: buildDataLayout
import org.elasticsearch.search.SearchHitField; //导入依赖的package包/类
private void buildDataLayout(SearchHits hits) {
System.out.println("buildDataLayout");
// For each row
for (int i = 0; i < hits.getHits().length; i++) {
// Row n
Map<String, SearchHitField> responseFields = hits.getAt(i).getFields();
row = sheet.createRow(rownumber);
for (int j = 0; j < configObj.length(); j++) {
cell = row.createCell((short) j);
JSONObject headerJSON = (JSONObject) configObj.get(j);
// JSONArray fields = headerJSON.getJSONArray("fields");
String format = null;
format = headerJSON.getString("format");
format = getExprValue(responseFields, format);
cell.setCellValue(format);
}
rownumber++;
}
}