1 回答
TA贡献1807条经验 获得超9个赞
分开doc_title&是不好的做法doc_content——你并没有真正减少任何东西。
跟着这个:
docs = [
{"doc_id": 1, "doc_title": "good", "doc_content": ["a", "b"]},
{"doc_id": 2, "doc_title": "good", "doc_content": ["c", "d"]},
{"doc_id": 3, "doc_title": "bad", "doc_content": ["a", "e"]}
]
for doc in docs:
r = requests.post(f'http://127.0.0.1:9200/{index}/_doc', json=doc)
并且您的查询将按预期工作。a无论如何都b应该被共享doc_id=1,不是吗?
更新——使contents语法嵌套
PUT test
{
"mappings": {
"properties": {
"contents": {
"type": "nested",
"properties": {
"doc_content": {
"type": "text"
}
}
},
"doc_id": {
"type": "keyword"
},
"doc_title": {
"type": "text"
}
}
}
}
POST test/_doc
{
"doc_id": 1,
"doc_title": "good",
"contents": [
{"doc_content": "a"},
{"doc_content": "b"}
]
}
GET test/_search
{
"_source": ["doc_title", "inner_hits"],
"query": {
"bool": {
"must": [
{
"match": {
"doc_title": "good"
}
},
{
"nested": {
"path": "contents",
"query": {
"match": {
"contents.doc_content": "a"
}
},
"inner_hits": {}
}
}
]
}
}
}
屈服
[
{
"_index":"test",
"_type":"_doc",
"_id":"sySOoXEBdiyDG0RsIq21",
"_score":0.98082924,
"_source":{
"doc_title":"good" <------
},
"inner_hits":{
"contents":{
"hits":{
"total":1,
"max_score":0.6931472,
"hits":[
{
"_index":"test",
"_type":"_doc",
"_id":"sySOoXEBdiyDG0RsIq21",
"_nested":{
"field":"contents",
"offset":0
},
"_score":0.6931472,
"_source":{
"doc_content":"a" <-----
}
}
]
}
}
}
}
]
添加回答
举报