为什么使用MinHash分析器的查询无法检索重复项?

2024-03-28 15:46:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用Elasticsearch索引的MinHash implementation查询近似重复的索引。 我使用在容器中运行的Python客户端来索引和执行搜索

我的语料库是一个JSONL文件,有点像这样:

{"id":1, "text":"I'd just like to interject for a moment"}
{"id":2, "text":"I come up here for perception and clarity"}
...

我成功地创建了Elasticsearch索引,尝试使用自定义设置和分析器,并从official examplesMinHash docs中获得灵感:

def create_index(client):
    client.indices.create(
        index="documents",
        body={
            "settings": {
                "analysis": {
                    "filter": {
                        "my_shingle_filter": {      
                        "type": "shingle",
                        "min_shingle_size": 5,
                        "max_shingle_size": 5,
                        "output_unigrams": False
                        },
                        "my_minhash_filter": {
                        "type": "min_hash",
                        "hash_count": 10,          
                        "bucket_count": 512,      
                        "hash_set_size": 1,       
                        "with_rotation": True     
                        }
                    },
                    "analyzer": {
                        "my_analyzer": {
                        "tokenizer": "standard",
                        "filter": [
                            "my_shingle_filter",
                            "my_minhash_filter"
                        ]
                        }
                    }
                }
            },
            "mappings": {
                "properties": {
                    "name": {"type": "text", "analyzer": "my_analyzer"}
                }
            },
        },
        ignore=400,
    )

我通过Kibana和访问http://localhost:9200/documents/_settings验证了索引创建没有大问题,我得到了一些看起来有序的东西:

enter image description here

但是,使用以下命令查询索引:

def get_duplicate_documents(body, K, es):
    doc = {
        '_source': ['_id', 'body'],
        'size': K,
        'query': {
            "match": {
                "body": {
                    "query": body,
                    "analyzer" : "my_analyzer"
                }
            }
        }
    }

    res = es.search(index='documents', body=doc)
    top_matches = [hit['_source']['_id'] for hit in res['hits']['hits']]

我的res['hits']始终为空,即使我将我的body设置为与语料库中某个条目的文本完全匹配。换句话说,如果我尝试作为body的值,我不会得到任何结果

"I come up here for perception and clarity"

或者像这样的子字符串

"I come up here for perception"

理想情况下,我希望该过程返回近似重复项,分数是通过MinHash获得的查询和近似重复项的Jaccard相似性的近似值

我的查询和/或索引Elasticsearch的方式是否有问题?我完全错过了什么吗

附言:你可以看一下https://github.com/davidefiocco/dockerized-elasticsearch-duplicate-finder/tree/ea0974363b945bf5f85d52a781463fba76f4f987中的一个非功能性但希望是可重复的例子(我也会在找到解决方案后更新回购协议!)


Tags: textidforsizeheremybodyfilter
1条回答
网友
1楼 · 发布于 2024-03-28 15:46:55

以下是一些您应该仔细检查的事项,因为它们可能是罪魁祸首:

  • 创建映射时,应在body参数内的client.indices.create方法中将“name”更改为“text”,因为json文档有一个名为text的字段:

      "mappings": {
          "properties": {
              "text": {"type": "text", "analyzer": "my_analyzer"}
          }
    
  • 在索引阶段,您还可以在the documentation之后使用以下内容重新编写generate_actions()方法:

    for elem in corpus:
      yield {
          "_op_type": "index"
          "_index": "documents",
          "_id": elem["id"],
          "_source": elem["text"]
      }
    

    顺便说一句,如果您正在索引pandas数据帧,那么您可能需要检查实验性的官方库eland

  • 此外,根据您的映射,您正在使用minhash标记过滤器,因此Lucene将在哈希中的text字段内转换文本。因此,您可以使用散列而不是字符串来查询此字段,就像您在示例"I come up here for perception and clarity"中所做的那样。 因此,使用它的最佳方法是检索字段text的内容,然后在Elasticsearch中查询检索到的相同值。那么_id元字段不在_source元字段内,因此您应该在以下位置更改get_duplicate_documents()方法:

    def get_duplicate_documents(body, K, es):
      doc = {
          '_source': ['text'],
          'size': K,
          'query': {
              "match": { 
                  "text": { # I changed this line!
                      "query": body
                  }
              }
          }
      }
    
      res = es.search(index='documents', body=doc)
      # also changed the list comprehension!
      top_matches = [(hit['_id'], hit['_source']) for hit in res['hits']['hits']]
    

相关问题 更多 >