Elasticsearch自定义分析器,带有ngram,连字符上没有单词分隔符

时间:2017-04-16 03:19:14

标签: elasticsearch tokenize analysis analyzer

我正在尝试索引包含连字符但不包含空格,句点或任何其他标点符号的字符串。我不想基于连字符拆分单词,而是希望将连字符作为索引文本的一部分。

例如,我的6个文本字符串将是:

  • magazineplayon
  • magazineofhorses
  • 在线杂志
  • 最好杂志
  • 朋友-的-杂志
  • magazineplaygames

我希望能够在这些字符串中搜索包含&#34;播放&#34; 的文字,或者搜索以&#34;杂志开头的文字&#34; < /强>

我已经能够使用 ngram 来制作包含&#34; play&#34;好好工作。但是,连字符会导致文本分裂,并且包含结果,其中&#34; magazine&#34;在连字符后面的单词。我只想要在&#34; magazine&#34;字符串开头的单词开头。出现。

根据上面的示例,当从&#34;杂志&#34;开始时,只会出现这3个:

  • magazineplayon
  • magazineofhorses
  • magazineplaygames

请帮助我的ElasticSearch Index Sample:

DELETE /sample

PUT /sample
{
    "settings": {
        "index.number_of_shards":5,
        "index.number_of_replicas": 0,
        "analysis": {
            "filter": {
                "nGram_filter": {
                   "type": "nGram",
                   "min_gram": 2,
                   "max_gram": 20,
                   "token_chars": [
                      "letter",
                      "digit"
                   ]
                },
                "word_delimiter_filter": {
                    "type": "word_delimiter",
                    "preserve_original": true,
                    "catenate_all" : true
                }
             },
          "analyzer": {
            "ngram_index_analyzer": {
              "type" : "custom",
              "tokenizer": "lowercase",
              "filter" : ["nGram_filter", "word_delimiter_filter"]
            }
          }
        }
    }
}
PUT /sample/1/_create
{
    "name" : "magazineplayon"
}
PUT /sample/3/_create
{
    "name" : "magazineofhorses"
}
PUT /sample/4/_create
{
    "name" : "online-magazine"
}
PUT /sample/5/_create
{
    "name" : "best-magazine"
}
PUT /sample/6/_create
{
    "name" : "friend-of-magazines"
}
PUT /sample/7/_create
{
    "name" : "magazineplaygames"
}

GET /sample/_search
{
"query": {
        "wildcard": {
          "name": "*play*" 
        }
    }
}

GET /sample/_search
{
"query": {
        "wildcard": {
          "name": "magazine*" 
        }
    }
}

更新1 我更新了所有的create语句,以便在示例后使用TEST:

PUT /sample/test/7/_create
{
    "name" : "magazinefairplay"
}

然后我运行以下命令只返回带有&#34; play&#34;在他们而不是做通配符搜索。这工作正常,只返回两个记录。

POST /sample/test/_search
{
    "query": {
        "bool": {
            "minimum_should_match": 1,
            "should": [
                {"match": { "name.substrings": "play" }}
            ]
        }
    }
}

我运行以下命令只返回以&#34; magazine&#34;开头的名字。我的期望是&#34;在线杂志&#34;,&#34;最佳杂志&#34;和#34;杂志的朋友&#34;不会出现。然而,归还了所有七个记录,包括这三个记录。

POST /sample/test/_search
{
    "query": {
        "bool": {
            "minimum_should_match": 1,
            "should": [
                {"match": { "name.prefixes": "magazine" }}
            ]
        }
    }
}

有没有办法过滤掉使用连字符的前缀?

1 个答案:

答案 0 :(得分:4)

您正走在正确的道路上,但是,您还需要添加另一个利用edge-ngram token filter的分析器,以使&#34;以&#34;开头。约束工作。您可以保留ngram以检查&#34;包含&#34;一个给定的单词,但您需要edge-ngram来检查字段&#34;是否以&#34;一些令牌。

PUT /sample
{
  "settings": {
    "index.number_of_shards": 5,
    "index.number_of_replicas": 0,
    "analysis": {
      "filter": {
        "nGram_filter": {
          "type": "nGram",
          "min_gram": 2,
          "max_gram": 20,
          "token_chars": [
            "letter",
            "digit"
          ]
        },
        "edgenGram_filter": {
          "type": "edgeNGram",
          "min_gram": 2,
          "max_gram": 20
        }
      },
      "analyzer": {
        "ngram_index_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "nGram_filter"
          ]
        },
        "edge_ngram_index_analyzer": {
          "type": "custom",
          "tokenizer": "keyword",
          "filter": [
            "lowercase",
            "edgenGram_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "test": {
      "properties": {
        "name": {
          "type": "string",
          "fields": {
            "prefixes": {
              "type": "string",
              "analyzer": "edge_ngram_index_analyzer",
              "search_analyzer": "standard"
            },
            "substrings": {
              "type": "string",
              "analyzer": "ngram_index_analyzer",
              "search_analyzer": "standard"
            }
          }
        }
      }
    }
  }
}

然后您的查询将成为(即搜索name字段包含play或以magazine开头的所有文档)

POST /sample/test/_search
{
    "query": {
        "bool": {
            "minimum_should_match": 1,
            "should": [
                {"match": { "name.substrings": "play" }},
                {"match": { "name.prefixes": "magazine" }}
            ]
        }
    }
}

注意:请勿使用wildcard搜索子字符串,因为它会影响群集的效果(更多信息herehere