Prerequisite: project uses spring-data-elasticsearch 3.2.0
A, QueryBuilder use record
(1) fuzzyQuery
function: fuzzy matching
principle: the situation after the fuzzy search technology, when searching, you might enter the search text appears incorrect spelling automatically misspelled search text, correct, correct to try data correction to match the index within a certain range if the big difference can not be searched
Java:
xxxRepository.search(QueryBuilders.fuzzyQuery("name", "张三"))
ES wording:
GET / my_index / my_type / _search
{
"Query": {
"Fuzzy": {
"text": {
"value": "Surprize",
"fuzziness": 2 // fuzziness is the correct match and then go up to two letters, The default is Auto (2)
}
}
}
}
(2)matchQuery
Function: match according to word
Java:
MatchQueryBuilder matchQuery = QueryBuilders.matchQuery("name", archiveListFilterDTO.getPersonName());
xxxRepository.search(matchQuery);
ES wording:
GET my_index/my_type/_search
{
"query": {
"match": {
"xxx": "Quick Foxes!"
}
}
}
(3)termQuery
Function: exact query exactly matches
Java:
TermQueryBuilder termQuery = QueryBuilders.termQuery("cid",archiveListFilterDTO.getPersonCid());
xxxRepository.search(termQuery);
ES wording:
GET my_index/_search
{
"query": {
"term" : {
"cid" : {
"value" : "5137376667422s31000000"
}
}
}
}
(4)rangeQuery
Function: range queries
Java:
RangeQueryBuilder rangeQueryBuilder = QueryBuilders.rangeQuery("dt")
.gte(dateFormat.parse(snapMapDTO.getStartTime()).getTime())
.lte(dateFormat.parse(snapMapDTO.getEndTime()).getTime());
xxxRepository.search(rangeQueryBuilder);
ES wording:
GET my_index/_search
{
"query": {
"range" : {
"personFileCreateTime" : {
"from" : 1572331788000,
"to" : 1572331789000,
"include_lower" : true,
"include_upper" : true
}
}
}
}