Skip to content

Instantly share code, notes, and snippets.

@dantin
Last active February 24, 2016 09:54
Show Gist options
  • Save dantin/ae3cce075473f061694e to your computer and use it in GitHub Desktop.
Save dantin/ae3cce075473f061694e to your computer and use it in GitHub Desktop.
elasticsearch-tips
elasticsearch的Tips
elasticsearch里的分页查询
主要是from和size
=======================================
http://localhost:9200/
/erp_base/es/_search
POST
{
"from": 0,
"size": 1,
"query": {
"match": {
"name": "b"
}
}
}
===================================
//分页查询
public static String findByPage(String queryParam,String indexName,String indexTypeName,int from,int size) {
PostMethod postMethod = new PostMethod("http://localhost:9200/"+indexName+"/"+indexTypeName+"/_search");//请求地址
//postMethod.setRequestBody("{\"query\": {\"match\": "+queryParam+"}}");
if(queryParam==null||queryParam.length()==0){ //如果查询条件为空,则查询所有
postMethod.setRequestBody("{\n" +
" \"from\": "+from+",\n" +
" \"size\": "+size+",\n" +
" \"query\": {\n" +
" \"match_all\": \n" +
" {}\n" +
" \n" +
" }\n" +
"}");
}else{
postMethod.setRequestBody("{\n" +
" \"from\": "+from+",\n" +
" \"size\": "+size+",\n" +
" \"query\": {\n" +
" \"match\": \n" +
" "+queryParam+"\n" +
" \n" +
" }\n" +
"}");
}
// 指定请求内容的类型
postMethod.setRequestHeader("Content-type", "text/xml; charset=UTF-8");
HttpClient httpclient = new HttpClient();
int result;
String rsJson="";
try {
result = httpclient.executeMethod(postMethod);
System.out.println("Response status code: " + result);//返回200为成功
rsJson = new String(postMethod.getResponseBodyAsString().getBytes(), "UTF-8");
postMethod.releaseConnection();//释放连接
} catch (HttpException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rsJson;
}
public static void main(String[] args){ //findByPage
JSONObject queryParam = new JSONObject();
queryParam.put("note","b111");
String rsJson = findByPage(null,"base","es",0,3);
System.out.println("======================查询结果:"+rsJson);
}
find score bigger than value
{
"min_score": 0.5,
"query" : {
"term" : { "user" : "kimchy" }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment