弹性搜索 | copy_to 部分搜索

Elasticsearch | copy_to with partial searching

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

设置:

{
   "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:

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

现在当我搜索时...

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

...我得到结果了!!万岁,但如果我搜索....

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

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

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

你的映射有一个小错误,你需要将名字和姓氏复制到 vital.full_name 字段中,而不仅仅是 full_name,否则会创建一个名为 full_name 在映射的 top-level 处使用标准分析器(如果您 运行 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"
      }
    }
  }
}

两者都会如您所愿。