【问题标题】:ElasticSearch autocomplete returning 0 hitsElasticSearch 自动完成返回 0 次点击
【发布时间】:2014-02-17 11:35:29
【问题描述】:

我正在尝试为我们在 MongoDB 上运行的数据库构建一个自动完成功能。我们需要提供自动完成功能,让用户在搜索框中输入内容时提供建议来完成他们的查询。

我收集了来自各种来源的articles,其中包含以下字段:

{
    "title" : "Its the title of a random article",
    "cont" : {  "paragraphs" : [ .... ]  },
    and so on..
}

我通过了video by Clinton Gormley。从 37:00 到 42:00 分钟,Gormley 使用 edgeNGram 描述了一个自动完成功能。另外,我参考了this question 认识到两者几乎是相同的东西,只是映射不同。

所以基于这些经验,我构建了几乎相同的设置和映射,然后恢复 articles 集合以确保它被 ElasticSearch 索引

索引方案如下:

POST /title_autocomplete/title
{
    "settings": {

        "analysis": {
            "filter": {
                "autocomplete": {
                    "type": "edgeNGram",
                    "min_gram": 2,
                    "max_gram": 50
                }
            },

            "analyzer": {

                "title" : {
                    "type" : "standard",
                    "stopwords":[]
                },
                "autocomplete": {
                    "type" : "autocomplete",
                     "tokenizer": "standard",
                     "filter": ["lowercase", "autocomplete"]
                }
             }
        }
    },
    "mappings": {
        "title": {
            "type": "multi_field",
            "fields" :  {
                "title" : {
                    "type": "string",
                    "analyzer": "title"
                },
                "autocomplete" : {
                    "type": "string",
                    "index_analyzer": "autocomplete",
                    "search_analyzer" : "title"
                }
            }
       }
    }
} 

但是当我运行搜索查询时,我无法获得任何点击!

GET /title_autocomplete/title/_search
{
    "query": {
        "bool" : {
            "must" : {
                "match" : {
                    "title.autocomplete" : "Its the titl"
                }
            },
            "should" : {
                "match" : {
                    "title" : "Its the titl"
                }
            }
        }
    }
}

谁能解释一下映射查询或设置有什么问题?我已经阅读 ElasticSearch 文档超过 7 天了,但似乎只获得了全文搜索!

  • ElastiSearch 版本:0.90.10
  • MongoDB 版本:v2.4.9
  • 使用_river
  • Ubuntu 12.04 64 位

更新 我意识到应用以前的设置后映射搞砸了:

GET /title_autocomplete/_mapping
{
   "title_autocomplete": {
      "title": {
         "properties": {
            "analysis": {
               "properties": {
                  "analyzer": {
                     "properties": {
                        "autocomplete": {
                           "properties": {
                              "filter": {
                                 "type": "string"
                              },
                              "tokenizer": {
                                 "type": "string"
                              },
                              "type": {
                                 "type": "string"
                              }
                           }
                        },
                        "title": {
                           "properties": {
                              "type": {
                                 "type": "string"
                              }
                           }
                        }
                     }
                  },
                  "filter": {
                     "properties": {
                        "autocomplete": {
                           "properties": {
                              "max_gram": {
                                 "type": "long"
                              },
                              "min_gram": {
                                 "type": "long"
                              },
                              "type": {
                                 "type": "string"
                              }
                           }
                        }
                     }
                  }
               }
            },
            "content": { 
                  ... paras and all  ...
            }
            "title": {
               "type": "string"
            },
            "url": {
               "type": "string"
            }
         }
      }
   }
}

分析器和过滤器在应用设置后实际上映射到文档中,而原始title 字段根本不受影响!这正常吗?? 我想这解释了为什么查询不匹配。根本没有title.autocomplete 字段或title.title 字段。

那我现在应该怎么做呢?

【问题讨论】:

  • 从您所展示的内容来看,它对我来说看起来不错,所以我认为缺少一些东西。你能更新完整的映射吗(使用 GET /title_autocomplete/title/_mapping 从 Elasticsearch 检索,加上完整的命令来索引和搜索你的文档。你可能想使用explain API 告诉你为什么没有匹配。跨度>
  • @DrTech:这是我为复制相同问题所采取的完整步骤:hastebin.com/wovihomaxi.vala
  • 您应该如何进行?删除并重新开始:) 首先使用 curl,然后使用 mongo 解决。我建议不要使用 mongo 河。只需通过您的应用将更改推送到 ES。

标签: mongodb autocomplete elasticsearch elasticsearch-mongo-river


【解决方案1】:

对于那些面临这个问题的人,最好删除索引并重新开始,而不是像DrTech pointed out in the comment一样浪费时间使用_river

这可以节省时间,但不是解决方案。 (因此不将其标记为答案。)

【讨论】:

    【解决方案2】:

    关键是在启动河流之前设置映射和索引。

    我们有一个现有的设置,其中包含一个 mongodb River 和一个名为 coresearch 的索引,我们想向其中添加自动完成功能,这是我们用来删除现有索引和 River 并重新开始的一组命令。

    堆栈是:

    • 弹性搜索 1.1.1
    • MongoDB 2.4.9
    • ElasticSearchMapperAttachments v2.0.0
    • ElasticSearchRiverMongoDb/2.0.0
    • Ubuntu 12.04.2 LTS

      curl -XDELETE "localhost:9200/_river/node" curl -XDELETE "localhost:9200/coresearch"

      curl -XPUT "localhost:9200/coresearch" -d ' { “设置”:{ “分析”: { “筛选”: { “自动完成过滤器”:{ “类型”:“edge_ngram”, “min_gram”:1, “最大克数”:20 } }, “分析仪”:{ “自动完成”:{ “类型”:“自定义”, “标记器”:“标准”, “筛选”: [ "小写", “自动完成过滤器” ] } } } } }'

      curl -XPUT "localhost:9200/coresearch/_mapping/users" -d '{ “用户”:{ “特性”: { “名”: { “类型”:“字符串”, "search_analyzer": "标准", “index_analyzer”:“自动完成” }, “姓”: { “类型”:“字符串”, "search_analyzer": "标准", “index_analyzer”:“自动完成” }, “用户名”: { “类型”:“字符串”, "search_analyzer": "标准", “index_analyzer”:“自动完成” }, “电子邮件”: { “类型”:“字符串”, "search_analyzer": "标准", “index_analyzer”:“自动完成” } } } }'

      curl -XPUT "localhost:9200/_river/node/_meta" -d ' { “类型”:“mongodb”, “蒙哥”:{ “服务器”:[ {“主机”:“127.0.0.1”,“端口”:27017 } ], “选项”:{ “排除字段”:[“时间”] },

      "db": "users",  
      "gridfs": false,
      "options": {
        "import_all_collections": true
      }
      },
      "index": {
        "name": "coresearch",
        "type": "documents"
      }
      

      }'

    【讨论】:

      猜你喜欢
      • 2020-02-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-04
      • 2020-01-19
      • 2012-06-29
      相关资源
      最近更新 更多