ES的模糊查询(ES查询),在多个索引或者单个索引,全局查询
官方的简单查询如下图:
可以在new SearchRequest()里的()中加入索引,就会根据下图的代码查询索引下所有数据。
或者可以指定多个索引进行查询:
因为FieldCapabilitiesRequest继承的是SearchRequest,所以只需要直接像FieldCapabilitiesRequest一样把fields()和indices()添加到SearchRequest后面就行了。可以指定多个field和index。
查询某个索引下的所有数据:
在第一张图上的searchSourceBuilder.query()中添加参数如下两个:
// 我用的7.7es会自动中文分词
QueryBuilders.multiMatchQuery("我是老八");
//我用的7.7es会自动中文分词
// new MultiMatchQueryBuilder("我是宋三")也行
new MultiMatchQueryBuilder("我是宋三","user","message");
我自己的测试代码如下:(下面的可以不看,我自己存下来的方便自己查看)
package com.rg1b;
import org.apache.http.HttpHost;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.MatchQueryBuilder;
import org.elasticsearch.index.query.MultiMatchQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.fetch.subphase.highlight.HighlightField;
import java.net.InetAddress;
import java.sql.*;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
public class Ceshi {
public static void createAndupdate(String[] args) throws Exception{
String sql = "select * from BLOG_USER LIMIT 100 ";
String driver = "com.mysql.cj.jdbc.Driver";
String url = "jdbc:mysql://ip:3306/数据库?serverTimezone=UTC&autoReconnect=true";
String user = "账号";
String pwd = "密码";
Class.forName(driver);
Connection conn = DriverManager.getConnection(url, user, pwd);
Statement statement = conn.createStatement();
ResultSet rs = statement.executeQuery(sql);
String username = "";
String password = "";
String Uname = "";
String ID = "";
String phone = "";
String mailbox = "";
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
));
Map<String, String> jsonMap = new HashMap<>();
//批量处理类
BulkRequest bulkrequest = new BulkRequest();
//批量从mysql中引到ES
while(rs.next()){
username = rs.getString("username");
password = rs.getString("password");
Uname = rs.getString("Uname");
ID = rs.getString("ID");
phone = rs.getString("phone");
mailbox = rs.getString("mailbox");
jsonMap.put("username", username);
jsonMap.put("password", password);
jsonMap.put("Uname", Uname);
jsonMap.put("ID", ID);
jsonMap.put("phone",phone);
jsonMap.put("mailbox", mailbox);
IndexRequest request = new IndexRequest("blog_user")
.id(ID).source(jsonMap);
bulkrequest.add(request);
}
BulkResponse bulkResponse = client.bulk(bulkrequest, RequestOptions.DEFAULT);
System.out.println(bulkResponse.toString());
}
//查询
public static void select(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
));
SearchRequest searchRequest = new SearchRequest();
searchRequest.indices("posts");
SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();
// String[] includeFields = new String[] {"user"};
// String[] excludeFields = new String[] {""};
// searchSourceBuilder.fetchSource(includeFields, excludeFields);
// searchSourceBuilder.query(QueryBuilders.multiMatchQuery("我是老八"));
searchSourceBuilder.query(new MultiMatchQueryBuilder("我是张三","user","message"));
searchRequest.source(searchSourceBuilder);
SearchResponse searchResponse = client.search(searchRequest, RequestOptions.DEFAULT);
SearchHits hits = searchResponse.getHits();
SearchHit[] searchHits = hits.getHits();
for (SearchHit hit : searchHits) {
// do something with the SearchHit
System.out.println(hit.getSourceAsString());
}
}
//最简单的添加索引数据
public static void zz(String[] args) throws Exception{
RestHighLevelClient client = new RestHighLevelClient(
RestClient.builder(
new HttpHost("localhost", 9200, "http")
));
Map<String, Object> jsonMap = new HashMap<>();
jsonMap.put("user", "bababa");
jsonMap.put("postDate", new Date());
jsonMap.put("message", "张三");
IndexRequest request = new IndexRequest("posts")
.id("1").source(jsonMap);
IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT);
System.out.println(indexResponse.toString());
}
public static void main(String[] args) throws Exception{
select(null);
}
}