Elasticsearch Java API:返回一个布尔值

时间:2015-11-27 23:17:33

标签: elasticsearch java-api

我需要构建一个返回Java可以读取的布尔值的查询。当我尝试这个时:

public static string ShowFields(object whatever)
{
    string S = "";
    foreach(var field in whatever.GetType().GetFields(
        BindingFlags.Instance |
        BindingFlags.NonPublic |
        BindingFlags.Public))
    {
        S = S + field.Name + ": " + field.GetValue(whatever).ToString() + "\n";
    }
    return S;
}

返回user123的所有匹配。如果user123不存在,则返回以下内容:

 QueryBuilder qb = matchQuery(
     "user.name",                  
     "user123"
 );
 SearchResponse response = client.prepareSearch("logstash-*").addFields("user.name")
.setTypes("names").setQuery(qb)
.execute()
.actionGet();

然而,我需要的是真值或假值。如果用户存在,则为true,否则为false,则为false。我怎么能做到这一点?

谢谢。

1 个答案:

答案 0 :(得分:1)

如何Count API并检查结果是否为0?

QueryBuilder qb = matchQuery(
 "user.name",                  
 "user123"
);
SearchResponse response = client.prepareCount("logstash-*").addFields("user.name")
                      .setTypes("names").setQuery(qb)
                      .execute()
                      .actionGet();
return response.count() != 0 // return true if found