elasticsearch - 如何在 Elasticsearch 中查询自动完成

标签 elasticsearch

这是我使用的索引和 doc_type 的设置和映射。

    es_mapping = {
  "settings": {
      "analysis": {
          "filter": {
              "edgeNGram_filter": {
                  "type": "edgeNGram",
                  "min_gram": 2,
                  "max_gram": 20,
                  "side": "front"
              }
          },
          "analyzer": {
              "edge_nGram_analyzer": {
                  "type": "custom",
                  "tokenizer": "edge_ngram_tokenizer",
                  "filter": [
                      "lowercase",
                      "asciifolding",
                      "edgeNGram_filter"
                  ]
              },
              "whitespace_analyzer": {
                  "type": "custom",
                  "tokenizer": "whitespace",
                  "filter": [
                      "lowercase",
                      "asciifolding"
                  ]
              }
          },
          "tokenizer": {
              "edge_ngram_tokenizer": {
                  "type": "edgeNGram",
                  "min_gram": "2",
                  "max_gram": "5",
                  "token_chars": [
                      "letter",
                      "digit"
                  ]
              }
          }
      }
  },
  "mappings": {
    "videos": {
        "properties": {
            "fileName": {
              "type": "string"
            },
            "path": {
              "type": "string",
              "index": "no"
            }
        }
    }
  }
}

# creating index with the above schema.
es.create_index('media', settings = es_mapping)

因此,只有两个字段,即 文件名 路径 , 我需要对 进行自动完成查询文件名 .

我已添加所有 ngram 分析器 空白分析器

我的查询是匹配短语
"query": {
    "match_phrase": {
        "fileName": {
            "query": "a"
        }
    }
}

上面的查询给了我所有匹配的短语,基本上是全文搜索,但我想要 替代 , 苹果 , 宏基作为建议,只要这些论文在索引中。

如何更改查询?

在 val 建议后编辑
      "settings": {
      "analysis": {
          "filter": {
            "autocomplete_filter": {
                "type":     "edge_ngram",
                "min_gram": 1,
                "max_gram": 20
            }
          },
          "analyzer": {
            "autocomplete": {
                "type":      "custom",
                "tokenizer": "standard",
                "filter": [
                    "lowercase",
                    "autocomplete_filter"
                ]
            }
          }
      }
  },
  "mappings": {
    "videos": {
        "properties": {
            "fileName": {
              "type": "string",
              "analyzer": "autocomplete",
              "search_analyzer": "standard"
            },
            "path": {
              "type": "string",
              "index": "no"
            }
        }
    }
  }
}

现在,这是自动完成的设置和映射。

查询的结构如下:
{
    "query": {
        "match": {
            "fileName": "a"
        }
    },
    "highlight" : {
        "fields" : {
            "fileName" : {"force_source" : true}
        }
    }
}

我用 亮点 ,这增加了用户的交互性。

最佳答案

您没有使用您定义的分析器。您需要将它们设置为默认分析器或专门设置为 analyzersearch_analyzer在你的领域。

然后,您需要删除索引,重新创建它并重新索引您的数据。

之后,您将获得您期望的结果。

关于elasticsearch - 如何在 Elasticsearch 中查询自动完成,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36806081/

相关文章:

ruby - 如何对 elasticsearch/searchkick 日期字段进行排序?

java - 创建加速搜索查询elasticsearch

java - Elasticsearch中的时间戳

ruby-on-rails - 重新轮胎 Elasticsearch 多表/索引搜索

elasticsearch - elasticsearch:在文本内搜索

elasticsearch - elastalert 中的电子邮件发送错误。 SMTPSenderRefused : (530, '5.5.1 需要身份验证)

python - 使用Django Haystack仅匹配查询中的某些单词

elasticsearch - Elasticsearch Java HighLevelRestClient如何创建not_analyzed索引?

elasticsearch - Marvel 无法连接到 Elasticsearch 节点

lucene - elasticsearch查询缺少过滤器