栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 前沿技术 > 大数据 > 大数据系统

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

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

文章目录

全文检索完全匹配高亮查询

全文检索

请求方式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": [
                        ""
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SZtEPn4BwYTyfKb78GIE",
                "_score": 1.6533571,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 7999.00
                },
                "highlight": {
                    "category": [
                        ""
                    ]
                }
            },
            {
                "_index": "shopping",
                "_type": "_doc",
                "_id": "SptFPn4BwYTyfKb7AGLp",
                "_score": 1.6533571,
                "_source": {
                    "title": "华为手机",
                    "category": "华为",
                    "images": "http://huawei.com",
                    "price": 1999.00
                },
                "highlight": {
                    "category": [
                        ""
                    ]
                }
            }
        ]
    }
}

使用 进行高亮

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/701762.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号