gpt4 book ai didi

elasticsearch - 使用Elasticsearch的Google类型查询

转载 作者:行者123 更新时间:2023-12-02 22:19:55 25 4
gpt4 key购买 nike

假设我有以下文档:

{title:"Sennheiser HD 800"}

我要所有这些查询返回此文档。
  • heise
  • sennheise
  • 森海塞尔
  • 森海塞尔800
  • 森海塞尔高清
  • 高清
  • 800高清
  • hd enhheise

  • 简而言之,我想找到一个或多个不完整词。
    在我的 map 上,我正在使用此分析仪
    {
    "settings": {
    "analysis": {
    "analyzer": {
    "case_insensitive_sort": {
    "tokenizer": "keyword",
    "filter": [
    "lowercase"
    ]
    }
    }
    }
    }
    }

    和 map
    {
    "title": {
    "type": "string",
    "fields": {
    "raw": {
    "type": "string",
    "index": "not_analyzed"
    },
    "lower_case_sort": {
    "type": "string",
    "analyzer": "case_insensitive_sort"
    }
    }
    }
    }

    该查询是一个简单的字符串查询
    {
    "query": {
    "query_string": {
    "fields": [
    "title.lower_case_sort"
    ],
    "query": "*800 hd*"
    }
    }
    }

    例如,此查询失败。

    最佳答案

    您需要ngrams

    这是我为Qbox撰写的博客文章:

    https://qbox.io/blog/an-introduction-to-ngrams-in-elasticsearch

    (请注意,"index_analyzer"在ES 2.x中不再起作用;请改为使用"analyzer";但是"search_analyzer"仍然有效。)

    使用此映射(在博客文章中略作修改;我将在那儿为您提供详细说明):

    PUT /test_index
    {
    "settings": {
    "analysis": {
    "filter": {
    "ngram_filter": {
    "type": "ngram",
    "min_gram": 2,
    "max_gram": 20
    }
    },
    "analyzer": {
    "ngram_analyzer": {
    "type": "custom",
    "tokenizer": "standard",
    "filter": [
    "lowercase",
    "ngram_filter"
    ]
    }
    }
    }
    },
    "mappings": {
    "doc": {
    "properties": {
    "title": {
    "type": "string",
    "analyzer": "ngram_analyzer",
    "search_analyzer": "standard"
    }
    }
    }
    }
    }

    为您的文档编制索引:
    POST /test_index/doc/1
    {
    "title": "Sennheiser HD 800"
    }

    然后您列出的任何查询都可以采用以下形式运行:
    POST /test_index/_search
    {
    "query": {
    "match": {
    "title": {
    "query": "heise hd 800",
    "operator": "and"
    }
    }
    }
    }

    如果只有一个术语,则不需要 "operator"部分:
    POST /test_index/_search
    {
    "query": {
    "match": {
    "title": "hd"
    }
    }
    }

    这是我以前玩过的一些代码:

    http://sense.qbox.io/gist/a9accf67f1713ca99819f45ce0ac28adaea691a9

    关于elasticsearch - 使用Elasticsearch的Google类型查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34303076/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com