我想知道如何搜索具有包含单词的字符串字段的所有文档。
我正在寻找一个在单词前后使用通配符*的解决方案。
但这不是很好,因为它还会检索包含包含该字符串的较大单词的文档。
https://www.elastic.co/guide/en/elasticsearch/guide/current/_wildcard_and_regexp_queries.html
即如果我搜索“新闻”
结果可以包含“Wikinews”,这不是我想要的。
我的索引是这样定义的:
PUT /index
{
"mappings" : {
"text" : {
"properties" : {
"text" : { "type" : "string", "index" : "not_analyzed" },
"url" : { "type" : "string"}
}
}
}
}
我想搜索给定单词在“文本”字段中的文档编辑:
示例数据:
curl -XPUT 'http://localhost:9200/index/type/1' -d '
{
"url": "wikipedia.com",
"Text": "in the news",
}'
curl -XPUT 'http://localhost:9200/index/type/2' -d '
{
"url": "wikipedia.com",
"Text": "Click here for Wikinews",
}'
curl -XPUT 'http://localhost:9200/index/type/3' -d '
{
"url": "wikipedia.com",
"Text": "news for each page are those:",
}'
curl -XPUT 'http://localhost:9200/index/type/4' -d '
{
"url": "wikipedia.com",
"Text": "What are the news means to you",
}'
curl -XPUT 'http://localhost:9200/index/type/5' -d '
{
"url": "walla.com",
"Text": "today News are more ...",
}'
这应该返回文件1,3,4,5文档5,因为搜索不区分大小写。
未包含文档2,因为它不是新闻一词,而是不相关的大词的一部分
谢谢 helper
最佳答案
首先,您需要删除"index" : "not_analyzed"
,因为您需要不区分大小写的搜索。 "index" : "not_analyzed"
将按原样索引该词,而您搜索“新闻”一词将不会给您文档5。
{
"mappings" : {
"text" : {
"properties" : {
"text" : { "type" : "string"},
"url" : { "type" : "string"}
}
}
}
}
我使用的是默认standard analyzer,因为我没有指定任何分析器。您可以了解有关ElasticSearch Analysis Here的更多信息。
之后,一个简单的
match query
就足以获取所有需要的文档。{
"query": {
"match": {
"text": "news"
}
}
}
如果要短语搜索,可以将匹配查询替换为
match_phrase
查询。
关于elasticsearch - Elasticsearch 查找所有文档在字段上包含单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33684886/