elasticsearch使用wildcard多字段模糊搜索
匹配0个或多个字符
? | 匹配任意字符 |
---|---|
* | 匹配0个或多个字符 |
1.wildcard 单条件搜索
index 索引 type 类型。
GET /index/type/_search
{
"size": 20,
"from": 0,
"query": {
"bool": {
"should": [{
"wildcard": {
"name": "*java*
}
}]
}
}
}
搜索名字中含有java文档(name中只要包含java即可)
2.wildcard 多条件搜索
GET /index/type/_search
{
"size": 20,
"from": 0,
"query": {
"bool": {
"should": [{
"wildcard": {
"name": "*java*"
},
{
"wildcard": {
"name": "?jack*"
}
}
}]
}
}
}
搜索名字中含有_jack**的文档;如: wjacksss; ajackwww
3.wildcard 代码实现单条件搜索
SearchRequest request = new SearchRequest(index);
WildcardQueryBuilder query = QueryBuilders.wildcardQuery("name", "*张*");;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(queryBuilder);
builder.query(boolQueryBuilder);
request.source(builder);
search = client.search(request, RequestOptions.DEFAULT);
如搜索张就可以找到name为张相关数据 :张三
搜索 三 也可以找到name为三相关数据 :张三
4.wildcard 代码实现多条件搜索
SearchRequest request = new SearchRequest(index);
WildcardQueryBuilder queryBuilder1 = QueryBuilders.wildcardQuery("*张*");;
WildcardQueryBuilder queryBuilder2 = QueryBuilders.wildcardQuery("*jack*");;
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
boolQueryBuilder.should(queryBuilder1);
boolQueryBuilder.should(queryBuilder2);
builder.query(boolQueryBuilder);
request.source(builder);
search = client.search(request, RequestOptions.DEFAULT);