elasticsearch - Elasticsearch 查找所有文档在字段上包含单词

标签 elasticsearch

我想知道如何搜索具有包含单词的字符串字段的所有文档。
我正在寻找一个在单词前后使用通配符*的解决方案。
但这不是很好,因为它还会检索包含包含该字符串的较大单词的文档。
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/

相关文章:

elasticsearch - bool 查询中的多个应该和必须

elasticsearch - elasticsearch如何查询具有数据范围的两个不同字段

search - 在 Elasticsearch 中查询时如何发送提升值?

elasticsearch - Logstash + elasticsearch中的UnresolvedAddressException

linux - 如何在 Redhat Linux 中设置 Lumberjack(logstash 转发器)

mysql - Logstash sql_last_value 未更新

spring - 为什么在使用spring-data-elasticsearch时将HEAD请求发送到我的索引

python - Haystack 和 ElasticSearch : Searching Related Fields; Use nested Type?

elasticsearch - 如何使用ElasticSearch日期直方图value_field执行存储桶过滤

elasticsearch - 使用嵌套查询时出现 Elasticsearch 问题