Search in sources :

Example 21 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class OldIndexBackwardsCompatibilityIT method assertStoredBinaryFields.

/**
     * 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());
    }
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) SearchRequestBuilder(org.elasticsearch.action.search.SearchRequestBuilder) SearchHit(org.elasticsearch.search.SearchHit) SearchHitField(org.elasticsearch.search.SearchHitField) SearchHits(org.elasticsearch.search.SearchHits)

Example 22 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class SearchFieldsIT method testSingleValueFieldDatatField.

// see #8203
public void testSingleValueFieldDatatField() throws ExecutionException, InterruptedException {
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("type", "test_field", "type=keyword").get());
    indexRandom(true, client().prepareIndex("test", "type", "1").setSource("test_field", "foobar"));
    refresh();
    SearchResponse searchResponse = client().prepareSearch("test").setTypes("type").setSource(new SearchSourceBuilder().query(QueryBuilders.matchAllQuery()).fieldDataField("test_field")).get();
    assertHitCount(searchResponse, 1);
    Map<String, SearchHitField> fields = searchResponse.getHits().getHits()[0].getFields();
    assertThat(fields.get("test_field").getValue(), equalTo("foobar"));
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder)

Example 23 with SearchHitField

use of org.elasticsearch.search.SearchHitField in project elasticsearch by elastic.

the class SearchFieldsIT method testLoadMetadata.

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"));
}
Also used : SearchHitField(org.elasticsearch.search.SearchHitField) Matchers.containsString(org.hamcrest.Matchers.containsString) SearchResponse(org.elasticsearch.action.search.SearchResponse) ElasticsearchAssertions.assertSearchResponse(org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)

Aggregations

SearchHitField (org.elasticsearch.search.SearchHitField)23 SearchHit (org.elasticsearch.search.SearchHit)16 SearchResponse (org.elasticsearch.action.search.SearchResponse)12 SearchHits (org.elasticsearch.search.SearchHits)10 ArrayList (java.util.ArrayList)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)7 IOException (java.io.IOException)6 List (java.util.List)6 Text (org.elasticsearch.common.text.Text)6 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)5 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)4 InnerHitBuilder (org.elasticsearch.index.query.InnerHitBuilder)4 Script (org.elasticsearch.script.Script)4 CollapseBuilder (org.elasticsearch.search.collapse.CollapseBuilder)4 HighlightBuilder (org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder)4 QueryHelper (org.codelibs.fess.helper.QueryHelper)3 FessConfig (org.codelibs.fess.mylasta.direction.FessConfig)3 OptionalEntity (org.dbflute.optional.OptionalEntity)3