gpt4 book ai didi

elasticsearch - 如何在 Elasticsearch 中必须或必须使用多个匹配短语?

转载 作者:行者123 更新时间:2023-12-03 02:23:26 25 4
gpt4 key购买 nike

如何在 Elasticsearch 中必须或必须使用多个匹配短语?

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上部分匹配

但是它不起作用。

添加评论

GETquestiondetails_new / question / _search? {“query”:{“bool”:{“should”:[{“match_phrase”:{“tags.keyword”:“azure-data-factory”}},{“match_phrase”:{“title”:“azure -data-factory“}}],” minimum_should_match“:1,” filter“:{” range“:{” creation_date“:{” gte“:1585170170,” lte“:1585170180}}}

在此查询中,我需要所有与azure-data-factory完全匹配或在其标题(String)中具有azure-data-factory的文档。应该是or或search。
但是它与具有azure-data-factory-2值的标签的匹配也

最佳答案

添加一个char过滤器,该过滤器在建立索引时会将“-”替换为“_”。您无需更改输入文本(它将与“-”一起使用)

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个 token [“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”分析相同的文本,那么将生成一个 token
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
}
]

关于elasticsearch - 如何在 Elasticsearch 中必须或必须使用多个匹配短语?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61769241/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com