【问题标题】:Elasticsearch Edge-NGrams Prefer Shorter TermsElasticsearch Edge-NGrams 更喜欢较短的术语
【发布时间】:2018-09-06 10:50:19
【问题描述】:

我喜欢使用 Edge-NGrams 从 Elasticsearch 获得的结果来索引数据和用于搜索的不同分析器。但是,我更喜欢匹配的较短术语比较长术语排名更高。

例如,使用术语ABC100ABC100xxx。如果我使用术语ABC 执行查询,我会以相同分数返回这两个文档。我希望ABC100 的得分高于ABC100xxx,因为ABC 更接近ABC100 根据Levenshtein distance algorithm 之类的东西。


设置索引:

PUT stackoverflow
{
    "settings": {
        "index": {
            "number_of_replicas": 0,
            "number_of_shards": 1
        },
        "analysis": {  
            "filter": {
                "edge_ngram": {
                    "type": "edgeNGram",
                    "min_gram": "1",
                    "max_gram": "20"
                }
            },

            "analyzer": {
              "my_analyzer": {
                "type": "custom",
                "tokenizer": "whitespace",
                "filter": [
                  "edge_ngram"
                ]
              }
            }
        }
    },

    "mappings": {
        "doc": {
            "properties": {
                "product": {
                  "type": "text",
                  "analyzer": "my_analyzer",
                  "search_analyzer": "whitespace"
                }
            }
        }
    }
}

插入文档:

PUT stackoverflow/doc/1
{
    "product": "ABC100"
}

PUT stackoverflow/doc/2
{
    "product": "ABC100xxx"
}

搜索查询:

GET stackoverflow/_search?pretty
{
  "query": {
    "match": {
      "product": "ABC"
    }
  }
}

结果:

{
  "took": 1,
  "timed_out": false,
  "_shards": {
    "total": 1,
    "successful": 1,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 0.28247002,
    "hits": [
      {
        "_index": "stackoverflow",
        "_type": "doc",
        "_id": "2",
        "_score": 0.28247002,
        "_source": {
          "product": "ABC100xxx"
        }
      },
      {
        "_index": "stackoverflow",
        "_type": "doc",
        "_id": "1",
        "_score": 0.28247002,
        "_source": {
          "product": "ABC100"
        }
      }
    ]
  }
}

有谁知道我如何将ABC100 等更短的词排在ABC100xxx 之上?

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    在找到大量关于将字段长度存储为字段或使用脚本查询的less than optimal solutions 之后,我找到了the root of my problem。这只是因为我使用的是 edge_ngrams 标记过滤器而不是 edge_ngrams 标记器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-05-05
      • 1970-01-01
      • 2015-04-03
      • 1970-01-01
      • 2015-09-19
      • 2023-03-08
      • 2016-09-14
      • 2014-04-13
      相关资源
      最近更新 更多