elasticsearch 中的部分搜索适用于一个记录而不适用于其他记录

如何解决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 只是 textkeyword,因此它没有您的分析器。因此,您的第一个查询有效只是巧合。

在索引第一个文档之前,您应该确保使用正确的映射正确创建索引。

PS:我能够使用您提供的设置/映射创建您的索引,并且我获得了两个查询的预期结果,所以您就快完成了。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?