如何匹配多词项?
How to match multiword terms?
我需要创建一些更复杂的查询。除了匹配 html
字段中的一些随机文本外,我还需要至少匹配列表中的 keywords
之一。当列表仅包含一个单词字符串时有效,但它不会在列表中注册多个单词字符串。我不能将它们拆分成单独的词,因为这可能会影响结果。
这是我目前尝试的方法。
{
"from":page_num * size - size,
"size": size,
"query":{
"bool":{
"must":[
{
"match":{
"html":{
"query":"some query",
"operator":"and"
}
}
},
{
"terms":{
"keywords":[
"word",
"two words",
"another words"
]
}
}
]
}
}
}
如果您没有明确定义任何映射,那么您需要将 .keyword 添加到关键字字段。这使用关键字分析器而不是标准分析器(注意关键字字段后的“.keyword”)。
{
"from":page_num * size - size,
"size": size,
"query":{
"bool":{
"must":[
{
"match":{
"html":{
"query":"some query",
"operator":"and"
}
}
},
{
"terms":{
"keywords.keyword":[
"word",
"two words",
"another words"
]
}
}
]
}
}
}
但是如果你想存储文本和关键字类型的关键字字段,那么你可以update your index mapping as shown below to use multi fields
PUT /_mapping
{
"properties": {
"keywords": {
"type": "keyword",
"fields": {
"raw": {
"type": "text"
}
}
}
}
}
然后重新索引数据。
我需要创建一些更复杂的查询。除了匹配 html
字段中的一些随机文本外,我还需要至少匹配列表中的 keywords
之一。当列表仅包含一个单词字符串时有效,但它不会在列表中注册多个单词字符串。我不能将它们拆分成单独的词,因为这可能会影响结果。
这是我目前尝试的方法。
{
"from":page_num * size - size,
"size": size,
"query":{
"bool":{
"must":[
{
"match":{
"html":{
"query":"some query",
"operator":"and"
}
}
},
{
"terms":{
"keywords":[
"word",
"two words",
"another words"
]
}
}
]
}
}
}
如果您没有明确定义任何映射,那么您需要将 .keyword 添加到关键字字段。这使用关键字分析器而不是标准分析器(注意关键字字段后的“.keyword”)。
{
"from":page_num * size - size,
"size": size,
"query":{
"bool":{
"must":[
{
"match":{
"html":{
"query":"some query",
"operator":"and"
}
}
},
{
"terms":{
"keywords.keyword":[
"word",
"two words",
"another words"
]
}
}
]
}
}
}
但是如果你想存储文本和关键字类型的关键字字段,那么你可以update your index mapping as shown below to use multi fields
PUT /_mapping
{
"properties": {
"keywords": {
"type": "keyword",
"fields": {
"raw": {
"type": "text"
}
}
}
}
}
然后重新索引数据。