easy-es使用详解与源码解析

1.git clone后,easy-es-core中的pom中需要引入:

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
            <version>4.4.12</version>
        </dependency>

2.easy-es-sample 中提供了基本案例,可以用来解析源码。

3.easy-es-common中的pom里可以看到,它是基于elasticsearch-rest-high-level-client的。

如果不熟悉elasticsearch-rest-high-level-client,建议先熟悉一下。

1. DSL语句

1.1 DSL常见的查询分类

查询所有:match_all (一般也就是测试用用)

全文检索:利用分词器对用户输入的内容进行分词后进行匹配查询

match_query,multi_match_query

精确查询:根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等
ids,range,term

地理(geo)查询:根据经纬度查询。
geo_distance,geo_bounding_box

复合查询:将上述查询条件组合起来,合并查询条件

bool,function_score

1.2 全文检索

对用户输入的内容进行分词后进行匹配查询

GET /easyes_document/_search
{
  "query": {
    "match_all": {}
  }
}

GET /easyes_document/_search
{
  "query": {
    "match": {
      "title": "老王"
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "multi_match": {
      "query": "老王",
      "fields": ["content","creator"]
    }
  }
}

1.3 精确检索

根据精确词条值查找数据,主要字段类型为keyword,日期,数值,布尔等,不会对用户输入的内容进行分词

term:根据词条精确值查询
range:根据值的范围查询

#term 的使用

GET /easyes_document/_search
{
  "query": {
    "term": {
      "_id": {
        "value": "3"
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "star_num": {
        "gte": 20
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "star_num": {
        "lte": 5
      }
    }
  }
}

GET /easyes_document/_search
{
  "query": {
    "range": {
      "gmt_create": {
        "lte": "2023-06-18 07:38:43"
      }
    }
  }
}

1.4地理坐标查询

如果没有提示需要硬写:

GET /easyes_document/_search
{
  "query": {
    "geo_distance": {
      "location": "40.13,116.63",
      "distance": "2km"
    }
  }
}

1.5 function score query改变权重影响得分

在这里插入图片描述

 term默认对keyword中文无效,需要field.keyword才可以。

#function score query
GET /easyes_document/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "老王"
        } 
      },
      "functions": [
        {
          "filter": { 
            "term": {
              "sub_title.keyword": "小毛子"
            }
          },
          "weight":100
        }
      ]
    }
  }
}

1.6 复合查询booleanQuery

布尔查询是一个或多个查询子句的组合。子查询的组合方式有:

  • must:必须匹配每个子查询,类似“与”
  • should:选择性匹配子查询,类似“或”【应该】
  • must_not:必须不匹配,不参与算分,类似“非”
  • filter:必须匹配,不参与算分

找汉子,把地理位置写在must中和放在filter中得分是不一样的

#bool query
GET /easyes_document/_search
{
  "query": {
    "bool": {
      	"must": [
      		{
            "match": {"title" : "测试"}
      		},
      		{
      		  "match": {"creator" : "老汉"}
      		},
      		{
    	      "geo_distance": {
              "geo_location": "POINT (34.400544 73.53028599999999)",
              "distance": "500km"
            }
    	    }
    	  ],
    	  "must_not": [
    	    {
    	      "range": {
    	        "star_num": {
    	          "gte": 10,
    	          "lte": 20
    	        }
    	      }
    	    }
    	  ]
    }
  }
}
#bool query
GET /easyes_document/_search
{
  "query": {
    "bool": {
      	"must": [
      		{
            "match": {"title" : "测试"}
      		},
      		{
      		  "match": {"creator" : "老汉"}
      		}
    	  ],
    	  "must_not": [
    	    {
    	      "range": {
    	        "star_num": {
    	          "gte": 10,
    	          "lte": 20
    	        }
    	      }
    	    }
    	  ],
    	  "filter": [
    	    {
    	      "geo_distance": {
              "geo_location": "POINT (34.400544 73.53028599999999)",
              "distance": "500km"
            }
    	    }
    	  ]
    }
  }
}

1.7 sort排序

sort集合,写在前面的起主要作用

#sort
GET /easyes_document/_search
{
  "query": {
    "function_score": {
      "query": {
        "match": {
          "title": "老王"
        } 
      },
      "functions": [
        {
          "filter": { 
            "term": {
              "sub_title.keyword": "小毛子"
            }
          },
          "weight":100
        }
      ]
    }
  },
  "sort": [
 
    {"star_num": "asc"},
    {"_score" : "desc"}
  ]
}

1.8 分页

GET /easyes_document/_search
{
  "query": {
    "match": {
      "title": "测试"
    }
  },
  "sort": [
    {
      "star_num": {
        "order": "desc"
      }
    }
  ],
  "from": 10, 
  "size": 10
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于Spring Boot整合Easy Elasticsearch的指导。 1. 添加依赖 首先,在`pom.xml`文件中添加Easy ElasticsearchElasticsearch的依赖: ```xml <dependency> <groupId>com.jun</groupId> <artifactId>easy-elasticsearch-spring-boot-starter</artifactId> <version>1.0.0</version> </dependency> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.12.0</version> </dependency> ``` 2. 配置Elasticsearch 在`application.yml`中添加Elasticsearch的配置信息: ```yaml spring: elasticsearch: rest: uris: http://localhost:9200 ``` 3. 创建Elasticsearch的Repository 创建一个继承自`ElasticsearchRepository`的接口,用于定义Elasticsearch的操作方法: ```java public interface BookRepository extends ElasticsearchRepository<Book, Long> { List<Book> findBooksByAuthor(String author); } ``` 其中,`Book`是我们要操作的实体类,`Long`是这个实体类的ID类型。 4. 测试Elasticsearch 可以编写一个测试方法来测试Elasticsearch是否成功整合: ```java @SpringBootTest class BookRepositoryTest { @Autowired private BookRepository bookRepository; @Test public void testSave() { Book book = new Book(); book.setId(1L); book.setTitle("Java编程思想"); book.setAuthor("Bruce Eckel"); bookRepository.save(book); } @Test public void testFind() { List<Book> books = bookRepository.findBooksByAuthor("Bruce Eckel"); System.out.println(books); } } ``` 执行测试方法后,如果能够正确输出结果,则说明Easy Elasticsearch已经成功整合到了Spring Boot中。 希望这些步骤能够对您有所帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值