Elasticsearch同义词分析器无法正常工作

时间:2015-03-30 19:11:09

标签: filter analyzer synonym elasticsearch

编辑:要添加到此,同义词似乎正在使用基本的查询字符串查询。

"query_string" : {
    "default_field" : "location.region.name.raw",
    "query" : "nh"
}

这将返回New Hampshire的所有结果,但“nh”的“匹配”查询不返回任何结果。


我正在尝试将同义词添加到我的弹性索引中的位置字段中,这样如果我搜索“Mass”,“Ma”或“Massachusetts”,我每次都会得到相同的结果。我将同义词过滤器添加到我的设置中并更改了位置的映射。以下是我的设置:

analysis":{
    "analyzer":{
        "synonyms":{
            "filter":[
                "lowercase",
                "synonym_filter"
            ],
        "tokenizer": "standard"
    }
},
"filter":{
    "synonym_filter":{
        "type": "synonym",
        "synonyms":[
            "United States,US,USA,USA=>usa",
            "Alabama,Al,Ala,Ala",
            "Alaska,Ak,Alas,Alas",
            "Arizona,Az,Ariz",
            "Arkansas,Ar,Ark",
            "California,Ca,Calif,Cal",
            "Colorado,Co,Colo,Col",
            "Connecticut,Ct,Conn",
            "Deleware,De,Del",
            "District of Columbia,Dc,Wash Dc,Washington Dc=>Dc",
            "Florida,Fl,Fla,Flor",
            "Georgia,Ga",
            "Hawaii,Hi",
            "Idaho,Id,Ida",
            "Illinois,Il,Ill,Ills",
            "Indiana,In,Ind",
            "Iowa,Ia,Ioa",
            "Kansas,Kans,Kan,Ks",
            "Kentucky,Ky,Ken,Kent",
            "Louisiana,La",
            "Maine,Me",
            "Maryland,Md",
            "Massachusetts,Ma,Mass",
            "Michigan,Mi,Mich",
            "Minnesota,Mn,Minn",
            "Mississippi,Ms,Miss",
            "Missouri,Mo",
            "Montana,Mt,Mont",
            "Nebraska,Ne,Neb,Nebr",
            "Nevada,Nv,Nev",
            "New Hampshire,Nh=>Nh",
            "New Jersey,Nj=>Nj",
            "New Mexico,Nm,N Mex,New M=>Nm",
            "New York,Ny=>Ny",
            "North Carolina,Nc,N Car=>Nc",
            "North Dakota,Nd,N Dak, NoDak=>Nd",
            "Ohio,Oh,O",
            "Oklahoma,Ok,Okla",
            "Oregon,Or,Oreg,Ore",
            "Pennsylvania,Pa,Penn,Penna",
            "Rhode Island,Ri,Ri & PP,R Isl=>Ri",
            "South Carolina,Sc,S Car=>Sc",
            "South Dakota,Sd,S Dak,SoDak=>Sd",
            "Tennessee,Te,Tenn",
            "Texas,Tx,Tex",
            "Utah,Ut",
            "Vermont,Vt",
            "Virginia,Va,Virg",
            "Washington,Wa,Wash,Wn",
            "West Virginia,Wv,W Va, W Virg=>Wv",
            "Wisconsin,Wi,Wis,Wisc",
            "Wyomin,Wi,Wyo"
        ]
    }
}

location.region字段的映射:

"region":{
    "properties":{
        "id":{"type": "long"},
        "name":{
            "type": "string",
            "analyzer": "synonyms",
            "fields":{"raw":{"type": "string", "index": "not_analyzed" }}
        }
    }
}

但同义词分析器似乎没有做任何事情。此查询例如:

"match" : {
    "location.region.name" : {
        "query" : "Massachusetts",
        "type" : "phrase",
        "analyzer" : "synonyms"
    }
}

这会返回数百个结果,但如果我将“Massachusetts”替换为“Ma”或“Mass”,我会得到0个结果。为什么不起作用?

2 个答案:

答案 0 :(得分:13)

过滤器的顺序是

filter":[
    "lowercase",
    "synonym_filter"
]

因此,如果弹性搜索是"低级"首先是令牌,当它执行第二步synonym_filter时,它不会匹配您定义的任何条目。

要解决这个问题,我会用小写

定义同义词

答案 1 :(得分:2)

您还可以将同义词过滤器定义为不区分大小写:


    "filter":{
        "synonym_filter":{
            "type": "synonym",
            "ignore_case" : "true",
            "synonyms":[
                ...
            ]
        }
    }