1、term query 分词精确查询,查询hotelName 分词后包含 hotel的term的文档
QueryBuilders.termQuery("hotelName","hotel")
2、terms Query 多term查询,查询hotelName 包含 hotel 或test 中的任何一个或多个的文档
QueryBuilders.termsQuery("hotelName","hotel","test")
3、range query
范围查询 查询hotelNo
QueryBuilders.rangeQuery("hotelNo")
.gt("10143262306") //大于 10143262306
.lt("101432623062055348221") //小于 101432623062055348221
.includeLower(true) //包括下界
.includeUpper(false); //包括上界
4、 exist query 查询字段不为null的文档 查询字段address 不为null的数据
QueryBuilders.existsQuery("address")
5、missing query
返回 没有字段或值为null或没有值的文档
java client 该方法已经标记为过时,推荐用exist代替 如下 existsQuery BoolQueryBuilder#mustNot(QueryBuilder)
QueryBuilders.missingQuery("accountGuid")
等同
QueryBuilders.boolQuery().mustNot(QueryBuilders.existsQuery("accountGuid"));
6.prefix query 匹配分词前缀 如果字段没分词,就匹配整个字段前缀
QueryBuilders.prefixQuery("hotelName","花园")
7、wildcard query 通配符查询,支持* 任意字符串;?任意一个字符
QueryBuilders.wildcardQuery("channelCode","ctr*")
QueryBuilders.wildcardQuery("channelCode","ctr?")
8、regexp query 正则表达式匹配分词,正则表达式自己写吧 略
9、 fuzzy query 分词模糊查询,通过增加fuzziness 模糊属性,来查询term 如下 能够匹配 hotelName 为 te el tel前或后加一个字母的term的 文档 fuzziness 的含义是检索的term 前后增加或减少n个单词的匹配查询
QueryBuilders.fuzzyQuery("hotelName", "tel").fuzziness(Fuzziness.ONE)
10、type Query 含义不是很理解,通过代码也还暂未理解 英文原文:Filters documents matching the provided document / mapping type.
QueryBuilders.typeQuery("channelCode")
11、idx Query 根据ID查询
QueryBuilders.idsQuery().addIds("exchange_operate_monitor_db$32293","exchange_operate_monitor_db$32294")
代码实现例子
package com.aura.utils; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexResponse; import org.elasticsearch.action.admin.indices.mapping.put.PutMappingResponse; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.IndicesAdminClient; import org.elasticsearch.client.transport.TransportClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.transport.InetSocketTransportAddress; import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.index.query.TermQueryBuilder; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.transport.client.PreBuiltTransportClient; import org.junit.Test; import java.io.IOException; import java.net.InetAddress; import java.net.UnknownHostException; import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder; public class ESUtils { //ES client private static TransportClient client; private static final String CLUSTER_NAME="elasticsearch"; private static final String HOST_IP="127.0.0.1"; private static final int TCP_PORT=9300; static Settings setting = Settings.builder().put("cluster.name",CLUSTER_NAME).build(); /** * 获取client对象 * */ public static TransportClient getClient(){ if(client==null){ synchronized (TransportClient.class){ try { client=new PreBuiltTransportClient(setting) .addTransportAddress( new InetSocketTransportAddress(InetAddress.getByName(HOST_IP),TCP_PORT) ); } catch (UnknownHostException e) { e.printStackTrace(); } } } System.out.println("连接成功"+client.toString()); return client; } /** * index * 获取index admin管理者 通过下面这个API 就能获取index的管理者 */ public static IndicesAdminClient getIndicesAdminClient(){ return getClient().admin().indices(); } /** * 创建一个index库 * 注意 es里面索引库的名称 都不需要小写 */ public static boolean createIndex(String indexName){ //用获取到的index管理对象去创建index 并设置该索引的主分片个数 副本个数 CreateIndexResponse response = getIndicesAdminClient().prepareCreate(indexName.toLowerCase()).setSettings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2)).execute().actionGet(); //如果操作成功则返回true 失败则返回false return response.isShardsAcked(); } /** * 创建index * @param indexName * @param numberShards * @param numberreplicas * @return */ public static boolean createIndex(String indexName,int numberShards,int numberreplicas){ //用获取到的index管理对象去创建index 并设置该索引的主分片个数 2个副分片 CreateIndexResponse response = getIndicesAdminClient().prepareCreate(indexName.toLowerCase()).setSettings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2)).execute().actionGet(); //如果操作成功则返回true 失败则返回false return response.isShardsAcked(); } /** * 删除index * @param indexName */ public static boolean deleteIndex(String indexName){ DeleteIndexResponse response= getIndicesAdminClient().prepareDelete(indexName.toLowerCase()).execute().actionGet(); return response.isAcknowledged(); } public static boolean setIndexMapping(String indexName,String typeName,XContentBuilder mappingStr){ PutMappingResponse response = getIndicesAdminClient().//获取索引管理者 preparePutMapping(indexName)//设置索引名 .setType(typeName)//设置表名 .setSource(mappingStr)//设置映射关系字符串 .execute()//执行操作 .actionGet(); return response.isAcknowledged(); } /** * 查询数据 根据ID 号 */ public static boolean query(){ TransportClient client = getClient(); GetResponse getFields = client.prepareGet("yuanshuai", "ES", "1").execute().actionGet(); //{"user":"malaoshi", //"postdate":"2018" //} System.out.println(getFields.getSourceAsString()); // {postdate=2018, user=malaoshi} System.out.println(getFields.getSourceAsMap()); return getFields.isExists(); } /** * 根据具体信息去匹配查询 */ public static boolean querydetail(){ TransportClient client = getClient(); TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("user", "malaoshi"); SearchResponse searchResponse = client.prepareSearch("yuanshuai").setQuery(termQueryBuilder).setTypes("testes").execute().actionGet(); SearchHits hits = searchResponse.getHits(); System.out.println(hits.totalHits+"000"); for (SearchHit hit:hits) { System.out.println(hit.getScore()+"1111"); System.out.println(hit.getSourceAsString()+"2222"); } return false; } @Test public void test1(){ /** * put yuanshuai/_mapping/type_xx { "properties":{ "user":{ "type":"text" }, "postDate":{ "type":"date" }, "message":{ "type":"text" } } } */ try { XContentBuilder xContentBuilder = jsonBuilder().startObject() .startObject("properties") .startObject("user") .field("type", "text") .endObject() .startObject("postDate") .field("type", "date") .endObject() .startObject("message") .field("type", "text") .endObject() .endObject() .endObject(); setIndexMapping("yuanshuai","type_xx",xContentBuilder); } catch (IOException e) { e.printStackTrace(); } } @Test public void test2(){ String json ="{\n" + " \"user\":{\n" + " \"type\":\"text\"\n" + " },\n" + " \"postDate\":{\n" + " \"type\":\"date\"\n" + " },\n" + " \"message\":{\n" + " \"type\":\"text\"\n" + " }\n" + " }"; TransportClient client = getClient(); //获取到客户端 创建索引库 表 然后设置表的映射 XContentType.CBOR.JSON 指定我们传入的是个json字符串 相当于给表创建字段及类型 IndexResponse response = client.prepareIndex ("test2", "testtype2").setSource(json, XContentType.CBOR.JSON).get(); System.out.println(response.status().getStatus()); } public static void main(String[] args) { //创建索引库 //createIndex("yuanshuai",3,2); //删除索引库 //deleteIndex("yuanshuai"); //query(); querydetail(); } }
package com.thunisoft.zsjinterface.service;
import com.thunisoft.zsjinterface.dao.EsDao;
import com.thunisoft.zsjinterface.dao.Testdao;
import com.thunisoft.zsjinterface.esconnect.ConnectEs;
import com.thunisoft.zsjinterface.pojo.RY;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.search.SearchType;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.lang3.StringUtils;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.WildcardQueryBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
public class RyListImpl implements RyList{
@Autowired
EsDao esDao;
@Autowired
ConnectEs connectEs;
@Override
public List
TransportClient client = connectEs.getClient();
if(ztlx==0||pagenum==0||limit==0){
List
return list;
}
if(ztlx==1){
if(StringUtils.isNotBlank(zjhm)){
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery("C_XM", "*" + zjhm + "*");
SearchResponse searchResponse = client.prepareSearch("zsj_ryzt")
.setTypes("t_zsj_ry")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder1)
.execute()
.actionGet();
return esDao.getDataForParam(searchResponse);
}else if(StringUtils.isNotBlank(xm)){
//姓名模糊查询
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery("C_XM", "*" + xm + "*");
SearchResponse searchResponse = client.prepareSearch("zsj_ryzt")
.setTypes("t_zsj_ry")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder1)
.execute()
.actionGet();
return esDao.getDataForParam(searchResponse);
}else{
//range 查询
// pagenum,pagenum+limit
QueryBuilder queryBuilder=QueryBuilders.rangeQuery("NUM")
.gt(pagenum)
.lt(pagenum+limit)
.includeLower(true);
SearchResponse searchResponse = client.prepareSearch("zsj_ryzt")
.setTypes("t_zsj_ry")
.setSearchType(SearchType.DFS_QUERY_THEN_FETCH)
.setQuery(queryBuilder)
.execute()
.actionGet();
return esDao.getDataForParam(searchResponse);
}
}
return null;
}
}