如何解决elasticsearch 中的部分搜索适用于一个记录而不适用于其他记录
弹性搜索由以下主体创建
body = {
"mappings": {
"properties": {
"TokenizedDocumentFileName": {
"type": "text","analyzer": "my_analyzer","search_analyzer": "standard"
}
}
},"settings": {
"analysis": {
"analyzer": {
"my_analyzer": {
"tokenizer": "keyword","filter": ["word_delimiter","lowercase"
]
}
}
},"number_of_shards": "1","number_of_replicas": "0"
}
}
现在elasticsearch中有两种不同的元数据
{'_index': 'fileboundunitmanuals','_type': '_doc','_id': '997439.PDF','_version': 2,'_seq_no': 166958,'_primary_term': 1,'found': True,'_source': {
'IndexKey': '997439.PDF','DocumentID': 997439,'Extension': 'PDF','FileID': 174508,'DocumentFileName': '\\UNIT xxxxx\\411xxx\\A9.xxxx_xxxxx GAS ENGINE xxxxx_x_997439.PDF','TokenizedDocumentFileName': '\\UNIT xxxxx\\411xxx\\A9. xxxx xxxxx GAS ENGINE xxxxx x 997439.PDF','F1': 'UNIT xxxxx','ProjectID': 8}}
第二条记录
{'_index': 'fileboundunitmanuals','_id': '3929829.pdf','_version': 1,'_seq_no': 538517,'_primary_term': 3,'_source': {
'Extension': 'pdf','DocumentID': 3929829,'IndexKey': '3929829.pdf','FileID': '','DocumentFileName': '\\Unit xxxxx\\Mary Testing\\marynewfiletest.pdf','TokenizedDocumentFileName': '\\Unit xxxxx\\Mary Testing\\marynewfiletest.pdf','F1': 'Unit xxxxx','ProjectID': 8}}
现在当使用以下查询第一条记录在elasticsearch中搜索时
{
"query":{
"bool":{
"must":{
"match":{
"TokenizedDocumentFileName":{
"query":"997439"
}
}
},"filter":{
"bool":{
"must":[
{
"term":{
"ProjectID":8
}
},{
"term":{
"Extension":"pdf"
}
}
]
}
}
}
}
}
{
"query":{
"bool":{
"must":{
"match":{
"TokenizedDocumentFileName":{
"query":"marynewfiletest"
}
}
},{
"term":{
"Extension":"pdf"
}
}
]
}
}
}
}
}
第一个查询给了我正确的结果,因为查询“997439”存在于 TokenizedDocumentFileName 中,但是当我搜索 marytesting 的 2 条记录时,我得到了以下响应。
{'took': 0,'timed_out': False,'_shards': {'total': 1,'successful': 1,'skipped': 0,'Failed': 0},'hits': {'total': {'value': 0,'relation': 'eq'},'max_score': None,'hits': []}}
但是当我提供文件名和扩展名时,即“marytesting.pdf”,在这种情况下,我得到了正确的结果。
GET fileunitmanuals 的输出
{
"fileboundunitmanuals" : {
"aliases" : { },"mappings" : {
"properties" : {
"DocumentFileName" : {
"type" : "text","analyzer" : "my_analyzer","search_analyzer" : "standard"
},"DocumentID" : {
"type" : "long"
},"Extension" : {
"type" : "text","fields" : {
"keyword" : {
"type" : "keyword","ignore_above" : 256
}
}
},"F1" : {
"type" : "text","FileID" : {
"type" : "long"
},"IndexKey" : {
"type" : "text","ProjectID" : {
"type" : "long"
},"TokenizedDocumentFileName" : {
"type" : "text","ignore_above" : 256
}
}
}
}
},"settings" : {
"index" : {
"number_of_shards" : "1","provided_name" : "fileboundunitmanuals","creation_date" : "1607069298331","analysis" : {
"analyzer" : {
"my_analyzer" : {
"filter" : [
"word_delimiter","lowercase"
],"tokenizer" : "keyword"
}
}
},"number_of_replicas" : "0","uuid" : "u8HasYfVT6iMr7XGpdjJHg","version" : {
"created" : "7090199"
}
}
}
}
}
解决方法
根据您的映射,字段 TokenizedDocumentFileName
只是 text
和 keyword
,因此它没有您的分析器。因此,您的第一个查询有效只是巧合。
在索引第一个文档之前,您应该确保使用正确的映射正确创建索引。
PS:我能够使用您提供的设置/映射创建您的索引,并且我获得了两个查询的预期结果,所以您就快完成了。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。