【问题标题】:ElasticSearch search results with variable values具有变量值的 ElasticSearch 搜索结果
【发布时间】:2020-12-26 06:04:25
【问题描述】:

我希望将 elasticsearch 用于具有自动完成功能的搜索栏。 我有一组问题,它们有多个变量,我想用其他值替换。

一个问题的模板是:你喜欢FIELDS吗? 用数学、物理、历史代替FIELDS 然后在查询“你喜欢”这个问题时,它会显示多个点击:

  • 你喜欢数学吗?
  • 你喜欢物理吗?
  • 你喜欢历史吗?

我将 elasticseach 视为同义词分析器,并认为它可以用于这个用例,但它似乎不像我预期的那样工作。以下是我目前所拥有的。

创建索引

{
        "mappings": {
            "properties": {
                "my_field": {
                    "type": "search_as_you_type",
                    "analyzer": "standard",
                    "search_analyzer": "synonym_analyzer"
                }
            }
        },
        "settings": {
            "index": {
                "analysis": {
                    "analyzer": {
                        "synonym_analyzer": {
                            "tokenizer": "whitespace",
                            "filter": ["my_synonyms"]
                        }
                    },
                    "filter": {
                        "my_synonyms": {
                            "type": "synonym",
                            "synonyms": [
                                "FIELDS => math, physics, history"
                            ]
                        }
                    }
                }
            }
        }
    }

查询

{
        "query": {
            "multi_match": {
                "query": partial_question,
                "type": "bool_prefix",
                "fields": [
                    "my_field",
                    "my_field._2gram",
                    "my_field._3gram",
                    "my_field._index_prefix"
                ]
            }
        }
    }

结果是一个结果“你喜欢FIELDS吗?”

【问题讨论】:

  • 是否可以提供您想要的输出示例?我有些困惑,无法理解。如果我很好地理解了你的问题,如果你搜索一个特定的值,它应该返回“你喜欢数学吗?”与其他热门歌曲[物理和历史]
  • 是的,想要的输出是你喜欢数学吗?你喜欢物理吗?你喜欢历史吗?当我有partial question 是“你喜欢吗”
  • @Ffloriel ,您能否看一下我的回答,如果您有后续相关问题,请告诉我,如我的回答所示,它解决了相关问题:)
  • @Ffloriel 如果您有后续问题,请告诉我,否则,如果有帮助,请投票并接受答案,在此先感谢 :)
  • @Ffloriel 遗憾的是,赏金也结束了,你没有获得全部赏金,也没有提出后续问题并接受任何答案,希望你一切都好,请尽可能回复。跨度>

标签: elasticsearch elasticsearch-query elasticsearch-7


【解决方案1】:

@hansley 回答会起作用,但由于通配符查询成本很高,您可以简单地使用 prefix query 而无需更改索引中的任何内容。

虽然在 ES 中实现 Autosuggest 有多种方法,但考虑到它的重要性和受欢迎程度,我已经写了一个 detailed blog on various approaches and their trade-off 并且我的 this SO answer 可以为您提供有关构建 Autosuggest 功能的功能和非功能要求的信息。

使用前缀查询的端到端示例:

默认索引映射,为每个文本字段创建一个.keyword field

索引示例文档:

{
  "title" : "i like red car"
}

{
  "title" : "do you like math?"
}

{
  "title" : "do you like physics?"
}

搜索查询

{
  "query": {
    "prefix": {
      "title.keyword": {
        "value": "do you like"
      }
    }
  }
}

搜索结果

"hits": [
      {
        "_index": "partialpre",
        "_type": "_doc",
        "_id": "1",
        "_score": 1.0,
        "_source": {
          "title": "do you like math?"
        }
      },
      {
        "_index": "partialpre",
        "_type": "_doc",
        "_id": "2",
        "_score": 1.0,
        "_source": {
          "title": "do you like physics?"
        }
      }
    ]

【讨论】:

    【解决方案2】:

    如果我很好地理解了您的问题,我会提出您可以使用的建议答案。我使用通配符查询和规范化器来小写所有值:

    这是我的索引的映射:

    PUT multiple-fields
    {
      "settings": {
        "analysis": {
          "normalizer": {
            "lowercase_normalizer": {
              "type":"custom",
            "filter": ["lowercase"]
            }
            
          }
        }
      },
      "mappings": {
        "properties": {
          "quest":{
            "type": "keyword",
            "normalizer": "lowercase_normalizer"
          }
        }
      }
    }
    

    我在索引中摄取了以下数据:

    "quest":"你喜欢数学吗?"

    "quest":"你喜欢物理吗?"

    "quest":"你喜欢历史吗?"

    “任务”:“我想我确实喜欢你”

    "quest":"我喜欢红色汽车"

    “任务”:“你不喜欢”

    "quest":"你喜欢数学吗?"

    根据这些值,我创建了以下查询:

    GET multiple-fields/_search
    {
      "query": {
        "wildcard": {
          "quest": {
            "value": "do you like*"
          }
        }
      }
    }
    

    回复是:

    "hits" : [
      {
        "_index" : "multiple-fields",
        "_type" : "_doc",
        "_id" : "bue1e3QBsTCl1BZvB0by",
        "_score" : 1.0,
        "_source" : {
          "quest" : "do you like math?"
        }
      },
      {
        "_index" : "multiple-fields",
        "_type" : "_doc",
        "_id" : "cOe1e3QBsTCl1BZvD0Yh",
        "_score" : 1.0,
        "_source" : {
          "quest" : "do you like physics?"
        }
      },
      {
        "_index" : "multiple-fields",
        "_type" : "_doc",
        "_id" : "cee1e3QBsTCl1BZvE0Zq",
        "_score" : 1.0,
        "_source" : {
          "quest" : "do you like history?"
        }
      },
      {
        "_index" : "multiple-fields",
        "_type" : "_doc",
        "_id" : "2-e1e3QBsTCl1BZvLUak",
        "_score" : 1.0,
        "_source" : {
          "quest" : "Do you like math?"
        }
      }
    ]
    

    链接: https://www.elastic.co/guide/en/elasticsearch/reference/current/normalizer.html https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-wildcard-query.html

    注意:但是,如果使用通配符,可能会影响性能

    如果对您有帮助,请告诉我,否则我们可以研究另一种解决方案,谢谢。

    【讨论】:

      【解决方案3】:

      我认为您正在寻找同义词的错误方向。 你想要的是suggester。 您将需要做一些具体的工作,但您将能够为您的用例创建非常强大的建议。

      【讨论】:

        猜你喜欢
        • 2015-11-11
        • 1970-01-01
        • 2021-04-19
        • 2014-10-10
        • 1970-01-01
        • 1970-01-01
        • 2016-03-17
        • 2015-03-22
        • 2020-08-30
        相关资源
        最近更新 更多