同义词分析器不工作

时间:2016-08-25 14:38:42

标签: elasticsearch

以下是我的设置:

{
"countries": {
"aliases": {},
"mappings": {
  "country": {
    "properties": {
      "countryName": {
        "type": "string"
      }
    }
  }
},
"settings": {
  "index": {
    "creation_date": "1472140045116",
    "analysis": {
      "filter": {
        "synonym": {
          "ignore_case": "true",
          "type": "synonym",
          "synonyms_path": "synonym.txt"
        }
      },
      "analyzer": {
        "synonym": {
          "filter": [
            "synonym"
          ],
          "tokenizer": "whitespace"
        }
      }
    },
    "number_of_shards": "5",
    "number_of_replicas": "1",
    "uuid": "7-fKyD9aR2eG3BwUNdadXA",
    "version": {
      "created": "2030599"
    }
  }
},
"warmers": {}
}
}

我的synonym.txt文件位于主config文件夹内的elasticsearch文件夹中。

这是我的问题:

query: {
    query_string: {
        fields: ["countryName"],
        default_operator: "AND",
        query: searchInput,
        analyzer: "synonym"
        }
     }

synonym.txt中的字词是:我们,美国,美国

所以这不起作用。有趣的是,当我输入synonym.txt文件中的任何单词时,搜索正常,除了。例如,当我通常在搜索中输入 us 时,我会得到结果。使用此分析仪,我们不会给我任何东西。

我已经完成了closeopen到我的ES服务器,但它仍然无法正常工作。

修改

document的一个例子:

{
    "_index": "countries",
    "_type": "country",
    "_id": "57aabeb80057405968de152b",
    "_score": 1,
    "_source": {
      "countryName": "United States"
    }

searchInput的示例(这来自前端):

united states

编辑#2

这是我更新的索引配置文件:

{
"countries": {
    "aliases": {},
    "mappings": {},
    "settings": {
        "index": {
            "number_of_shards": "5",
            "creation_date": "1472219634083",
            "analysis": {
                "filter": {
                    "synonym": {
                        "ignore_case": "true",
                        "type": "synonym",
                        "synonyms_path": "synonym.txt"
                    }
                },
                "analyzer": {
                    "synonym": {
                        "filter": [
                            "synonym"
                        ],
                        "tokenizer": "whitespace"
                    }
                }
            },
            "country": {
                "properties": {
                    "countryName": {
                        "type": "string",
                        "analyzer": "synonym"
                    },
                    "number_of_replicas": "1",
                    "uuid": "50ZwpIVFTqeD_rJxlmd59Q",
                    "version": {
                        "created": "2030599"
                    }
                }
            },
            "warmers": {}
        }
    }
}
}

当我尝试添加文档并对所述文档进行搜索时,synonym分析器对我不起作用。

编辑#3

以下是索引中的2个文档:

{
"took": 3,
"timed_out": false,
"_shards": {
    "total": 5,
    "successful": 5,
    "failed": 0
},
"hits": {
    "total": 2,
    "max_score": 1,
    "hits": [{
        "_index": "stocks",
        "_type": "stock",
        "_id": "2",
        "_score": 1,
        "_source": {
            "countryName": "United States"
        }
    }, {
        "_index": "stocks",
        "_type": "stock",
        "_id": "1",
        "_score": 1,
        "_source": {
            "countryName": "Canada"
        }
    }]
}
}

2 个答案:

答案 0 :(得分:2)

你很接近,但我建议仔细阅读this section from the documentation以更好地了解这项功能。

作为解决方案:

PUT /countries
{
  "mappings": {
    "country": {
      "properties": {
        "countryName": {
          "type": "string",
          "analyzer": "synonym"
        }
      }
    }
  },
  "settings": {
    "analysis": {
      "filter": {
        "synonym": {
          "ignore_case": "true",
          "type": "synonym",
          "synonyms_path": "synonym.txt"
        }
      },
      "analyzer": {
        "synonym": {
          "filter": [
            "lowercase",
            "synonym"
          ],
          "tokenizer": "whitespace"
        }
      }
    }
  }
}

您需要删除索引并使用上面的映射再次创建它。 然后使用此查询:

  "query": {
    "query_string": {
      "fields": [
        "countryName"
      ],
      "default_operator": "AND",
      "query": "united states"
    }
  }

答案 1 :(得分:0)

按下txt后删除/创建了索引吗?

如果您使用"synonyms": ""

,我认为您应该删除"synonyms_path"