2017-04-22 12 views
0

私は、Java APIを使用して弾性を使って検索を実行すると結果が返ってきますが、結果から値を抽出しようとするとフィールドがありません。ElasticSearchヒットにフィールドがありません

ElasticSearchのv5.3.1

弾性API:org.elasticsearch.client:輸送V5.3.0

マイコード:forEachのがobjを実行

SearchRequestBuilder srb = client.prepareSearch("documents").setTypes("documents").setSearchType(SearchType.QUERY_THEN_FETCH).setQuery(qb).setFrom(0).setSize((10)).setExplain(false); 

srb.addDocValueField("title.raw"); 
SearchResponse response = srb.get(); 

response.getHits().forEach(new Consumer<SearchHit>() { 

     @Override 
     public void accept(SearchHit hit) { 
      System.out.println(hit); 
      Map<String, SearchHitField> fields = hit.getFields(); 

      Object obj = fields.get("title.raw").getValue(); 

     } 

    }); 

は常に戻って来ているヌル。フィールドにはtitle.rawのキーを持つ項目があり、SearchHitFieldがあります。

答えて

1

フィールドは、格納されたフィールドを取得しようとしているときにのみ使用されます。デフォルトでは、ソースを使用してフィールドを取得する必要があります。次のコードサンプルでは、​​私はそれを説明しようとします。

PUT documents 
{ 
    "settings": { 
    "number_of_replicas": 0, 
    "number_of_shards": 1 
    }, 
    "mappings": { 
    "document": { 
     "properties": { 
     "title": { 
      "type": "text", 
      "store": true 
     }, 
     "description": { 
      "type": "text" 
     } 
     } 
    } 
    } 
} 

PUT documents/document/_bulk 
{"index": {}} 
{"title": "help me", "description": "description of help me"} 
{"index": {}} 
{"title": "help me two", "description": "another description of help me"} 

GET documents/_search 
{ 
    "query": { 
    "match": { 
     "title": "help" 
    } 
    }, 
    "stored_fields": ["title"], 
    "_source": { 
    "includes": ["description"] 
    } 
} 

次に、格納されたフィールドのタイトルと通常のフィールドの説明の違いに注意してください。

{ 
    "_index": "documents", 
    "_type": "document", 
    "_id": "AVuciB12YLj8D0X3N5We", 
    "_score": 0.14638957, 
    "_source": { 
    "description": "another description of help me" 
    }, 
    "fields": { 
    "title": [ 
     "help me two" 
    ] 
    } 
} 
+0

です。だからあなたが言っているのは、私のフィールドのどれもが保存されているとマークされていないからです。しかし、私はソースからデータにアクセスできます。 – Seamus

+0

あなたのアドバイスに従って、私はソースを使用しました: 'Map source = hit.getSource();' すべてのフィールドにアクセスできます。ありがとう! – Seamus

関連する問題