【问题标题】:How to use the Phrase Suggester in the Elasticsearch?如何在 Elasticsearch 中使用 Phrase Suggester?
【发布时间】:2015-12-07 15:51:45
【问题描述】:

我想在 Elasticsearch 1.7 中使用 Phrase Suggester。

https://www.elastic.co/guide/en/elasticsearch/reference/current/search-suggesters-phrase.html

所以,我在下面创建了类似文档页面示例的查询,但我得到了错误。

嵌套:ElasticsearchIllegalArgumentException [未找到映射 字段[项目名称]];

$ curl -XPOST 'localhost:9200/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  },
  "suggest": {
    "text": "cola",
    "simple_phrase": {
      "phrase": {
        "field": "itemname",
        "size": 5,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}'

但是字段 [itemname]] 是明确定义的。
事实上,我可以使用这个查询从 itemname 字段中进行搜索。

$ curl -XPOST 'localhost:9200/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  }
}'
{
  "took" : 9,
  "timed_out" : false,
  "_shards" : {
    "total" : 15,
    "successful" : 15,
    "failed" : 0
  },
  "hits" : {
    "total" : 97,
    "max_score" : 11.625176,
    "hits" : [ {
      "_index" : "my_index",
      "_type" : "my_type",
      "_id" : "20615",
      "_score" : 11.625176,
      "_source":{"itemid":"20615","itemname":"cola 500ml"}
    }, {

在这种情况下,我怎么了?
有人建议我如何正确使用短语建议器吗?

谢谢。

添加我的设置

# curl -XGET 'http://localhost:9200/my_index?pretty'
{
  "my_index" : {
    "aliases" : { },
    "mappings" : {
      "my_type" : {
        "_all" : {
          "enabled" : true,
          "analyzer" : "kuromoji_analyzer"
        },
        "properties" : {
          "itemid" : {
            "type" : "string",
            "index" : "not_analyzed",
            "store" : true
          },
          "catname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          },
          "itemname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          },
          "myscore" : {
            "type" : "double",
            "store" : true
          },
          "subcatname" : {
            "type" : "string",
            "store" : true,
            "analyzer" : "kuromoji_analyzer"
          }
        }
      }
    },

【问题讨论】:

  • 您是否有可能在您的一个索引中定义了几种不同的类型(因为您在根目录/ 进行搜索)而一个没有itemname 字段?跨度>
  • @Val 感谢您的回复。是的,实际上正如你所说,我有两个索引。另一个指标是完成建议。我将我的设置添加到帖子中。有没有错误的设置?而且我还尝试将“itemname”添加到“my_index_suggest”设置并运行短语建议查询,但我得到了同样的错误。
  • 您在哪个索引上运行您的建议器?它没有出现在您的问题中(即顶部的第一个查询)
  • @Val 哦,“curl -XPOST 'localhost:9200/my_index/my_type/_search?pretty”正在工作。
  • 是的,因为itemnamemy_type 中定义。那么好

标签: elasticsearch


【解决方案1】:

我认为,由于您在根端点 / 上运行建议查询,因此您的搜索会命中另一个索引,该索引没有任何定义 itemname 字段的映射类型。

尝试直接在具有定义itemname 字段的映射类型的索引上运行查询。

根据您的第二个查询的结果,您应该尝试在 /my_index/my_type 而不是根端点 / 上运行您的建议器

                        add the index and the type
                                |      |
                                v      v
curl -XPOST 'localhost:9200/my_index/my_type/_search?pretty' -d '{
  "query": {
    "function_score": {
      "query": {
        "filtered": {
          "query": {
            "query_string": {
              "fields": [
                "itemname"
              ],
              "query": "cola"
            }
          }
        }
      }
    }
  },
  "suggest": {
    "text": "cola",
    "simple_phrase": {
      "phrase": {
        "field": "itemname",
        "size": 5,
        "real_word_error_likelihood": 0.95,
        "max_errors": 0.5,
        "gram_size": 2
      }
    }
  }
}'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-01-24
    • 2022-01-14
    • 1970-01-01
    • 1970-01-01
    • 2018-07-20
    相关资源
    最近更新 更多