Search in sources :

Example 11 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project elasticflow by springwings.

the class EsSearcher method addResult.

private void addResult(SearcherResult res, SearchResponse response, SearcherModel<?, ?, ?> fq, List<String> returnFields) {
    SearchHits searchHits = response.getHits();
    res.setTotalHit(searchHits.getTotalHits().value);
    SearchHit[] hits = searchHits.getHits();
    for (SearchHit h : hits) {
        Map<String, DocumentField> fieldMap = h.getFields();
        ResponseDataUnit u = ResponseDataUnit.getInstance();
        for (Map.Entry<String, DocumentField> e : fieldMap.entrySet()) {
            String name = e.getKey();
            EFField param = instanceConfig.getWriteFields().get(name);
            DocumentField v = e.getValue();
            if (param != null && param.getSeparator() != null) {
                u.addObject(name, v.getValues());
            } else if (returnFields.contains(name)) {
                u.addObject(name, v.getValue());
            }
        }
        if (fq.isShowQueryInfo()) {
            u.addObject("_SCORE", h.getExplanation().getValue());
            u.addObject("_EXPLAINS", h.getExplanation().toString().replace("", ""));
        }
        res.getUnitSet().add(u);
    }
    if (fq.getFacetSearchParams() != null && response.getAggregations() != null) {
        res.setFacetInfo(JSON.parseObject(response.toString()).get("aggregations"));
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) DocumentField(org.elasticsearch.common.document.DocumentField) EFField(org.elasticflow.field.EFField) ResponseDataUnit(org.elasticflow.model.searcher.ResponseDataUnit) SearchHits(org.elasticsearch.search.SearchHits) Map(java.util.Map)

Example 12 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project spring-data-elasticsearch by spring-projects.

the class DocumentAdaptersUnitTests method shouldAdaptSearchResponse.

// DATAES-628, DATAES-848
@Test
public void shouldAdaptSearchResponse() {
    Map<String, DocumentField> fields = Collections.singletonMap("field", new DocumentField("field", Collections.singletonList("value")));
    SearchShardTarget shard = new SearchShardTarget("node", new ShardId("index", "uuid", 42), null);
    SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), null, fields);
    searchHit.shard(shard);
    searchHit.setSeqNo(1);
    searchHit.setPrimaryTerm(2);
    searchHit.score(42);
    SearchDocument document = DocumentAdapters.from(searchHit);
    assertThat(document.getIndex()).isEqualTo("index");
    assertThat(document.hasId()).isTrue();
    assertThat(document.getId()).isEqualTo("my-id");
    assertThat(document.hasVersion()).isFalse();
    assertThat(document.getScore()).isBetween(42f, 42f);
    assertThat(document.get("field")).isEqualTo("value");
    assertThat(document.hasSeqNo()).isTrue();
    assertThat(document.getSeqNo()).isEqualTo(1);
    assertThat(document.hasPrimaryTerm()).isTrue();
    assertThat(document.getPrimaryTerm()).isEqualTo(2);
}
Also used : ShardId(org.elasticsearch.index.shard.ShardId) DocumentField(org.elasticsearch.common.document.DocumentField) SearchHit(org.elasticsearch.search.SearchHit) SearchShardTarget(org.elasticsearch.search.SearchShardTarget) Text(org.elasticsearch.common.text.Text) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test)

Example 13 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project spring-data-elasticsearch by spring-projects.

the class DocumentAdaptersUnitTests method shouldAdaptGetResponse.

// DATAES-628, DATAES-848
@Test
public void shouldAdaptGetResponse() {
    Map<String, DocumentField> fields = Collections.singletonMap("field", new DocumentField("field", Collections.singletonList("value")));
    GetResult getResult = new GetResult("index", "type", "my-id", 1, 2, 42, true, null, fields, null);
    GetResponse response = new GetResponse(getResult);
    Document document = DocumentAdapters.from(response);
    assertThat(document.getIndex()).isEqualTo("index");
    assertThat(document.hasId()).isTrue();
    assertThat(document.getId()).isEqualTo("my-id");
    assertThat(document.hasVersion()).isTrue();
    assertThat(document.getVersion()).isEqualTo(42);
    assertThat(document.get("field")).isEqualTo("value");
    assertThat(document.hasSeqNo()).isTrue();
    assertThat(document.getSeqNo()).isEqualTo(1);
    assertThat(document.hasPrimaryTerm()).isTrue();
    assertThat(document.getPrimaryTerm()).isEqualTo(2);
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) GetResult(org.elasticsearch.index.get.GetResult) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Document(org.springframework.data.elasticsearch.core.document.Document) GetResponse(org.elasticsearch.action.get.GetResponse) Test(org.junit.jupiter.api.Test)

