org.elasticsearch.search.SearchHitField Java Examples

The following examples show how to use org.elasticsearch.search.SearchHitField. 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: RwCrawlerThread.java    From elasticsearch-river-web with Apache License 2.0 6 votes vote down vote up
@Override
protected boolean isContentUpdated(final CrawlerClient client, final UrlQueue<?> urlQueue) {
    final RiverConfigManager riverConfigManager = SingletonLaContainer.getComponent(RiverConfigManager.class);
    final RiverConfig riverConfig = riverConfigManager.get(crawlerContext.getSessionId());
    if (riverConfig.isIncremental()) {
        final EsClient esClient = SingletonLaContainer.getComponent(EsClient.class);
        try {
            final SearchResponse response = esClient.prepareSearch(riverConfig.getIndex()).setTypes(riverConfig.getType())
                    .setQuery(QueryBuilders.termQuery("url", urlQueue.getUrl())).addField("lastModified")
                    .addSort("lastModified", SortOrder.DESC).execute().actionGet();
            final SearchHits hits = response.getHits();
            if (hits.getTotalHits() > 0) {
                final SearchHitField lastModifiedField = hits.getAt(0).getFields().get("lastModified");
                if (lastModifiedField != null) {
                    final Date lastModified = ConversionUtil.convert(lastModifiedField.getValue(), Date.class);
                    if (lastModified != null) {
                        urlQueue.setLastModified(lastModified.getTime());
                    }
                }
            }
        } catch (final Exception e) {
            logger.debug("Failed to retrieve lastModified.", e);
        }
    }
    return super.isContentUpdated(client, urlQueue);
}
 
Example #2
Source File: ResultMapperExt.java    From roncoo-education with MIT License 6 votes vote down vote up
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;
	}
}
 
Example #3
Source File: UKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #4
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 #5
Source File: FetchPhase.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private Map<String, SearchHitField> getSearchFields(SearchContext context, int nestedSubDocId, boolean loadAllStored, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) {
    Map<String, SearchHitField> searchFields = null;
    if (context.hasFieldNames() && !context.fieldNames().isEmpty()) {
        FieldsVisitor nestedFieldsVisitor = null;
        if (loadAllStored) {
            nestedFieldsVisitor = new AllFieldsVisitor();
        } else if (fieldNames != null || fieldNamePatterns != null) {
            nestedFieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.<String>emptySet() : fieldNames,
                    fieldNamePatterns == null ? Collections.<String>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 InternalSearchHitField(entry.getKey(), entry.getValue()));
                }
            }
        }
    }
    return searchFields;
}
 
Example #6
Source File: XiaoEUKResultMapper.java    From youkefu with Apache License 2.0 6 votes vote down vote up
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;
	}
}
 
Example #7
Source File: FieldDataFieldsFetchSubPhase.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    for (FieldDataFieldsContext.FieldDataField field : context.getFetchSubPhaseContext(CONTEXT_FACTORY).fields()) {
        if (hitContext.hit().fieldsOrNull() == null) {
            hitContext.hit().fields(new HashMap<String, SearchHitField>(2));
        }
        SearchHitField hitField = hitContext.hit().fields().get(field.name());
        if (hitField == null) {
            hitField = new InternalSearchHitField(field.name(), new ArrayList<>(2));
            hitContext.hit().fields().put(field.name(), hitField);
        }
        MappedFieldType fieldType = context.mapperService().smartNameFieldType(field.name());
        if (fieldType != null) {
            AtomicFieldData data = context.fieldData().getForField(fieldType).load(hitContext.readerContext());
            ScriptDocValues values = data.getScriptValues();
            values.setNextDocId(hitContext.docId());
            hitField.values().addAll(values.getValues());
        }
    }
}
 
