Elasticsearch | copy_to与部分搜索

时间:2017-01-23 23:24:18

标签: search elasticsearch mapping settings partial

我有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:

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"
      }
    }
  }
}

...我没有得到结果:(我希望部分匹配也适用于full_name。另外我不能成功地在名字和姓氏上进行部分匹配。它只是full_name,它不起作用我怎么会这样呢?

2 个答案:

答案 0 :(得分:4)

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

两者都会按预期工作。

答案 1 :(得分:1)

只需从您的映射中删除"search_analyzer": "standard",只有在某些用例中才需要它,例如自动填充搜索。请参阅此处的说明https://www.elastic.co/guide/en/elasticsearch/guide/master/_index_time_search_as_you_type.html