Sankt和St

时间:2018-03-29 09:14:17

标签: elasticsearch elasticsearch-5

我正在尝试让同义词适用于我现有的设置。目前我有这样的设置:

PUT city
{
    "settings": {
        "analysis": {
            "analyzer": {
                "autocomplete": {
                    "tokenizer": "autocomplete",
                    "filter": [
                        "lowercase",
                        "my_synonym_filter",
                        "german_normalization",
                        "my_ascii_folding"
                    ]
                },
                "autocomplete_search": {
                    "tokenizer": "lowercase",
                    "filter": [
                        "lowercase",
                        "my_synonym_filter",
                        "german_normalization",
                        "my_ascii_folding"
                    ]
                }
            },
                  "filter": {
                     "my_ascii_folding": {
                     "type": "asciifolding",
                     "preserve_original": true
            },
                  "my_synonym_filter": {
                  "type": "synonym",
                  "ignore_case": "true",
                  "synonyms": [
                     "sankt, st => sankt"
                  ]
            }
          },
            "tokenizer": {
                "autocomplete": {
                    "type": "edge_ngram",
                    "min_gram": 1,
                    "max_gram": 15,
                    "token_chars": [
                        "letter",
                        "digit",
                        "symbol"
                    ]
                }
            }
        }
    },
    "mappings": {
        "city": {
            "properties": {
                "name": {
                    "type": "text",
                    "analyzer": "autocomplete",
                    "search_analyzer": "autocomplete_search"
                }
            }
        }
    }
}

在这个City索引中,我有这样的文档:

St. WolfgangSankt Wolfgang等等。对我而言St.Sankt是同义词。因此,如果我搜索Sankt,则应显示两个文档。

我创建了一个新过滤器并将过滤器添加到我的autocomplete analyzer

"my_synonym_filter": {
   "type": "synonym",
    "ignore_case": "true",
    "synonyms": [
        "sankt, st."
    ]
} 

现在很好。但我面临的问题是:

很明显st之后的点没有被分析,而且目前无法搜索。但对于同义词来说,点很重要。

第二个问题是,如果我搜索sankt同义词是st,它会向我提供所有以Stuttgart开头的文档。所以这也是因为没有使用点。

你知道我怎么能实现这些目标吗?如果您需要更多信息,请告诉我。

更新

经过讨论后,我在我的设置中进行了这项更改:

edge_ngram标记生成器更改为standard标记生成器。

添加了edgeNGram过滤器,并将此过滤器添加到我的分析器中。

从我的分析器中删除过滤器german_normalizationmy_ascii_folding以简化测试。

PUT city
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "autocomplete",
          "filter": [
            "lowercase",
            "my_synonym_filter",
            "edge_filter"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "autocomplete",
          "filter": [
            "my_synonym_filter",
            "lowercase"
          ]
        }
      },
      "filter": {
        "edge_filter": {
          "type": "edgeNGram",
          "min_gram": 1,
          "max_gram": 15
        },
        "my_synonym_filter": {
          "type": "synonym",
          "ignore_case": "true",
          "synonyms": [
            "sankt, st => sankt"
          ]
        }
      },
      "tokenizer": {
        "autocomplete": {
          "type": "standard"
        }
      }
    }
  },
  "mappings": {
    "city": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}

我将这3个文件添加到索引中:

"name":"Sankt Wolfgang",
"name":"Stuttgart",
"name":"St. Wolfgang"

查询字符串 - 结果

st      ->    "St. Wolfgang", "Stuttgart"
st.     ->    "St. Wolfgang", "Sankt Wolfgang"
sankt   ->    "St. Wolfgang", "Sankt Wolfgang"

1 个答案:

答案 0 :(得分:1)

这对我来说效果很好。这里的要点是确保

  • 将同义词过滤器放在小写的
  • 之后
  • 将edge-n-gram过滤器放在最后
  • 仅在索引时使用edge-n-gram

所以我们创建索引:

PUT city
{
  "settings": {
    "analysis": {
      "analyzer": {
        "autocomplete": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonym_filter",
            "edge_filter"
          ]
        },
        "autocomplete_search": {
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "my_synonym_filter"
          ]
        }
      },
      "filter": {
        "edge_filter": {
          "type": "edgeNGram",
          "min_gram": 1,
          "max_gram": 15
        },
        "my_synonym_filter": {
          "type": "synonym",
          "ignore_case": "true",
          "synonyms": [
            "sankt, st. => sankt"
          ]
        }
      }
    }
  },
  "mappings": {
    "city": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "autocomplete",
          "search_analyzer": "autocomplete_search"
        }
      }
    }
  }
}

然后我们索引数据:

PUT city/city/1
{
  "name":"St. Wolfgang"
}
PUT city/city/2
{
  "name":"Stuttgart"
}
PUT city/city/3
{
  "name":"Sankt Wolfgang"
}

最后搜索stsankt只返回文档1和3但不返回2

POST city/_search?q=name:st
POST city/_search?q=name:sankt