Example #8
Source File: ElasticSearchService.java    From samantha with MIT License 6 votes vote down vote up
public Map<Map<String, String>, List<SearchHit>> searchFieldsByKeys(String index, String type,
                                                              List<String> keys,
                                                              List<String> fields,
                                                              JsonNode data) {
    Map<Map<String, String>, List<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());
            }
        }
        if (keyVals.containsKey(keyVal)) {
            keyVals.get(keyVal).add(hit);
        } else {
            List<SearchHit> vals = new ArrayList<>();
            vals.add(hit);
            keyVals.put(keyVal, vals);
        }
    }
    return keyVals;
}
 
Example #9
Source File: TTLTest.java    From elasticsearch-reindex-tool with Apache License 2.0 6 votes vote down vote up
@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);
}
 
Example #10
Source File: DeploymentSystemTest.java    From logstash with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeploymentExternalConfiguration() throws Exception {
    final ElasticsearchContainer elasticsearchInstance = new ElasticsearchContainer(dockerClient);
    cluster.addAndStartContainer(elasticsearchInstance);

    final File logstashConfig = new File(tmpDir, "logstash.config");
    FileUtils.writeStringToFile(logstashConfig, "input { generator {} } output { elasticsearch { hosts => \"" + elasticsearchInstance.getIpAddress() + ":9200" + "\" } }");

    Client elasticsearchClient = elasticsearchInstance.createClient();

    deployScheduler(null, null, false, logstashConfig, false);

    SECONDS.sleep(2);

    await().atMost(20, SECONDS).pollDelay(1, SECONDS).until(() -> {
        final SearchHits hits = elasticsearchClient.prepareSearch("logstash-*").setQuery(QueryBuilders.simpleQueryStringQuery("Hello*")).addField("message").addField("mesos_agent_id").execute().actionGet().getHits();
        assertNotEquals(0, hits.totalHits());
        Map<String, SearchHitField> fields = hits.getAt(0).fields();

        String esMessage = fields.get("message").getValue();
        assertEquals("Hello world!", esMessage.trim());
    });

}
 
Example #11
Source File: ExportFields.java    From elasticsearch-inout-plugin with Apache License 2.0 6 votes vote down vote up
@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    SearchHitField field = hit.getFields().get(fieldName);
    if (field != null && !field.values().isEmpty()) {
        if (field.values().size() == 1) {
            builder.field(field.name(), field.values().get(0));
        } else {
            builder.field(field.name());
            builder.startArray();
            for (Object value : field.values()) {
                builder.value(value);
            }
            builder.endArray();
        }
    }
    return builder;
}
 
Example #12
Source File: FieldReader.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Long read(SearchHit hit) {
    SearchHitField field = hit.getFields().get(
            TTLFieldMapper.NAME);
    if (field != null && !field.values().isEmpty()) {
        return field.value();
    }
    return null;
}
 
Example #13
Source File: FieldReader.java    From elasticsearch-inout-plugin with Apache License 2.0 5 votes vote down vote up
@Override
public Long read(SearchHit hit) {
    SearchHitField field = hit.getFields().get(
            TimestampFieldMapper.NAME);
    if (field != null && !field.values().isEmpty()) {
        return field.value();
    }
    return null;
}
 
Example #14
Source File: ExpanderUtilities.java    From samantha with MIT License 5 votes vote down vote up
static public void parseEntityFromSearchHit(List<String> entityFields,
                                            String elasticSearchScoreName,
                                            SearchHit hit, ObjectNode entity) {
    if (elasticSearchScoreName != null) {
        entity.put(elasticSearchScoreName, hit.getScore());
    }
    Map<String, SearchHitField> elasticSearchFields = hit.getFields();
    for (String fieldName : entityFields) {
        if (elasticSearchFields.containsKey(fieldName)) {
            entity.set(fieldName,
                    Json.toJson(elasticSearchFields
                            .get(fieldName).value()));
        }
    }
}
 
Example #15
Source File: InternalSearchHit.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, SearchHitField> fields() {
    if (fields == null) {
        return ImmutableMap.of();
    }
    return fields;
}
 
Example #16
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
protected Double getDoubleFieldValueOrNull(final SearchHit searchHit,
		final String fieldName) {
	final SearchHitField searchHitField = searchHit.field(fieldName);
	if (searchHitField != null && searchHitField.value() != null) {
		return Double.valueOf(searchHitField.value().toString());
	}
	return null;
}
 
