2016-05-24 14 views
0

私はElasticSearchデータベース内のすべてのフィールド名を動的に取得しようとしていますので、それらをGUIドロップダウン検索ボックスに入力できます。ElasticSearchですべてのフィールド名のリストを取得する方法は?

{
"odd": ["three", "five", "nine"],
"even": ["two", "four", "six"],
"prime": ["seven", "thirteen", "seventeen"]
}

フィールド名を取得する方法はあります:偶数、奇数とプライム私は、ユーザーインターフェイスにするJComboBoxにそれらを入力することができます
このようなことを? jqueryので

+2

[Lucene/Elasticsearchインデックスのフィールドを表示](http://stackoverflow.com/questions/14165537/show-fields-for-a-lucene-elasticsearch-index)の可能な複製 – sunkuet02

答えて

0
GetMappingsResponse res = null; //Get entire mapping of elasticsearch 
    try { 
     res = client.admin().indices().getMappings(new GetMappingsRequest().indices("foo").types("bar")).get(); 
    } catch (InterruptedException e) { 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     e.printStackTrace(); 
    } 
    ImmutableOpenMap<String, MappingMetaData> mapping = res.mappings().get("foo"); 
    String[] FieldNames = new String[20]; 
    String[] FieldNameArray; 

    for (ObjectObjectCursor<String, MappingMetaData> c : mapping) { 
     FieldNames[0] = c.value.source().toString(); //grabs and stores the mapping 
    } 
    String dirtyField = FieldNames[0]; 
    List<String> badWords = Arrays.asList( "foo"); //here i parse the entire mapping to just obtain the field names 

    for (String badWord : badWords) { //filters the mapping so we can capture only the added metadata 
     dirtyField = dirtyField.replaceAll("" + badWord + "", ""); 
    } 
    String cleanField = dirtyField.replaceAll("[^\\w\\s]"," "); 

    FieldNameArray = cleanField.split("\\s+"); //splits the string into an array of individual words 
    System.out.println("Updated Field Name Array"); 
    return FieldNameArray; 

それはかなり醜いですが、これは私が問題を解決し、単にフィールド名をつかん終わった方法です。

0

...

//...Get data 

var hits = body.hits.hits; 
$.each(hits, function(index, value){ 
    var sources = hits[index]._source; 
    $.each(sources, function(i, j){//i is the name of the field and j is the value 
    //Use data 
    }); 
}); 
関連する問題