elasticsearch Elasticsearch:使用关键字标记器索引字段,但不使用stopwords,elasticsearch,tokenize,analyzer,stop-words,elasticsearch,Tokenize,Analyzer,Stop Words" /> elasticsearch Elasticsearch:使用关键字标记器索引字段,但不使用stopwords,elasticsearch,tokenize,analyzer,stop-words,elasticsearch,Tokenize,Analyzer,Stop Words" />

elasticsearch Elasticsearch:使用关键字标记器索引字段,但不使用stopwords

elasticsearch Elasticsearch:使用关键字标记器索引字段,但不使用stopwords,elasticsearch,tokenize,analyzer,stop-words,elasticsearch,Tokenize,Analyzer,Stop Words,我正在寻找一种方法来搜索公司名称与关键字标记,但没有停止词 例如:索引公司名称为“Hansel und Gretel Gmbh” 这里“und”和“Gmbh”是公司名称的停止词 如果搜索词为“Hansel Gretel”,则应找到该文档, 如果搜索词为“Hansel”,则不应找到任何文档。如果搜索词为“hansel gmbh”,则也应找到no文档 我曾尝试在自定义分析器中将关键字标记器和停止字组合在一起,但并没有起作用(我猜这和预期的一样) 我也尝试过使用通用术语查询,但“Hansel”开始出现

我正在寻找一种方法来搜索公司名称与关键字标记,但没有停止词

例如:索引公司名称为“Hansel und Gretel Gmbh”

这里“und”和“Gmbh”是公司名称的停止词

如果搜索词为“Hansel Gretel”,则应找到该文档, 如果搜索词为“Hansel”,则不应找到任何文档。如果搜索词为“hansel gmbh”,则也应找到no文档

我曾尝试在自定义分析器中将关键字标记器和停止字组合在一起,但并没有起作用(我猜这和预期的一样)

我也尝试过使用通用术语查询,但“Hansel”开始出现(正如预期的那样)


提前谢谢。

有两种方式:坏的和丑的。第一种方法使用正则表达式来删除停止词和修剪空格。有很多缺点:

  • 您必须支持空白标记化(regexp(/s+)和特殊符号(,;)的删除
  • 不支持突出显示-关键字标记器不支持
  • 区分大小写也是一个问题
  • (关键字分析器)是实验性功能-支持不好,没有功能
下面是一个循序渐进的示例:

curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "normalizer": {
        "custom_normalizer": {
          "type": "custom",
          "char_filter": ["stopword_char_filter", "trim_char_filter"],
          "filter": ["lowercase"]
        }
      },
      "char_filter": {
        "stopword_char_filter": {
          "type": "pattern_replace",
          "pattern": "( ?und ?| ?gmbh ?)",
          "replacement": " "
        },
        "trim_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\\s+)$",
          "replacement": ""
        }
      }
    }
  },
  "mappings": {
    "file": {
      "properties": {
        "name": {
          "type": "keyword",
          "normalizer": "custom_normalizer"
        }
      }
    }
  }
}'
curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_analyzer": {
          "type":      "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "custom_stopwords"]
        }
      }, "filter": {
        "custom_stopwords": {
          "type": "stop",
          "stopwords": ["und", "gmbh"]
        }
      }
    }
  },
  "mappings": {
    "file": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "custom_analyzer"
        }
      }
    }
  }
}' 
现在我们可以检查分析器的工作方式(请注意,只有在ES 6.x中才支持对normalyzer的请求)

现在,我们准备为文档编制索引:

curl -XPUT "http://localhost:9200/test/file/1" -H 'Content-Type: application/json' -d'
{
  "name": "hansel und gretel gmbh"
}'
最后一步是搜索:

curl -XGET "http://localhost:9200/test/_search" -H 'Content-Type: application/json' -d'
{
    "query": {
        "match" : {
            "name" : {
                "query" : "hansel gretel"
            }
        }
    }
}'
另一种方法是:

  • 使用停止字过滤器创建标准文本分析器
  • 使用分析过滤掉所有停止字和特殊符号
  • 手动连接令牌
  • 将术语作为关键字发送到ES
下面是一个循序渐进的示例:

curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "normalizer": {
        "custom_normalizer": {
          "type": "custom",
          "char_filter": ["stopword_char_filter", "trim_char_filter"],
          "filter": ["lowercase"]
        }
      },
      "char_filter": {
        "stopword_char_filter": {
          "type": "pattern_replace",
          "pattern": "( ?und ?| ?gmbh ?)",
          "replacement": " "
        },
        "trim_char_filter": {
          "type": "pattern_replace",
          "pattern": "(\\s+)$",
          "replacement": ""
        }
      }
    }
  },
  "mappings": {
    "file": {
      "properties": {
        "name": {
          "type": "keyword",
          "normalizer": "custom_normalizer"
        }
      }
    }
  }
}'
curl -XPUT "http://localhost:9200/test" -H 'Content-Type: application/json' -d'
{
  "settings": {
    "analysis": {
      "analyzer": {
        "custom_analyzer": {
          "type":      "custom",
          "tokenizer": "standard",
          "filter": ["lowercase", "custom_stopwords"]
        }
      }, "filter": {
        "custom_stopwords": {
          "type": "stop",
          "stopwords": ["und", "gmbh"]
        }
      }
    }
  },
  "mappings": {
    "file": {
      "properties": {
        "name": {
          "type": "text",
          "analyzer": "custom_analyzer"
        }
      }
    }
  }
}' 
现在我们准备分析我们的文本:

POST test/_analyze
{
  "analyzer": "custom_analyzer",
  "text": "Hansel und Gretel Gmbh."
}
结果如下:

{
  "tokens": [
    {
      "token": "hansel",
      "start_offset": 0,
      "end_offset": 6,
      "type": "<ALPHANUM>",
      "position": 0
    },
    {
      "token": "gretel",
      "start_offset": 11,
      "end_offset": 17,
      "type": "<ALPHANUM>",
      "position": 2
    }
  ]
}
{
“代币”:[
{
“令牌”:“hansel”,
“起始偏移量”:0,
“端部偏移”:6,
“类型”:“,
“位置”:0
},
{
“令牌”:“gretel”,
“起始偏移量”:11,
“端部偏移”:17,
“类型”:“,
“职位”:2
}
]
}

最后一步是令牌连接:hansel+gretel。唯一的缺点是使用自定义代码进行手动分析

问问题时请更具体一点:到目前为止,您用代码示例做了哪些尝试?()/你期待什么?/你有什么错误?有关帮助,请查看“”,并提供设置、文档和query@Ivan,我无法确定应该使用哪些设置和查询。事实上,这就是问题所在。对于文档,只要考虑一个字符串字段。(它必须分析一些关于停用词的猜测,但问题是如何分析……)USER 308242,然后请用“@ Hille”写在您的问题中,我想我已经表达了我的期望,我没有任何错误。我曾尝试使用stopwords作为自定义分析器来cobine关键字过滤器,但没有成功。问题不在于为什么,而在于如何,因为我不知道如何达到目的。如果你认为我试图做的不清楚,那么让我用另一种方式来表达。