gpt4 book ai didi

elasticsearch - 如何在Elasticsearch中执行完全匹配和部分匹配?

转载 作者:行者123 更新时间:2023-12-03 01:41:51 25 4
gpt4 key购买 nike

这是json:

{
"took": 3,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 2,
"max_score": 1,
"hits": [
{
"_index": "testing",
"_type": "skills",
"_id": "AV9FMnRfkEZ90S4dhzF6",
"_score": 1,
"_source": {
"skill": "java"
}
}
,
{
"_index": "testing",
"_type": "skills",
"_id": "AV9FM777kEZ90S4dhzF7",
"_score": 1,
"_source": {
"skill": "c language"
}
}
]
}
}

我有两个方面,我需要获得部分和确切的匹配技能。

部分匹配:

假设我给“c”,那么我应该获得结果“c语言”的技能。

输入:
c-> c语言

完全符合:

假设我给“java”,那么我应该得到结果“java”技能。

输入:
java-> Java

最佳答案

试试这个。
通过以下方式定义映射:

PUT index
{
"mappings": {
"type": {
"properties": {
"skill": {
"type": "string",
"index" : "analyzed",
"fields": {
"keyword": {
"type": "string",
"index" : "not_analyzed"
}
}
}
}
}
}
}

相当于上述命令的CURL:
curl -XPUT localhost:9200/index -d '
{
"mappings": {
"type": {
"properties": {
"skill": {
"type": "string",
"index" : "analyzed",
"fields": {
"keyword": {
"type": "string",
"index" : "not_analyzed"
}
}
}
}
}
}
}'

添加文件:
POST index/type
{
"skill":"c language"
}

POST index/type
{
"skill":"java"
}

相当于上述命令的CURL:
curl -XPOST localhost:9200/index/type -d '
{
"skill":"c language"
}'

curl -XPOST localhost:9200/index/type -d '
{
"skill":"java"
}'

搜索您的文档:

部分匹配:
GET index/_search
{
"query": {
"match": {
"skill": "c"
}
}
}

完全符合:
GET index/_search
{
"query": {
"term": {
"skill.keyword": "java"
}
}
}

相当于上述命令的CURL:
curl -XGET localhost:9200/index/_search -d '
{
"query": {
"match": {
"skill": "c"
}
}
}'


curl -XGET localhost:9200/index/_search -d '
{
"query": {
"term": {
"skill.keyword": "java"
}
}
}'

关于elasticsearch - 如何在Elasticsearch中执行完全匹配和部分匹配?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46877425/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com