编辑:要补充一点,同义词似乎可以与基本的查询字符串查询一起使用。
"query_string" : { "default_field" : "location.region.name.raw", "query" : "nh" }
这将返回新罕布什尔州的所有结果,但对“ nh”的“匹配”查询将不返回结果。
我正在尝试将同义词添加到我的Elastic索引中的位置字段中,这样,如果我对“质量”,“马”或“马萨诸塞州”进行位置搜索,则每次都会得到相同的结果。我将同义词过滤器添加到设置中,并更改了位置映射。这是我的设置:
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个结果。为什么不起作用?
过滤器的顺序为
filter":[ "lowercase", "synonym_filter" ]
因此,如果elasticsearch首先将令牌“降低”,则在执行第二步时synonym_filter,它将与您定义的任何条目都不匹配。
synonym_filter
为了解决这个问题,我将用小写字母定义同义词