ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

es 7.x http 全文检索 完全匹配 高亮查询

2022-01-09 18:31:26  阅读:206  来源: 互联网

标签:category http 全文检索 华为 score images shopping es


文章目录

全文检索

请求方式get
请求url: http://127.0.0.1:9200/shopping/_search
请求体执行如下的请求:

{
    "query":{
        "match":{
            "category":"小华"
        }
    }
}

得到的查询结果如下:

{
    "took": 13,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 7,
            "relation": "eq"
        },
        "max_score": 0.8266786,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SJtEPn4BwYTyfKb722JN",
                "_score": 0.8266786,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 6999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SZtEPn4BwYTyfKb78GIE",
                "_score": 0.8266786,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 7999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SptFPn4BwYTyfKb7AGLp",
                "_score": 0.8266786,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 1999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "3",
                "_score": 0.5753642,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 1
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "ex0KPX4BPkzBbPhZJmdM",
                "_score": 0.5753642,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "fB0NPX4BPkzBbPhZNmcn",
                "_score": 0.5753642,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "2",
                "_score": 0.5753642,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "http://xiaomi.com",
                    "price": 3999.00
                }
            }
        ]
    }
}

可以看到是把所有的数据都 查询出来了.
然而category并没有"小华"
原因是因为es默认的分词器, 是把中文按照一个一个汉字进行分词,放入倒排索引中的, 小米和华为, 分别包含小和华. 在查询的时候, 把小华也是进行一个一个汉字 分词去查询的, 因此查询出来了所有的数据 .

完全匹配

完全匹配要用match_phrase , 而不是match
请求体执行如下的请求:

{
    "query":{
        "match_phrase":{
            "category":"小华"
        }
    }
}

结果如下:

{
    "took": 22,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 0,
            "relation": "eq"
        },
        "max_score": null,
        "hits": []
    }
}

只有如下完全匹配, 才能查询到数据

{
    "query":{
        "match_phrase":{
            "category":"华为"
        }
    }
}

高亮查询

使用highlight 进行高亮的结果显示.
案例

{
    "query":{
        "match_phrase":{
            "category":"华为"
        }
    },
	"highlight": {
		"fields": {
			"category": {}
		}
	}
}

category 进行高亮
结果如下

{
    "took": 119,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.6533571,
        "hits": [
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SJtEPn4BwYTyfKb722JN",
                "_score": 1.6533571,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 6999.00
                },
                "highlight": {
                    "category": [
                        "<em>华</em><em>为</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SZtEPn4BwYTyfKb78GIE",
                "_score": 1.6533571,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 7999.00
                },
                "highlight": {
                    "category": [
                        "<em>华</em><em>为</em>"
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SptFPn4BwYTyfKb7AGLp",
                "_score": 1.6533571,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 1999.00
                },
                "highlight": {
                    "category": [
                        "<em>华</em><em>为</em>"
                    ]
                }
            }
        ]
    }
}

使用<em> 进行高亮

标签:category,http,全文检索,华为,score,images,shopping,es
来源: https://blog.csdn.net/qq_33229669/article/details/122396890

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有