Node.js 弹性搜索多匹配查询可以';不要忽略特殊字符
我有一个名称字段值为“abc_name”,所以当我搜索“abc_”时,我会得到正确的结果,但当我搜索“abc_@#&-#&”时,我仍然会得到相同的结果。我希望我的查询忽略与我的查询不匹配的特殊字符 我的问题是:Node.js 弹性搜索多匹配查询可以';不要忽略特殊字符,node.js,elasticsearch,graphql,Node.js,elasticsearch,Graphql,我有一个名称字段值为“abc_name”,所以当我搜索“abc_”时,我会得到正确的结果,但当我搜索“abc_@#&-#&”时,我仍然会得到相同的结果。我希望我的查询忽略与我的查询不匹配的特殊字符 我的问题是: 多重匹配 键入交叉字段 操作员和 我正在为我的字段使用search_analyzer标准 我想要这个结构,否则它会影响我的其他搜索行为 "name": { "type": "text",
- 多重匹配
- 键入交叉字段
- 操作员和
- 我正在为我的字段使用search_analyzer标准
"name": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
},
"analyzer": "autocomplete",
"search_analyzer": "standard"
}
请参阅以下适合您的用例的示例,我创建了一个适合您的用例的示例: 样本映射: 生成的令牌:
{
"tokens" : [
{
"token" : "abc_name",
"start_offset" : 0,
"end_offset" : 8,
"type" : "word",
"position" : 0
},
{
"token" : "asda",
"start_offset" : 9,
"end_offset" : 13,
"type" : "word",
"position" : 1
},
{
"token" : "1213_adav",
"start_offset" : 25,
"end_offset" : 34,
"type" : "word",
"position" : 2
}
]
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "abc_"
}
}
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "efg_!@#!@#"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47992462,
"hits" : [
{
"_index" : "some_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.47992462,
"_source" : {
"my_field" : "abc_name asda efg_!@#!@# 1213_adav"
}
}
]
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
请注意,令牌efg.@#@#代码>已被删除
我添加了edge\u ngram fitler,因为如果您使用abc\u
进行搜索,并且通过标记器生成的标记是abc\u name
,则您希望搜索成功
样本文件:
查询请求:
用例1:
{
"tokens" : [
{
"token" : "abc_name",
"start_offset" : 0,
"end_offset" : 8,
"type" : "word",
"position" : 0
},
{
"token" : "asda",
"start_offset" : 9,
"end_offset" : 13,
"type" : "word",
"position" : 1
},
{
"token" : "1213_adav",
"start_offset" : 25,
"end_offset" : 34,
"type" : "word",
"position" : 2
}
]
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "abc_"
}
}
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "efg_!@#!@#"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47992462,
"hits" : [
{
"_index" : "some_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.47992462,
"_source" : {
"my_field" : "abc_name asda efg_!@#!@# 1213_adav"
}
}
]
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
用例2:
{
"tokens" : [
{
"token" : "abc_name",
"start_offset" : 0,
"end_offset" : 8,
"type" : "word",
"position" : 0
},
{
"token" : "asda",
"start_offset" : 9,
"end_offset" : 13,
"type" : "word",
"position" : 1
},
{
"token" : "1213_adav",
"start_offset" : 25,
"end_offset" : 34,
"type" : "word",
"position" : 2
}
]
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "abc_"
}
}
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "efg_!@#!@#"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47992462,
"hits" : [
{
"_index" : "some_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.47992462,
"_source" : {
"my_field" : "abc_name asda efg_!@#!@# 1213_adav"
}
}
]
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
响应:
用例1的响应:
{
"tokens" : [
{
"token" : "abc_name",
"start_offset" : 0,
"end_offset" : 8,
"type" : "word",
"position" : 0
},
{
"token" : "asda",
"start_offset" : 9,
"end_offset" : 13,
"type" : "word",
"position" : 1
},
{
"token" : "1213_adav",
"start_offset" : 25,
"end_offset" : 34,
"type" : "word",
"position" : 2
}
]
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "abc_"
}
}
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "efg_!@#!@#"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47992462,
"hits" : [
{
"_index" : "some_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.47992462,
"_source" : {
"my_field" : "abc_name asda efg_!@#!@# 1213_adav"
}
}
]
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
用例2的响应:
{
"tokens" : [
{
"token" : "abc_name",
"start_offset" : 0,
"end_offset" : 8,
"type" : "word",
"position" : 0
},
{
"token" : "asda",
"start_offset" : 9,
"end_offset" : 13,
"type" : "word",
"position" : 1
},
{
"token" : "1213_adav",
"start_offset" : 25,
"end_offset" : 34,
"type" : "word",
"position" : 2
}
]
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "abc_"
}
}
}
POST some_test_index/_search
{
"query": {
"match": {
"my_field": "efg_!@#!@#"
}
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.47992462,
"hits" : [
{
"_index" : "some_test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 0.47992462,
"_source" : {
"my_field" : "abc_name asda efg_!@#!@# 1213_adav"
}
}
]
}
}
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
最新答复:
根据我创建的索引按如下方式创建映射,并告知是否有效:
PUT some_test_index
{
"settings": {
"analysis": {
"analyzer": {
"my_custom_analyzer": {
"type": "custom",
"tokenizer": "punctuation",
"filter": ["lowercase"]
}
},
"tokenizer": {
"punctuation": {
"type": "pattern",
"pattern": "\\w+_+[^a-zA-Z\\d\\s_]+|\\s+"
}
}
}
},
"mappings": {
"properties": {
"my_field":{
"type": "text",
"analyzer": "autocompete", <----- Assuming you have already this in setting
"search_analyzer": "my_custom_analyzer". <----- Note this
}
}
}
}
放置一些测试索引
{
“设置”:{
“分析”:{
“分析器”:{
“我的自定义分析器”:{
“类型”:“自定义”,
“标记器”:“标点符号”,
“过滤器”:[“小写”]
}
},
“标记器”:{
“标点符号”:{
“类型”:“模式”,
“图案”:“\\w++[^a-zA-Z\\d\\s\++\\s+”
}
}
}
},
“映射”:{
“财产”:{
“我的领域”:{
“类型”:“文本”,
“analyzer”:“autocompete”,我希望我的查询忽略与我的查询不匹配的特殊字符,这是什么意思?当一些其他特殊字符(字段值中包含的字符除外)出现时,您是否希望搜索结果为空搜索查询中是否包含?是的。我希望此搜索结果为空。你能回答吗?我正在处理你的问题。你至少可以给我一些输入,但我正在使用analyzer autcomplete,因此我可以在任何字段上使用两个分析器。嘿@abhijeetSnap你能用你的映射更新问题吗?不,你不能使用两个分析器在字段上。您可以在搜索过程中使用不同的分析器,即通过search\u analyzer
,但在搜索或索引过程中,您不能在字段上指定两个分析器。请检查映射我已在上面更新,但我无法删除自动完成。我遵循了上面提到的所有步骤,还替换了analyzer autocompletee到我的_自定义_分析器,但仍然能够通过多个特殊的characters@AbhijeetSanap我已经更新了答案,但看到了您更新的评论,好吧,您正在尝试多种方法。首先,只在搜索\u analyzer
中进行更改,然后查看analyzer是否适用于您拥有的自动匹配
。如果可以的话不行,请执行此操作并在问题中发布详细信息。GET/\u设置