Example #17
Source File: ElasticSearchResult.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public String[] getValues(String string) {
    String[] values = new String[hit.getFields().size()];
    int i=0;
    for (SearchHitField field: hit.getFields().values()) {
        values[i++] = field.getValue();
    }
    return values;
}
 
Example #18
Source File: ProductQueryServiceImpl.java    From searchanalytics-bigdata with MIT License 5 votes vote down vote up
protected String getFieldValueOrNull(final SearchHit searchHit,
		final String fieldName) {
	final SearchHitField searchHitField = searchHit.field(fieldName);
	if (searchHitField != null && searchHitField.value() != null) {
		return searchHitField.value().toString();
	}
	return null;
}
 
Example #19
Source File: DcSearchHitImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, DcSearchHitField> fields() {
    Map<String, DcSearchHitField> map = new HashMap<String, DcSearchHitField>();
    for (Map.Entry<String, SearchHitField> entry : this.searchHit.fields().entrySet()) {
        map.put(entry.getKey(), DcSearchHitFieldImpl.getInstance(entry.getValue()));
    }
    return map;
}
 
Example #20
Source File: BuscadorConteudo.java    From portal-de-servicos with MIT License 5 votes vote down vote up
private Page<PaginaEstatica> executaQuery(Optional<String> termoBuscado, Integer paginaAtual,
                                          Integer quantidadeDeResultados, Function<String, QueryBuilder> criaQuery) {
    Optional<String> termo = termoBuscado.filter(t -> !t.isEmpty());
    PageRequest pageable = new PageRequest(paginaAtual, quantidadeDeResultados);

    return termo.map(criaQuery)
            .map(q -> et.query(
                    new NativeSearchQueryBuilder()
                            .withIndices(PORTAL_DE_SERVICOS_INDEX)
                            .withTypes(ORGAO.getNome(), PAGINA_TEMATICA.getNome(), SERVICO.getNome())
                            .withFields("id", "tipoConteudo", "nome", "conteudo", "descricao")
                            .withQuery(q)
                            .withPageable(pageable)
                            .build(),
                    r -> new FacetedPageImpl<>(Stream.of(r.getHits().getHits())
                            .map(h -> new PaginaEstatica()
                                    .withId(h.field("id").value())
                                    .withTipoConteudo((String) Optional.ofNullable(h.field("tipoConteudo"))
                                            .filter(Objects::nonNull)
                                            .map(SearchHitField::value)
                                            .orElse("servico"))
                                    .withNome(h.field("nome").value())
                                    .withConteudo(Optional.ofNullable(h.field("descricao"))
                                            .orElse(h.field("conteudo"))
                                            .value()))
                            .collect(toList()), pageable, r.getHits().totalHits())))
            .orElse(SEM_RESULTADOS);
}
 
Example #21
Source File: DcSearchHitImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<DcSearchHitField> iterator() {
    Map<String, DcSearchHitField> map = new HashMap<String, DcSearchHitField>();
    for (Map.Entry<String, SearchHitField> entry : this.searchHit.fields().entrySet()) {
        map.put(entry.getKey(), DcSearchHitFieldImpl.getInstance(entry.getValue()));
    }
    return map.values().iterator();
}
 
Example #22
Source File: DcSearchHitFieldImpl.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * .
 * @param field .
 * @return .
 */
public static DcSearchHitField getInstance(SearchHitField field) {
    if (field == null) {
        return null;
    }
    return new DcSearchHitFieldImpl(field);
}
 
Example #23
Source File: DcSearchHitImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Map<String, DcSearchHitField> fields() {
    Map<String, DcSearchHitField> map = new HashMap<String, DcSearchHitField>();
    for (Map.Entry<String, SearchHitField> entry : this.searchHit.fields().entrySet()) {
        map.put(entry.getKey(), DcSearchHitFieldImpl.getInstance(entry.getValue()));
    }
    return map;
}
 
