【问题标题】:Partial Search in elasticsearch is working for one and not for other recordelasticsearch中的部分搜索适用于一个记录,而不适用于其他记录
【发布时间】:2021-07-23 07:00:30
【问题描述】:

弹性搜索是使用以下主体创建的

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"
        }
    }

现在弹性搜索中有 2 个不同的元数据

{'_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',
  '_type': '_doc',
  '_id': '3929829.pdf',
  '_version': 1,
  '_seq_no': 538517,
  '_primary_term': 3, 
  'found': True, '_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}}

现在在弹性搜索中使用以下查询搜索第一条记录时

  {
  "query":{
  "bool":{
     "must":{
        "match":{
           "TokenizedDocumentFileName":{
              "query":"997439"
           }
        }
     },
     "filter":{
        "bool":{
           "must":[
              {
                 "term":{
                    "ProjectID":8
                 }
              },
              {
                 "term":{
                    "Extension":"pdf"
                 }
              }
           ]
        }
     }
  }
  }
  }

查询以搜索第二条记录

  {
  "query":{
  "bool":{
     "must":{
        "match":{
           "TokenizedDocumentFileName":{
              "query":"marynewfiletest"
           }
        }
     },
     "filter":{
        "bool":{
           "must":[
              {
                 "term":{
                    "ProjectID":8
                 }
              },
              {
                 "term":{
                    "Extension":"pdf"
                 }
              }
           ]
        }
     }
  }
  }
  }

第一个查询给了我正确的结果,因为 TokenizedDocumentFileName 中存在查询“997439”,但是当我在 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 文件单元手册的输出

{
"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",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "FileID" : {
      "type" : "long"
    },
    "IndexKey" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "ignore_above" : 256
        }
      }
    },
    "ProjectID" : {
      "type" : "long"
    },
    "TokenizedDocumentFileName" : {
      "type" : "text",
      "fields" : {
        "keyword" : {
          "type" : "keyword",
          "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"
    }
  }
}
}
}

所以问题是为什么部分搜索适用于第一个而不是第二个。

【问题讨论】:

  • 我可以复制你的索引,然后我得到第二个查询的预期结果......不知道为什么它不适合你
  • @Val ,知道我应该在哪里查看代码,开始调试它
  • 你能分享你在运行GET fileboundunitmanuals 时得到了什么吗?我怀疑您的实际映射与您认为已安装的映射不同
  • @Val,添加了 GET fileboundunitmanuals 的输出

标签: elasticsearch elasticsearch-query elasticsearch-analyzers


【解决方案1】:

根据您的映射,字段TokenizedDocumentFileName 只是textkeyword,因此它没有您的分析器。因此,您的第一个查询有效只是巧合。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多