gpt4 book ai didi

elasticsearch - 搜索文档在ElasticSearch的列表中至少有一个单词

转载 作者:行者123 更新时间:2023-12-03 01:18:15 25 4
gpt4 key购买 nike

我想用以下方式搜索文档:1)在三个字段之一中必须存在的某些短语2)和一个列表词,其中至少一个出现在其中一个字段中,例如['supply','procure', '采购']。

以下是我使用的满足第一个要求的当前ES查询。但是,如何在此查询中添加单词列表?

{
"query": {
"bool": {
"must": [
{
"multi_match": {
"query": "ford",
"fields": [
"title",
"description",
"news_content"
]
}
},
{
"multi_match": {
"query": "lone star",
"fields": [
"title",
"description",
"news_content"
],
"type": "phrase"
}
}
]
}
}
}

最佳答案

您快到了,只需在查询中添加运算符OR,即可解决list words in which at least one of them occurs in one of the fields,的第二个用例

让我举例说明一下:

索引定义

{
"mappings" :{
"properties" :{
"title" :{
"type" : "text"
},
"description":{
"type" : "text"
}
}
}
}

索引样本文档
{
"title" : "foo",
"description": "opster"
}
{
"title" : "bar",
"description": "stackoverflow"
}
{
"title" : "baz",
"description": "nodesc"
}

搜索查询,请注意我正在搜索foo amit,单词列表,因此其中至少一个单词应与 2个字段中的任何一个匹配
{
"query": {
"bool": {
"should": {
"multi_match": {
"query": "foo amit",
"fields": [
"title",
"description"
],
"operator": "or" --> notice operator OR
}
}
}
}
}

搜索结果
"hits": [
{
"_index": "white",
"_type": "_doc",
"_id": "1",
"_score": 0.9808291,
"_source": {
"title": "foo", --> notice this match as `foo` is present and we used opertor OR in query.
"description": "opster"
}
}
]

关于elasticsearch - 搜索文档在ElasticSearch的列表中至少有一个单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61469101/

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