如何在弹性搜索中使用多个匹配短语?
GET questiondetails/question/_search
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"tags.keyword": "azure-data"
}
},
{
"match_phrase": {
"body": "azure-data"
}
},
{
"match_phrase": {
"title": "azure-data"
}
},
{
"match_phrase": {
"answers.body": "azure-data"
}
}
],
"minimum_should_match": 1,
"filter": {
"range": {
"creation_date": {
"gte": 1584748800,
"lte": 1585612800
}
}
}
}
},
"size": "10000"
}已尝试此查询....我需要的结果,在那里的词完全匹配的标签或部分匹配的正文或标题或answer.body
但它不起作用。
添加评论
获取问题详细信息_new/问题/_搜索?{“查询”:{“布尔”:{“应该”:{ "match_phrase":{ "tags.keyword":“蓝色数据工厂”} },{ "match_phrase":{“标题”:“蓝色数据工厂”}},"minimum_should_match":1,"filter":{ "range":{ "creation_date":{ "gte":1585170170,"lte":1585170180 }}}
在这个查询中,我需要精确匹配为azure-data-factory或titile (字符串)中包含azure-data-factory的所有文档。它应该是一个或搜索。但它也与值为azure-data-factory-2的标记匹配
发布于 2020-05-13 17:27:05
添加一个字符过滤器,它将在索引时将"-“替换为"_”。您不需要更改输入文本(它将与“-”一起工作)
PUT index38
{
"settings": {
"analysis": {
"char_filter": {
"my_char_filter": {
"type": "mapping",
"mappings": [
"- => _"
]
}
},
"analyzer": {
"my_analyzer": {
"tokenizer": "standard",
"char_filter": [
"my_char_filter"
],
"filter": [
"lowercase"
]
}
}
}
},
"mappings": {
"properties": {
"tags": {
"type": "text",
"analyzer": "my_analyzer",
"fields": {
"keyword":{
"type":"keyword"
}
}
},
"title": {
"type": "text",
"analyzer": "my_analyzer",
"fields": {
"keyword":{
"type":"keyword"
}
}
}
}
}
}查询:
{
"query": {
"bool": {
"should": [
{
"match_phrase": {
"tags": "azure-data-factory"
}
},
{
"bool": {
"must_not": [
{
"match_phrase": {
"tags": "azure-data-factory"
}
}
],
"should": [
{
"match_phrase": {
"body": "azure-data-factory"
}
},
{
"match_phrase": {
"title": "azure-data-factory"
}
},
{
"match_phrase": {
"answers.body": "azure-data-factory"
}
}
]
}
}
],
"minimum_should_match": 1
}
},
"size": "100"
}如果您将使用默认分析器分析文本"azure-data-factory“,它将生成3个标记"azure","data","factory”
GET index38/_analyze
{
"text": "azure-data-factory"
}
Result:
"tokens" : [
{
"token" : "azure",
"start_offset" : 0,
"end_offset" : 5,
"type" : "<ALPHANUM>",
"position" : 0
},
{
"token" : "data",
"start_offset" : 6,
"end_offset" : 10,
"type" : "<ALPHANUM>",
"position" : 1
},
{
"token" : "factory",
"start_offset" : 11,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 2
}
]如果您将使用"my_analyzer“分析相同的文本,则会生成一个标记
GET index38/_analyze
{
"text": "azure-data-factory",
"analyzer": "my_analyzer"
}
Result:
"tokens" : [
{
"token" : "azure_data_factory",
"start_offset" : 0,
"end_offset" : 18,
"type" : "<ALPHANUM>",
"position" : 0
}
]https://stackoverflow.com/questions/61769241
复制相似问题