【问题标题】:Elasticsearch | copy_to with partial searching弹性搜索 |部分搜索的copy_to
【发布时间】:2017-01-23 23:24:18
【问题描述】:

我的 copy_to 可以正常工作以进行完全匹配,但我无法正确设置部分匹配。以下是我的映射/设置和查询以及预期和实际结果。

设置:

{
   "test": {
      "settings": {
         "index": {
            "analysis": {
               "filter": {
                  "ngram_filter": {
                     "type": "edge_ngram",
                     "min_gram": "1",
                     "max_gram": "15"
                  }
               },
               "analyzer": {
                  "ngram_analyzer": {
                     "filter": [
                        "lowercase",
                        "ngram_filter"
                     ],
                     "type": "custom",
                     "tokenizer": "standard"
                  }
               }
            },
            "number_of_shards": "1",
            "number_of_replicas": "1",
           }
      }
   }
}

映射:

POST /test/_mapping/name
{
   "name": {
      "properties": {
         "vital": {
            "properties": {
               "first": {
                  "type": "string",
                  "copy_to": "full_name",
                   "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
               },
               "last": {
                  "type": "string",
                  "copy_to": "full_name",
                   "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
               },
               "full_name": {
                  "type": "string",
                   "term_vector": "yes",
                    "analyzer": "ngram_analyzer",
                    "search_analyzer": "standard"
               }
            }
         }
      }
   }
}

发布:

POST /test/name 
{
   "vital": {
      "first": "Tom",
      "last": "Doe"
   }
}

现在当我进行搜索时...

GET /test/name/_search
{
  "query": {
    "match": {
      "full_name": { 
        "query": "Tom Doe",
        "operator": "and"
      }
    }
  }
}

...我得到了结果!! Hurrraaaay,但如果我进行搜索....

GET /test/name/_search
{
  "query": {
    "match": {
      "full_name": { 
        "query": "Tom Do",
        "operator": "and"
      }
    }
  }
}

...我没有返回任何结果:(我希望部分匹配也适用于全名。作为另一个,我不能成功地对名字和姓氏进行部分匹配。只是全​​名不起作用. 我该怎么办?

【问题讨论】:

    标签: search elasticsearch mapping settings partial


    【解决方案1】:

    您的映射有一个小错误,您需要将名字和姓氏复制到vital.full_name 字段中,而不仅仅是full_name,否则将使用标准分析器创建一个名为full_name 的新字符串字段在映射的顶层(如果您运行 GET test,您将在映射中看到该新字段):

    POST /test/_mapping/name
    {
       "name": {
          "properties": {
             "vital": {
                "properties": {
                   "first": {
                      "type": "string",
                      "copy_to": "vital.full_name",      <--- fix this
                       "term_vector": "yes",
                        "analyzer": "ngram_analyzer",
                        "search_analyzer": "standard"
                   },
                   "last": {
                      "type": "string",
                      "copy_to": "vital.full_name",      <--- fix this
                       "term_vector": "yes",
                        "analyzer": "ngram_analyzer",
                        "search_analyzer": "standard"
                   },
                   "full_name": {
                      "type": "string",
                       "term_vector": "yes",
                        "analyzer": "ngram_analyzer",
                        "search_analyzer": "standard"
                   }
                }
             }
          }
       }
    }
    

    然后像这样修复您的查询:

    POST /test/name/_search
    {
      "query": {
        "match": {
          "vital.full_name": {          <-- fix this
            "query": "Tom Doe",
            "operator": "and"
          }
        }
      }
    }
    
    POST /test/name/_search
    {
      "query": {
        "match": {
          "vital.full_name": {          <-- fix this
            "query": "Tom Do",
            "operator": "and"
          }
        }
      }
    }
    

    两者都将按您的预期工作。

    【讨论】:

      【解决方案2】:

      只需从您的映射中删除 "search_analyzer": "standard",它仅在某些用例中需要,例如自动完成搜索。在此处查看说明https://www.elastic.co/guide/en/elasticsearch/guide/master/_index_time_search_as_you_type.html

      【讨论】:

      • 我刚试过你的方法,不幸的是它没有返回全名的部分匹配
      猜你喜欢
      • 1970-01-01
      • 2021-06-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-26
      • 2021-11-23
      • 2012-02-12
      • 2017-01-27
      • 1970-01-01
      相关资源
      最近更新 更多