gpt4 book ai didi

elasticsearch - 在Elasticsearch中搜索具有不同映射的两种不同类型

转载 作者:行者123 更新时间:2023-12-03 00:59:09 25 4
gpt4 key购买 nike

具有两个类型testeritems的索引items_two的以下映射:

curl -XPUT 'localhost:9200/tester?pretty=true' -d '{
"mappings": {
"items": {
"properties" : {
"body" : { "type": "string" }
}},
"items_two": {
"properties" : {
"body" : { "type": "string" },
"publised" : { "type": "integer"}
}}}}'

我在上面加上了三个要素。
curl -XPUT 'localhost:9200/tester/items/1?pretty=true' -d '{
"body" : "Hey there im reading a book"
}'

curl -XPUT 'localhost:9200/tester/items_two/1?pretty=true' -d '{
"body" : "I love the new book of my brother",
"publised" : 0
}'

curl -XPUT 'localhost:9200/tester/items_two/2?pretty=true' -d '{
"body" : "Stephen kings book is very nice",
"publised" : 1
}'

我需要进行一个查询,该查询与单词 book匹配并且具有 published = 1,并且在映射上没有 published,但是在其上具有 book(作为 items的唯一项)。

通过以下查询,我只能与 "Stephen kings book is very nice"项匹配(很明显)。
curl -XGET 'localhost:9200/tester/_search?pretty=true' -d '{
"query": {
"bool": {
"must": [
{
"match": { "body": "book" }
},
{
"match": { "publised": "1" }
}]
}}}'

如果我搜索字符串 book,则我想要的输出应与 items类型( "Hey there im reading a book")中的项目#1和 item类型( items_two)中的 "Stephen kings book is very nice"#2相匹配。

我不想更改映射或其他内容,我需要通过一个查询将其归档,那么如何构建查询?

提前致谢。

最佳答案

您可以使用_type字段进行此类搜索。尝试以下查询

{
"query": {
"bool": {
"should": [
{
"bool": {
"must": [
{
"match": {
"body": "text"
}
},
{
"match": {
"publised": "1"
}
}
],
"filter": {
"term": {
"_type": "items_two"
}
}
}
},
{
"bool": {
"must": [
{
"match": {
"body": "text"
}
}
],
"filter": {
"term": {
"_type": "items"
}
}
}
}
]
}
}
}

关于elasticsearch - 在Elasticsearch中搜索具有不同映射的两种不同类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40179739/

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