我试图建立一个查询,在那里我使用精确的短语匹配和同义词,我不能弄清楚。此外,当使用通配符方法时,我不知道如何使用模糊性。通配符有可能吗?"职责召唤"、"编码"等词如果能得到同样的结果,那就太好了。或者"dutz的召唤">
我创建了这个索引:
PUT exact_search
{
"settings": {
"index": {
"number_of_shards": "1",
"number_of_replicas": "0",
"analysis": {
"analyzer": {
"analyzer_exact": {
"type": "custom",
"tokenizer": "keyword",
"filter": [
"lowercase",
"icu_folding",
"synonyms"
]
}
},
"filter": {
"synonyms": {
"type": "synonym",
"synonyms_path": "synonyms.txt"
}
}
}
}
},
"mappings": {
"properties": {
"name": {
"type": "keyword",
"fields": {
"analyzer_exact": {
"type": "text",
"analyzer": "analyzer_exact"
}
}
}
}
}
}
我用这些条目填充它:
POST exact_search/_doc/1
{
"name": "Hoodie Call of Duty"
}
POST exact_search/_doc/2
{
"name": "Call of Duty 2"
}
POST exact_search/_doc/3
{
"name": "Call of Duty: Modern Warfare 2"
}
POST exact_search/_doc/4
{
"name": "COD: Modern Warfare 2"
}
POST exact_search/_doc/5
{
"name": "Call of duty"
}
POST exact_search/_doc/6
{
"name": "Call of the sea"
}
POST exact_search/_doc/7
{
"name": "Heavy Duty"
}
synonys .txt看起来像这样:
cod,call of duty
我想要达到的是,当我搜索"职责召唤"时,得到所有的结果(除了海上召唤和重型任务);或"cod".
到目前为止,我构造了这个查询,但是当使用"cod"时,它不像预期的那样工作。搜索词(词"职责召唤";工作正常):GET exact_search/_search
{
"explain": false,
"query":{
"bool":{
"must":[
{
"wildcard": {
"name.analyzer_exact": {
"value": "*cod*"
}
}
}
]
}
}
}
但是结果只有两个条目:
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "exact_search",
"_id" : "4",
"_score" : 1.0,
"_source" : {
"name" : "COD: Modern Warfare 2"
}
},
{
"_index" : "exact_search",
"_id" : "5",
"_score" : 1.0,
"_source" : {
"name" : "Call of duty"
}
}
]
}
}
看起来同义词起作用了,因为它返回"call of duty";例如,它不会返回《使命召唤2》。
我需要寻找确切的短语匹配,因为我不想得到的结果重型任务或海洋的召唤(当单词" Call "one_answers";duty"比赛)。
谢谢你给我指正确的方向。
我怀疑分析器是否会生成与analyzer_exact "tokenizer" "关键字"同义的标记。我将改变一些东西使它工作。
-
关键字→标准
"analyzer_exact": { "type": "custom", "tokenizer": "standard", "filter": [ "lowercase", "synonyms" ] }
-
我会使用匹配短语来消除除了使命召唤和上帝以外的名字。
{ "match_phrase": { "name.analyzer_exact": "cod" } }
变化后的反应
{
"hits": {
"hits": [
{
"_source": {
"name": "Call of duty"
}
},
{
"_source": {
"name": "COD: Modern Warfare 2"
}
},
{
"_source": {
"name": "Call of Duty 2"
}
},
{
"_source": {
"name": "hoddies Call of Duty"
}
},
{
"_source": {
"name": "Call of Duty: Modern Warfare 2"
}
}
]
}