很抱歉,如果这是一个简单的问题,但我无法自己提出一个查询或在网上找到一个答案。
如果我的elasticsearch索引中有一个字段“分类”,它使用path_hierarchy令牌程序来存储分类法结构,那么有什么方法可以查询和匹配该路径中的任何单词吗?
PUT /stuff
{
"settings": {
"analysis": {
"analyzer": {
"categories": {
"tokenizer": "path_hierarchy"
}
}
}
}
}
PUT /stuff/_mapping/item
{
"properties": {
"name": {
"type": "string",
"index": "not_analyzed"
},
"category": {
"type": "string",
"index": "not_analyzed",
"fields": {
"tree": {
"type": "string",
"analyzer": "categories"
}
}
}
}
}然后我放了几个东西:
PUT /stuff/item/1 {"name": "The Killer Books", "category": "Entertainment/Movie/Horror"}
PUT /stuff/item/2 {"name": "The Adventure", "category": "Entertainment/Books And Stories/Fantasy"}然后我有一个用户搜索‘图书’。它只是自由形式的文本,我没有任何额外的参数可以帮助确定类别。我想要一部名为“杀手书”的电影,但也想得到“娱乐/书籍和故事”下的文件。
如果用户只搜索“故事”,我也想返回第二个文档。
我的问题是,我很难从搜索词中很容易地看出,在路径结构中,这个词可能落在哪里。是否有适当的方法来查询这个问题?我尝试过以下查询,但没有成功:
GET /stuff/item/_search
{
"query":{
"multi_match":{
"query":"Books",
"fields":["title", "category"]
}
}
}发布于 2015-12-02 01:48:58
我不知道你为什么要使用path_hierarchy tokenizer。要使给定的查询正常工作,可以像这样使用standard analyzer映射字段
PUT /stuff/_mapping/item
{
"properties": {
"name": {
"type": "string" <--- using default standard analyzer
},
"category": {
"type": "string" <---- using default standard analyzer
"fields": {
"tree": {
"type": "string",
"analyzer": "categories"
}
}
}
}
}现在您的查询
GET /stuff/item/_search
{
"query":{
"multi_match":{
"query":"Books",
"fields":["name", "category"]
}
}
}会给你们两个文件。我删除了"index" : "not_analyzed",因为它将按原样对字符串进行索引,因此Killer将按原样进行索引,但是使用standard analyzer,它将生成三个令牌,即、杀手、书籍,因此您的查询将正常工作。
使用分析端点查看如何对数据进行索引,这样就可以轻松地编写查询。
我希望这能帮到你。
https://stackoverflow.com/questions/34033417
复制相似问题