gpt4 book ai didi

Elasticsearch:全文搜索

转载 作者:行者123 更新时间:2023-12-03 02:36:07 27 4
gpt4 key购买 nike

我正在尝试在多个字段上使用以下文本“Gold Cartier watch ”构建一个 Elasticsearch 全文搜索查询。

我必须遵循这条规则:首先找到所有“黄金”文件。从检索“Gold”文件中,找到所有“Cartier”文件并从中找到所有“watches”文件。

这是我的multi_match询问:

{
"query": {
"multi_match": {
"query": "Fred or rose",
"fields": [
"name",
"status",
"categories.name",
"brand.name",
"reference"
]
}
}
}

有我的映射
{
"product": {
"mappings": {
"product": {
"dynamic_date_formats": [],
"properties": {
"available": {
"type": "text"
},
"brand": {
"properties": {
"available": {
"type": "text"
},
"name": {
"type": "keyword"
},
"shopProductBrands": {
"properties": {
"available": {
"type": "text"
},
"priority": {
"type": "integer"
},
"slug": {
"type": "keyword"
}
}
},
"slug": {
"type": "keyword"
}
}
},
"categories": {
"type": "nested",
"properties": {
"available": {
"type": "text"
},
"brand": {
"properties": {
"available": {
"type": "text"
},
"name": {
"type": "keyword"
},
"slug": {
"type": "keyword"
}
}
},
"name": {
"type": "keyword"
},
"parent": {
"type": "keyword"
},
"slug": {
"type": "keyword"
}
}
},
"createdAt": {
"type": "date",
"format": "date_time_no_millis"
},
"longDescription": {
"type": "text",
"analyzer": "french_search"
},
"name": {
"type": "text",
"boost": 15,
"fields": {
"raw": {
"type": "keyword"
}
},
"analyzer": "french_search"
},
"purchasePrice": {
"type": "double"
},
"rawPrice": {
"type": "double"
},
"reference": {
"type": "keyword",
"boost": 10
},
"shortDescription": {
"type": "text",
"boost": 3,
"analyzer": "french_search"
},
"slug": {
"type": "keyword"
},
"status": {
"type": "text"
},
"updatedAt": {
"type": "date",
"format": "date_time_no_millis"
}
}
}
}
}
}

我的搜索将检索所有“Gold”、“Cartier”和“watches”文件的组合。

如何构建遵循我的规则的查询?

谢谢

最佳答案

我不确定是否有一个简单的解决方案。我认为你能得到的最接近的是使用 cross_fields"operator": "and"并且只搜索具有相同分析器的字段。您可以添加每个字段的“french_search”版本吗?

cross_fields analyzes the query string into individual terms, then looks for each term in any of the fields, as though they were one big field.



然而:

The cross_field type can only work in term-centric mode on fields that have the same analyzer. ... If there are multiple groups, they are combined with a bool query.



所以这个查询:
{
"query": {
"multi_match": {
"type": "cross_fields",
"query": "gold Cartier watches",
"fields": [
"name",
"status",
"categories.name",
"brand.name",
"reference"
]
}
}
}

会变成这样:
"query": {
"bool": {
"should": [
{
"multi_match": {
"query": "gold Cartier watches",
"fields": ["name"]
}
},
{
"multi_match": {
"query": "gold Cartier watches",
"fields": ["status"]
}
},
{
"multi_match": {
"query": "gold Cartier watches",
"fields": [
"categories.name",
"brand.name",
"reference"
]
}
}
]
}
}

该查询过于松散,但添加 "operator": "and""minimum_should_match": "100%"太严格了。

它既不美观也不高效,但您可以进行应用程序端术语解析并构建 bool 查询。像这样的东西:
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "gold",
"fields": [
"name",
"status",
...
"reference"
]
}
},
{
"multi_match": {
"query": "Cartier",
"fields": [
"name",
"status",
...
"reference"
]
}
}
...
]
}
}

关于Elasticsearch:全文搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58858480/

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