ES查询索引字段的分词结果

一、_termvectors 

1、查看文档中某一个字段的分词结果

GET /{index}/{type}/{_id}/_termvectors?fields=[field]

2、样例:

text的值为:https://www.b4d99.com/html/202204/45672.html

GET http://IP:POST/textcontent_2022/textcontent/20220422191235893045256250/_termvectors?fields=text

得到的结果:

"terms": {
	"202204": {
		"term_freq": 1,
		"tokens": [
			{
				"position": 4,
				"start_offset": 27,
				"end_offset": 33
			}
		]
	},
	"45672": {
		"term_freq": 1,
		"tokens": [
			{
				"position": 5,
				"start_offset": 34,
				"end_offset": 39
			}
		]
	},
	"com": {
		"term_freq": 1,
		"tokens": [
			{
				"position": 2,
				"start_offset": 18,
				"end_offset": 21
			}
		]
	},
	"html": {
		"term_freq": 2,
		"tokens": [
			{
				"position": 3,
				"start_offset": 22,
				"end_offset": 26
			},
			{
				"position": 6,
				"start_offset": 40,
				"end_offset": 44
			}
		]
	},
	"https": {
		"term_freq": 1,
		"tokens": [
			{
				"position": 0,
				"start_offset": 0,
				"end_offset": 5
			}
		]
	},
	"www.b4d99": {
		"term_freq": 1,
		"tokens": [
			{
				"position": 1,
				"start_offset": 8,
				"end_offset": 17
			}
		]
	}
}

二、_analyze

1、语法

POST _analyze
{
  "analyzer": "具体的分词器",
  "text": "待分词的内容"
}

2、样例:

text的值为:https://www.b4d99.com/html/202204/45672.html

POST _analyze
{
  "analyzer": "standard",
  "text": "https://www.b4d99.com/html/202204/45672.html"
}

得到的结果:

{
    "tokens": [
        {
            "token": "https",
            "start_offset": 0,
            "end_offset": 5,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "www.b4d99",
            "start_offset": 8,
            "end_offset": 17,
            "type": "<ALPHANUM>",
            "position": 1
        },
        {
            "token": "com",
            "start_offset": 18,
            "end_offset": 21,
            "type": "<ALPHANUM>",
            "position": 2
        },
        {
            "token": "html",
            "start_offset": 22,
            "end_offset": 26,
            "type": "<ALPHANUM>",
            "position": 3
        },
        {
            "token": "202204",
            "start_offset": 27,
            "end_offset": 33,
            "type": "<NUM>",
            "position": 4
        },
        {
            "token": "45672",
            "start_offset": 34,
            "end_offset": 39,
            "type": "<NUM>",
            "position": 5
        },
        {
            "token": "html",
            "start_offset": 40,
            "end_offset": 44,
            "type": "<ALPHANUM>",
            "position": 6
        }
    ]
}

在 Spring Boot 中使用 Elasticsearch 进行多字段模糊查询,可以通过设置 `match_phrase_prefix` 查询来实现,同时关闭分词器(analyzer)。具体步骤如下: 1. 在 `application.properties` 中添加 Elasticsearch 相关配置: ``` spring.elasticsearch.rest.uris=http://localhost:9200 spring.elasticsearch.rest.username=your_username spring.elasticsearch.rest.password=your_password ``` 2. 创建一个 `QueryBuilder` 对象,设置多字段查询条件,如下所示: ```java MultiMatchQueryBuilder multiMatchQuery = QueryBuilders.multiMatchQuery(keyword, "field1", "field2", "field3"); ``` 其中,`keyword` 为查询关键字,`field1`、`field2`、`field3` 为要查询的多个字段。 3. 设置查询类型为 `match_phrase_prefix`,并关闭分词器: ```java multiMatchQuery.type(MultiMatchQueryBuilder.Type.MATCH_PHRASE_PREFIX).analyzer("keyword"); ``` 4. 使用 `RestHighLevelClient` 执行查询,并将结果返回: ```java SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder(); searchSourceBuilder.query(multiMatchQuery); SearchRequest searchRequest = new SearchRequest("index_name"); searchRequest.source(searchSourceBuilder); SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT); return Arrays.stream(searchResponse.getHits().getHits()) .map(hit -> hit.getSourceAsMap()) .collect(Collectors.toList()); ``` 其中,`restHighLevelClient` 为 Elasticsearch 客户端对象,`index_name` 为要查询索引名。 以上就是在 Spring Boot 中使用 Elasticsearch 进行多字段模糊查询的方法。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值