【问题标题】:Elastic search find all documents contain word on a field弹性搜索查找包含字段上的单词的所有文档
【发布时间】:2015-11-13 20:34:32
【问题描述】:

我想知道如何搜索所有具有包含单词的字符串字段的文档。

我正在寻找一种在单词前后使用通配符和 * 的解决方案。 但它不好,因为它还检索包含包含该字符串的更大单词的文档。 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 不包括在内,因为它不是新闻这个词,它是不相关的更大词的一部分

感谢帮助

【问题讨论】:

  • 您的数据集样本是什么样的?我假设新闻这个词不仅仅在字段文本中。
  • 您能否提供更多关于您想要执行的查询类型、想要的结果和想要避免的结果的信息?

标签: elasticsearch


【解决方案1】:

首先您需要删除"index" : "not_analyzed",因为您需要不区分大小写的搜索。 "index" : "not_analyzed" 将按原样索引单词,而您搜索单词“news”不会给您文档 5。

{
   "mappings" : {
          "text" : {
             "properties" : {
                "text" : { "type" : "string"},
                "url" : { "type" : "string"}
             }
          }
   }
}

我使用默认的standard analyzer,因为我没有指定任何分析器。您可以了解更多关于 ElasticSearch 分析Here

之后,一个简单的match query 就足以获取所有需要的文档。

{
  "query": {
    "match": {
      "text": "news"
    }
  }
}

如果您想要词组搜索,可以将匹配查询替换为 match_phrase 查询。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2020-09-11
    相关资源
    最近更新 更多