如何获取ElasticSearch中所有字段名称的列表?

时间:2016-05-24 02:05:43

标签: elasticsearch

我一直在尝试动态获取我的ElasticSearch数据库中的所有字段名称,以便我可以将它们输入到GUI下拉搜索框中。

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

有没有办法获得字段名称:奇数,偶数和素数 这样我可以在用户界面中将它们输入到Jcombobox中吗?

2 个答案:

答案 0 :(得分:0)

在jquery ......

//...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
  });
});

答案 1 :(得分: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;

这很丑陋,但这就是我最终解决问题并抓住字段名称的方式。