Example 14 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project spring-data-elasticsearch by spring-projects.

the class DocumentAdaptersUnitTests method shouldAdaptGetResult.

// DATAES-799, DATAES-848
@Test
public void shouldAdaptGetResult() {
    Map<String, DocumentField> fields = Collections.singletonMap("field", new DocumentField("field", Collections.singletonList("value")));
    GetResult getResult = new GetResult("index", "type", "my-id", 1, 2, 42, true, null, fields, null);
    Document document = DocumentAdapters.from(getResult);
    assertThat(document.getIndex()).isEqualTo("index");
    assertThat(document.hasId()).isTrue();
    assertThat(document.getId()).isEqualTo("my-id");
    assertThat(document.hasVersion()).isTrue();
    assertThat(document.getVersion()).isEqualTo(42);
    assertThat(document.get("field")).isEqualTo("value");
    assertThat(document.hasSeqNo()).isTrue();
    assertThat(document.getSeqNo()).isEqualTo(1);
    assertThat(document.hasPrimaryTerm()).isTrue();
    assertThat(document.getPrimaryTerm()).isEqualTo(2);
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) GetResult(org.elasticsearch.index.get.GetResult) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Document(org.springframework.data.elasticsearch.core.document.Document) Test(org.junit.jupiter.api.Test)

Example 15 with DocumentField

use of org.elasticsearch.common.document.DocumentField in project spring-data-elasticsearch by spring-projects.

the class DocumentAdaptersUnitTests method shouldRenderToJson.

// DATAES-628
@Test
public void shouldRenderToJson() {
    Map<String, DocumentField> fields = new LinkedHashMap<>();
    fields.put("string", new DocumentField("string", Collections.singletonList("value")));
    fields.put("bool", new DocumentField("bool", Arrays.asList(true, true, false)));
    SearchHit searchHit = new SearchHit(123, "my-id", new Text("type"), fields, null);
    SearchDocument document = DocumentAdapters.from(searchHit);
    assertThat(document.toJson()).isEqualTo("{\"string\":\"value\",\"bool\":[true,true,false]}");
}
Also used : DocumentField(org.elasticsearch.common.document.DocumentField) SearchHit(org.elasticsearch.search.SearchHit) Text(org.elasticsearch.common.text.Text) LinkedHashMap(java.util.LinkedHashMap) SearchDocument(org.springframework.data.elasticsearch.core.document.SearchDocument) Test(org.junit.jupiter.api.Test)

Aggregations

DocumentField (org.elasticsearch.common.document.DocumentField)20 SearchHit (org.elasticsearch.search.SearchHit)8 Test (org.junit.jupiter.api.Test)6 SearchDocument (org.springframework.data.elasticsearch.core.document.SearchDocument)6 SearchResponse (org.elasticsearch.action.search.SearchResponse)5 Text (org.elasticsearch.common.text.Text)5 LinkedHashMap (java.util.LinkedHashMap)4 LocalDateTime (java.time.LocalDateTime)3 HashMap (java.util.HashMap)3 GetResponse (org.elasticsearch.action.get.GetResponse)3 PrestoException (com.facebook.presto.spi.PrestoException)2 ArrayList (java.util.ArrayList)2 Map (java.util.Map)2 GetResult (org.elasticsearch.index.get.GetResult)2 SearchHits (org.elasticsearch.search.SearchHits)2 Document (org.springframework.data.elasticsearch.core.document.Document)2 TrinoException (io.trino.spi.TrinoException)1 IOException (java.io.IOException)1 List (java.util.List)1 MessageSearchIndex (org.apache.james.mailbox.store.search.MessageSearchIndex)1