elasticsearch ngram和postgresql trigram搜索结果不匹配

时间:2017-07-17 11:53:38

标签: postgresql elasticsearch similarity n-gram sentence-similarity

我在 elasticsearch 上创建了一个与下面相同的索引:

"settings" : {
    "number_of_shards": 1,
    "number_of_replicas": 0,
    "analysis": {
                "filter": {
                    "trigrams_filter": {
                        "type":     "ngram",
                        "min_gram": 3,
                        "max_gram": 3
                    }
                },
                "analyzer": {
                    "trigrams": {
                        "type":      "custom",
                        "tokenizer": "standard",
                        "filter":   [
                            "lowercase",
                            "trigrams_filter"
                        ]
                    }
                }
    }
},
"mappings": {
    "issue": {
        "properties": {
            "description": {
                "type":     "string",
                "analyzer": "trigrams"
            }
        }
    }
}

我的测试项目如下:

"alici onay verdi basarili satisiniz gerceklesti diyor ama hesabima para transferi gerceklesmemis"

"otomatik onay işlemi gecikmiş"

"************* nolu iade islemi urun kargoya verilmedi zamaninda iade islemlerinde urun erorr hata veriyor"

我用bellow查询测试了这个索引:

GET issue/_search
{
  "query": {
      "match": {
            "description":{
                 "query": "otomatik onay istemi zamaninda gerceklesmemis"
            }
       }
   }
}

结果:

{
      ....
      "hits": {
            ....
                "max_score": 2.3507352,
                "hits": [
                          {
                              ....                                   
                              "_score": 2.3507352,
                              "_source": {
                                   "issue_id": "*******",
                                   "description": "alici onay verdi basarili satisiniz gerceklesti diyor ama hesabima para transferi gerceklesmemis"
                                          }
                           }
                        ]
                }
 }

postgresql 上的相同数据与下面的SQL响应有另一个结果:

SELECT 
     public.tbl_issue_descriptions_big.description,
     similarity(description, 'otomatik onay islemi zamaninda gerceklesmemis') AS sml
FROM
     public.tbl_issue_descriptions_big
WHERE
     description %'otomatik onay islemi zamaninda gerceklesmemis'
ORDER BY
     sml DESC
LIMIT 10

结果是:

description                                           | sml
======================================================|======
otomatik onay islemi gecikmis                         |0,351852

为什么会造成这种差异?

1 个答案:

答案 0 :(得分:1)

我不太了解postgres在那里给出一个合格的答案(因为这也取决于索引的文档,如果他们的评分公式完全相同,我怀疑),但Elasticsearch有一个explain API并且搜索中有explain parameter,可帮助您找出某个文档以这种方式评分的原因。