首页 > 解决方案 > 定义了一个edgeNGram,搜索只对完整的单词起作用,而不是tokens

问题描述

我使用 es 6.4 作为 AWS 服务。这是我的映射 -

{
  "settings": {
    "analysis": {
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "my_tokenizer"
        }
      }
    },
    "tokenizer": {
      "my_tokenizer": {
        "type": "edge_ngram",
        "min_gram": 3,
        "max_gram": 20,
        "token_chars": [
          "letter"
        ]
      }
    }
  },
  "mappings": {
    "tsetse": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "user_id": {
          "type": "integer"
        },
        "description": {
          "type": "text",
          "analyzer": "my_analyzer"
        },
        "type": {
          "type": "integer"
        }
      }
    }
  }
}

该指数有一个描述=“地球上最伟大的表现”的记录。当我尝试搜索时,它总是适用于完整的词 - 地球或性能。不返回出色或性能的结果。我错过了什么?

这是使用 EdgeNGram 更新的映射`

{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "tsetse": {
      "properties": {
        "id": {
          "type": "integer"
        },
        "user_id": {
          "type": "integer"
        },
        "description": {
          "type": "text",
          "analyzer": "my_analyzer"
        },
        "type": {
          "type": "integer"
        }
      }
    }
  }
}

`

Gist 脚本 - https://gist.github.com/swati-patil/0b1cea74fc52b1b96d44ad239ad2580d 谢谢,

标签: elasticsearch

解决方案


感谢您的要点。我可以看到您没有正确创建索引:

  1. 您正在使用 POST 而不是 PUT
  2. 你指定了一个你不应该指定的类型
  3. 最后需要删除两个闭合花括号

改为这样做:

# first delete your index
curl -XDELETE 'my-instance-us-east1.amazonaws.com/my_index'

# then create it correctly
curl -XPUT "my-instance-us-east1.amazonaws.com/my_index" -H 'Content-Type: application/json' -d '{
  "settings": {
    "analysis": {
      "filter": {
        "autocomplete_filter": {
          "type": "edge_ngram",
          "min_gram": 1,
          "max_gram": 20
        }
      },
      "analyzer": {
        "my_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "autocomplete_filter"
          ]
        }
      }
    }
  },
  "mappings": {
    "my_type": {
      "properties": {
        "text": {
          "type": "text",
          "analyzer": "my_analyzer"
        }
      }
    }
  }
}'

# then analyze works
curl -XPOST my-instance-us-east1.amazonaws.com/my_index/_analyze -H 'Content-Type: application/json' -d '{
  "analyzer": "my_analyzer",
  "text": "Greatest performance on earth"
}'

然后索引您的文档并运行您的查询,它们都会起作用。


推荐阅读