ElasticsearchRepository简单用法

2020-12-07 10:35:37 浏览数 (1)

1.查询全部

1.1 java

代码语言:javascript复制
 esBlogRepositoryl.findAll();

1.2 es

代码语言:javascript复制
GET /blog/_search
{
  "query": {
    "match_all": {
      "boost": 1
    }
  }
}

2.模糊查询

2.1 java

代码语言:javascript复制
        //fuzziness 即为最多纠正两个字母然后去匹配,默认为 auto(2)
        FuzzyQueryBuilder fuzzyQueryBuilder = QueryBuilders.fuzzyQuery("title", "springboot1");
        System.out.println(fuzzyQueryBuilder);
        Iterable<EsBlog> search = esBlogRepositoryl.search(fuzzyQueryBuilder);
        System.err.println("==============================================");
        search.forEach(x->{
            System.out.println(x);
        });

2.2 es

代码语言:javascript复制
GET /blog/_search
{
  "query": {
    "fuzzy": {
      "title": {
        "value": "springboot",
        "fuzziness": "AUTO",
        "prefix_length": 0,
        "max_expansions": 50,
        "transpositions": false,
        "boost": 1
      }
    }
  }
}

3.and查询

3.1 java

代码语言:javascript复制
        BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery();
        queryBuilders.must(QueryBuilders.matchPhraseQuery("title", "springboot"));
        queryBuilders.must(QueryBuilders.matchPhraseQuery("content", "springboot"));
        System.out.println(queryBuilders.toString());
        Iterable<EsBlog> search = esBlogRepositoryl.search(queryBuilders);
        System.err.println("==============================================");
        search.forEach(x -> {
            System.out.println(x);
        });

3.2 es

代码语言:javascript复制
GET /blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "title": {
              "query": "springboot",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        },
        {
          "match_phrase": {
            "content": {
              "query": "springboot",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

4.or查询

4.1 java

代码语言:javascript复制
        BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery();
        queryBuilders.should(QueryBuilders.matchPhraseQuery("title", "springboot1"));
        queryBuilders.should(QueryBuilders.matchPhraseQuery("content", "springboot"));
        System.out.println(queryBuilders.toString());
        Iterable<EsBlog> search = esBlogRepositoryl.search(queryBuilders);
        System.err.println("==============================================");
        search.forEach(x -> {
            System.out.println(x);
        });

4.2 es

代码语言:javascript复制
GET /blog/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match_phrase": {
            "title": {
              "query": "springboot1",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        },
        {
          "match_phrase": {
            "content": {
              "query": "springboot",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

5.分页并按照id倒叙

5.1 java

代码语言:javascript复制
        BoolQueryBuilder queryBuilders = QueryBuilders.boolQuery();
        queryBuilders.must(QueryBuilders.matchPhraseQuery("title", "springboot"));
        System.out.println(queryBuilders.toString());
        Pageable pageable = PageRequest.of(0, 2, Sort.Direction.DESC,"createTime");
        Page<EsBlog> search = esBlogRepositoryl.search(queryBuilders, pageable);
        System.out.println(search);
        List<EsBlog> content = search.getContent();
        System.err.println("==============================================");
        content.forEach(x->{
            System.out.println(x);
        });

5.2 es

代码语言:javascript复制
GET /blog/_search
{
  "query": {
    "bool": {
      "must": [
        {
          "match_phrase": {
            "title": {
              "query": "springboot",
              "slop": 0,
              "zero_terms_query": "NONE",
              "boost": 1
            }
          }
        }
      ],
      "adjust_pure_negative": true,
      "boost": 1
    }
  }
}

名称解释

  1. boost:搜索条件的权重,可以将某个搜索条件的权重加大。此时当匹配这个搜索条件和匹配另一个搜索条件的document,计算relevance score时,匹配权重更大的搜索条件的document,relevance score会更高,当然也就会优先被返回回来

0 人点赞