1

我对弹性搜索有一些奇怪的行为。我正在使用带有自定义标记器的自定义分析器,它会在空格、+、- 的情况下拆分单词。

当我在寻找

{
  "query": {
    "match_phrase_prefix": {
      "name.default": {
        "query": "paris oly"
      }
    }
  }
}

我得到了巴黎奥林匹亚等预期的结果......但是当我搜索时

{
  "query": {
    "match_phrase_prefix": {
      "name.default": {
        "query": "paris ol"
      }
    }
  }
}

我根本没有得到任何结果。

设置:

     "analysis": {
           "analyzer": {
              "customAnalyzer": {
                 "type": "custom",
                 "filter": "lowercase",
                 "tokenizer": "customTokenizer"
              },
           "tokenizer": {
              "customTokenizer": {
                 "pattern": "[\\+\\s-]",
                 "type": "pattern"
              }
           }
        }

字段映射:

{
    "name": {
              "properties": {
                        "default": {
                        "type": "string",
                        "analyzer": "customAnalyzer"
                 }
            }
        }
}

部分文档样本(请求的字段):

 { 
"name": {
              "jp": "パリ オリンピア (劇場)",
              "default": "Paris Olympia",
              }
}

{    
    "TYPE_NAME": {
      "dynamic_templates": [
        {
          "name": {
            "path_match": "*name.*",
            "match_mapping_type": "string",
            "mapping": {
              "type": "string",
              "analyzer": "customAnalyzer"
            }
          }
        }
      ],
      "properties": {
        "point": {
          "type": "geo_point"
        }
      }
     }
}
4

1 回答 1

1

当我尝试测试您发布的内容时,它对我有用。我会发布我所做的,您可以查看它,看看您是否可以弄清楚您的设置有什么不同,如果您有其他问题,我会尽力提供帮助。

我使用您发布的映射和分析器/标记器创建了一个索引,然后添加了您发布的文档:

DELETE /test_index

PUT /test_index
{
   "settings": {
      "number_of_shards": 1,
      "number_of_replicas": 0,
      "analysis": {
         "tokenizer": {
            "customTokenizer": {
               "pattern": "[\\+\\s-]",
               "type": "pattern"
            }
         },
         "analyzer": {
            "customAnalyzer": {
               "type": "custom",
               "filter": "lowercase",
               "tokenizer": "customTokenizer"
            }
         }
      }
   },
   "mappings": {
      "doc": {
         "properties": {
            "name": {
               "properties": {
                  "default": {
                     "type": "string",
                     "analyzer": "customAnalyzer"
                  }
               }
            }
         }
      }
   }
}

PUT /test_index/doc/1
{
   "name": {
      "jp": "パリ オリンピア (劇場)",
      "default": "Paris Olympia"
   }
}

然后,您发布的任何一个查询都为我返回了文档:

POST /test_index/_search
{
   "query": {
      "match_phrase_prefix": {
         "name.default": {
            "query": "paris oly"
         }
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.38356602,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.38356602,
            "_source": {
               "name": {
                  "jp": "パリ オリンピア (劇場)",
                  "default": "Paris Olympia"
               }
            }
         }
      ]
   }
}

或者

POST /test_index/_search
{
   "query": {
      "match_phrase_prefix": {
         "name.default": {
            "query": "paris ol "
         }
      }
   }
}
...
{
   "took": 1,
   "timed_out": false,
   "_shards": {
      "total": 1,
      "successful": 1,
      "failed": 0
   },
   "hits": {
      "total": 1,
      "max_score": 0.38356602,
      "hits": [
         {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 0.38356602,
            "_source": {
               "name": {
                  "jp": "パリ オリンピア (劇場)",
                  "default": "Paris Olympia"
               }
            }
         }
      ]
   }
}

为方便起见,这是我使用的代码:

http://sense.qbox.io/gist/4e58344580dcf01299f7cc2199d0fb7694d2a051

所以肯定有其他事情发生。您能说出您的设置与我尝试的设置有何不同吗?

编辑:我确实必须切换标记器和分析器的顺序,否则会出错。所以你可能想调查一下。

于 2015-02-05T17:16:10.897 回答