gpt4 book ai didi

elasticsearch - 带有嵌套对象的Elasticsearch查询

转载 作者:行者123 更新时间:2023-12-02 22:35:47 25 4
gpt4 key购买 nike

我有一个带有嵌套映射的索引。
我想执行一个查询,该查询将返回以下内容:给我所有文档,其中搜索词中的每个单词都出现在一个或多个嵌套文档中。

这是索引:

properties: {
column_values_index_as_objects: {
type: "nested",
properties: {
value: {
ignore_above: 256,
type: 'keyword',
fields: {
word_middle: {
analyzer: "searchkick_word_middle_index",
type: "text"
},
analyzed: {
term_vector: "with_positions_offsets",
type: "text"
}
}
}
}
}
}

这是我尝试的最新查询:
nested: {
path: "column_values_index_as_objects",
query: {
bool: {
must: [
{
match: {
"column_values_index_as_objects.value.analyzed": {
query: search_term,
boost: 10 * boost_factor,
operator: "or",
analyzer: "searchkick_search"
}
}
}

例如,如果我搜索“食品和水”一词,我希望每个词至少出现在嵌套文档中。
即使只有一个单词存在,当前搜索也会返回文档

谢谢您的帮助!

更新:
正如克里斯托弗(Cristoph)所建议的那样,该解决方案有效。现在我有以下问题。

这是我的索引:
  properties: {
name: {
type: "text"
},
column_values_index_as_objects: {
type: "nested",
properties: {
value: {
ignore_above: 256,
type: 'keyword',
fields: {
word_middle: {
analyzer: "searchkick_word_middle_index",
type: "text"
},
analyzed: {
term_vector: "with_positions_offsets",
type: "text"
}
}
}
}
}
}

我要执行的查询是,如果我搜索“我的名字是男的”,​​它将给出所有单词都找到的所有文档-可能在嵌套文档中,也可能在名称字段中。
例如,我可能在名称字段中有一个值为'guy'的文档,而在嵌套文档中有其他单词

最佳答案

为了做到这一点,我通常会拆分条款并生成这样的请求(foo:bar是另一个字段上的另一个条件):

{
"bool": {
"must": [
{
"nested": {
"path": "column_values_index_as_objects",
"query": {
"match": {
"column_values_index_as_objects.value.analyzed": {
"query": "food",
"boost": "10 * boost_factor",
"analyzer": "searchkick_search"
}
}
}
}
},
{
"nested": {
"path": "column_values_index_as_objects",
"query": {
"match": {
"column_values_index_as_objects.value.analyzed": {
"query": "and",
"boost": "10 * boost_factor",
"analyzer": "searchkick_search"
}
}
}
}
},
{
"nested": {
"path": "column_values_index_as_objects",
"query": {
"match": {
"column_values_index_as_objects.value.analyzed": {
"query": "water",
"boost": "10 * boost_factor",
"analyzer": "searchkick_search"
}
}
}
}
},
{
"query": {
"term": {
"foo": "bar"
}
}
}
]
}
}

关于elasticsearch - 带有嵌套对象的Elasticsearch查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53904666/

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