Example #24
Source File: DcSearchHitImpl.java    From io with Apache License 2.0 5 votes vote down vote up
@Override
public Iterator<DcSearchHitField> iterator() {
    Map<String, DcSearchHitField> map = new HashMap<String, DcSearchHitField>();
    for (Map.Entry<String, SearchHitField> entry : this.searchHit.fields().entrySet()) {
        map.put(entry.getKey(), DcSearchHitFieldImpl.getInstance(entry.getValue()));
    }
    return map.values().iterator();
}
 
Example #25
Source File: DcSearchHitFieldImpl.java    From io with Apache License 2.0 5 votes vote down vote up
/**
 * .
 * @param field .
 * @return .
 */
public static DcSearchHitField getInstance(SearchHitField field) {
    if (field == null) {
        return null;
    }
    return new DcSearchHitFieldImpl(field);
}
 
Example #26
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
protected String getFieldValueOrNull(SearchHit searchHit, String fieldName)
{
    final SearchHitField searchHitField = searchHit.field(fieldName);
    if (searchHitField != null && searchHitField.value() != null) {
        return searchHitField.value().toString();
    }
    return null;
}
 
Example #27
Source File: ProductQueryServiceImpl.java    From elasticsearch-tutorial with MIT License 5 votes vote down vote up
protected Double getDoubleFieldValueOrNull(SearchHit searchHit, String fieldName)
{
    final SearchHitField searchHitField = searchHit.field(fieldName);
    if (searchHitField != null && searchHitField.value() != null) {
        return Double.valueOf(searchHitField.value().toString());
    }
    return null;
}
 
Example #28
Source File: ReindexingService.java    From elasticsearch-reindexing with Apache License 2.0 5 votes vote down vote up
private void sendToLocalCluster(final String scrollId, final SearchHit[] hits) {

            // prepare bulk request
            final BulkRequestBuilder bulkRequest = client.prepareBulk();
            for (final SearchHit hit : hits) {
                IndexRequestBuilder builder = client.prepareIndex(toIndex,
                        toType != null ? toType : hit.getType(), hit.getId())
                        .setSource(hit.getSource());
                Map<String, SearchHitField> fields = hit.getFields();
                if (fields != null && fields.containsKey("_parent")) {
                    SearchHitField parentField = fields.get("_parent");
                    if (parentField != null) {
                        String parentId = parentField.getValue();
                        builder.setParent(parentId);
                    }
                }
                bulkRequest.add(builder);
            }

            // send bulk request, if success response got, searching the next 10 results using scroll_id
            // using this listener (inner class) to listen to results
            bulkRequest.execute(new ActionListener<BulkResponse>() {
                @Override
                public void onResponse(final BulkResponse bulkResponse) {
                    if (bulkResponse.hasFailures()) {
                        throw new ReindexingException(bulkResponse
                                .buildFailureMessage());
                    }
                    client.prepareSearchScroll(scrollId).setScroll(scroll)
                            .execute(ReindexingListener.this);
                }

                @Override
                public void onFailure(final Throwable e) {
                    ReindexingListener.this.onFailure(e);
                }
            });
        }
 
Example #29
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
protected float getFloatValue(final SearchHit hit, final String field) {
    final SearchHitField result = hit.field(field);
    if (result == null) {
        throw new TasteException(field + " is not found.");
    }
    final Number floatValue = result.getValue();
    if (floatValue == null) {
        throw new TasteException("The result of " + field + " is null.");
    }
    return floatValue.floatValue();
}
 
Example #30
Source File: ElasticsearchDataModel.java    From elasticsearch-taste with Apache License 2.0 5 votes vote down vote up
protected long getLongValue(final SearchHit hit, final String field) {
    final SearchHitField result = hit.field(field);
    if (result == null) {
        throw new TasteException(field + " is not found.");
    }
    final Number longValue = result.getValue();
    if (longValue == null) {
        throw new TasteException("The result of " + field + " is null.");
    }
    return longValue.longValue();
}