gpt4 book ai didi

elasticsearch - 使用$ .getJSON的Elasticsearch查询

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

由于实现要求,我正在使用$ .getJSON语句查询以下名为job6的elasticsearch索引。

{
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1.0,
"hits": [{
"_index": "job6",
"_type": "_doc",
"_id": "2e2ed07786c01e9fd845b62fb86a693a",
"_score": 1.0,
"_source": {
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain",
"created": "2020-04-03T11:57:45.673+0000",
"last_modified": "2020-02-27T16:15:27.795+0000",
"last_accessed": "2020-04-03T11:57:45.673+0000",
"indexing_date": "2020-04-14T13:56:51.668+0000",
"filesize": 0,
"filename": "fichero0_borrador.txt",
"url": "file://C:\\TMPM\\carpeta0\\99.Borrador\\fichero0_borrador.txt"
},
"path": {
"root": "4cface21f575d5e5985e4e3889c278a3",
"virtual": "PM/carpeta0/99.Borrador/fichero0_borrador.txt",
"real": "C:\\TMPM\\carpeta0\\99.Borrador\\fichero0_borrador.txt"
}
}
}, {
"_index": "job6",
"_type": "_doc",
"_id": "6f475ff7ac21fc59f4edc34761d09",
"_score": 1.0,
"_source": {
"content": "cadena0 A\n",
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain; charset=ISO-8859-1",
"created": "2020-04-14T09:30:20.218+0000",
"last_modified": "2020-04-14T09:46:02.930+0000",
"last_accessed": "2020-04-14T09:46:02.930+0000",
"indexing_date": "2020-04-14T13:56:52.223+0000",
"filesize": 9,
"filename": "fichero0_A.txt",
"url": "file://C:\\TMPM\\carpeta0\\fichero0_A.txt"
},
"path": {
"root": "c073f96a8d16ebb2e604bebd4fe8b40",
"virtual": "PM/carpeta0/fichero0_A.txt",
"real": "C:\\TMPM\\carpeta0\\fichero0_A.txt"
}
}
}, {
"_index": "job6",
"_type": "_doc",
"_id": "7dfb102f2e64e99740a89b1cff7d92a3",
"_score": 1.0,
"_source": {
"content": "cadena1\n",
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain; charset=ISO-8859-1",
"created": "2020-04-03T11:56:21.165+0000",
"last_modified": "2020-04-03T12:23:23.872+0000",
"last_accessed": "2020-04-03T12:23:23.872+0000",
"indexing_date": "2020-04-14T13:56:52.254+0000",
"filesize": 7,
"filename": "fichero1.txt",
"url": "file://C:\\TMPM\\carpeta1\\fichero1.txt"
},
"path": {
"root": "7ff4e7b0e0c3172dbd29a23861ab2125",
"virtual": "PM/carpeta1/fichero1.txt",
"real": "C:\\TMPM\\carpeta1\\fichero1.txt"
}
}
}, {
"_index": "job6",
"_type": "_doc",
"_id": "74f0e8c1cccf2f32b4d43673cfdca0",
"_score": 1.0,
"_source": {
"content": "cadena2\n",
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain; charset=ISO-8859-1",
"created": "2020-04-03T11:56:30.524+0000",
"last_modified": "2020-04-03T12:23:35.029+0000",
"last_accessed": "2020-04-03T12:23:35.029+0000",
"indexing_date": "2020-04-14T13:56:52.270+0000",
"filesize": 7,
"filename": "fichero2.txt",
"url": "file://C:\\TMPM\\carpeta2\\fichero2.txt"
},
"path": {
"root": "fd0c8ae8a885725cfcaee774c72be43",
"virtual": "PM/carpeta2/fichero2.txt",
"real": "C:\\TMPM\\carpeta2\\fichero2.txt"
}
}
}, {
"_index": "job6",
"_type": "_doc",
"_id": "8d6c56c0448bc4bfa17787923baa9c",
"_score": 1.0,
"_source": {
"content": "cadenaX\n",
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain; charset=ISO-8859-1",
"created": "2020-04-14T15:12:56.632+0000",
"last_modified": "2020-04-14T15:34:53.139+0000",
"last_accessed": "2020-04-14T15:34:53.139+0000",
"indexing_date": "2020-04-14T15:35:14.685+0000",
"filesize": 7,
"filename": "fichero_X.txt",
"url": "file://C:\\TMPM\\carpetaX\\fichero_X.txt"
},
"path": {
"root": "f43118a935c6c7ef81ea65307759fc",
"virtual": "PM/carpetaX/fichero_X.txt",
"real": "C:\\TMPM\\carpetaX\\fichero_X.txt"
}
}
}
]
}

使用以下代码。 *必须使用$ .getJSON *
<html>
<head>
<meta charset='UTF-8' />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
</head>

<body>
<script type="text/javascript">
$(document).ready(function(){

var url = 'http://localhost:9200/job6/_search'

//QUERY A
var datos =
{
"query": {
"match_all": {}
},
"from" : "0", "size" : "2"
}
;


//QUERY B
/*
var datos =
{"query": {
"query_string" : {
"query" : "fichero1.txt",
"default_field" : "file.filename"
}
}
};
*/


//QUERY C
/*
var datos =
{
"query": {
"query_string": {
"query" : "fichero1.txt"
}
}
}
;
*/


//QUERY D
/*
var datos =
{
"query": {
"simple_query_string": {
"all_fields" : true ,
"query" : "fichero1.txt"
}
}
}
;
*/



//QUERY E
/*
var datos =
{
"query":
{ "query_string":
{ "query": "fichero1.txt", "fields": ["file.filename"]
}
}
}
;
*/


//QUERY F
/*
var datos =
{
"query" : {
"match" : {
"file.filename" : "fichero1.txt"
}
}
}
;
*/

var input_jsonString = JSON.stringify(datos);
console.log("input_jsonString=" + input_jsonString);


$.getJSON(url, datos, function (jsonFromFile) {

var output_jsonString = JSON.stringify(jsonFromFile);
console.log("output_jsonString=" + output_jsonString);

}
)

});

</script>
</body>
</html>

我只从以下查询A得到响应
var datos = {
"query": {
"match_all": {}
},
"from": "0",
"size": "2"
};

有答案
output_jsonString = {
"took": 0,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 5,
"relation": "eq"
},
"max_score": 1,
"hits": [{
"_index": "job6",
"_type": "_doc",
"_id": "2e2ed07786c01e9fd845b62fb86a693a",
"_score": 1,
"_source": {
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain",
"created": "2020-04-03T11:57:45.673+0000",
"last_modified": "2020-02-27T16:15:27.795+0000",
"last_accessed": "2020-04-03T11:57:45.673+0000",
"indexing_date": "2020-04-14T13:56:51.668+0000",
"filesize": 0,
"filename": "fichero0_borrador.txt",
"url": "file://C:\\TMPM\\carpeta0\\99.Borrador\\fichero0_borrador.txt"
},
"path": {
"root": "4cface21f575d5e5985e4e3889c278a3",
"virtual": "PM/carpeta0/99.Borrador/fichero0_borrador.txt",
"real": "C:\\TMPM\\carpeta0\\99.Borrador\\fichero0_borrador.txt"
}
}
}, {
"_index": "job6",
"_type": "_doc",
"_id": "6f475ff7ac21fc59f4edc34761d09",
"_score": 1,
"_source": {
"content": "cadena0 A\n",
"meta": {},
"file": {
"extension": "txt",
"content_type": "text/plain; charset=ISO-8859-1",
"created": "2020-04-14T09:30:20.218+0000",
"last_modified": "2020-04-14T09:46:02.930+0000",
"last_accessed": "2020-04-14T09:46:02.930+0000",
"indexing_date": "2020-04-14T13:56:52.223+0000",
"filesize": 9,
"filename": "fichero0_A.txt",
"url": "file://C:\\TMPM\\carpeta0\\fichero0_A.txt"
},
"path": {
"root": "c073f96a8d16ebb2e604bebd4fe8b40",
"virtual": "PM/carpeta0/fichero0_A.txt",
"real": "C:\\TMPM\\carpeta0\\fichero0_A.txt"
}
}
}
]
}

检查输入查询的值和获得的输出。我使用命令“console.log”创建了正在查询的变量“input_jsonString”和“output_jsonString”。

我遇到的问题是,在以下类型的查询中,我都遇到了具有相同特征的错误。代码中名为QUERY B,C,.. F(以检查删除注释代码)。例如对于下一个查询C
input_jsonString = {"query":{"query_string":{"query":"fichero1.txt"}}}
我收到以下错误:
 {"error":{"root_cause":[{"type":"illegal_argument_exception","reason":"request [/job6/_search] contains unrecognized parameter: [query[query_string][query]]"}],"type":"illegal_argument_exception","reason":"request [/job6/_search] contains unrecognized parameter: [query[query_string][query]]"},"status":400}

尽管以前我已经通过Kibana验证了所有查询的编码正确,并且从中我得到了正确的答案,所有查询。就像查询C的图像一样:

Query C in Kibana

拜托,我犯什么错误?

提前致谢

最佳答案

您使用的是GET而不是POST,因此是异常(exception)。如果可以使用GET,请进行查询URI-Search-friendly

如果您需要使用jQuery并且无法利用官方的ES JS Client,则需要使用POST请求,即:

$(document).ready(function () {
var url = "http://localhost:9200/_all/_search";

//QUERY A
var a = {
query: {
match_all: {}
},
from: "0",
size: "2"
};
//QUERY B

var b = {
query: {
query_string: {
query: "fichero1.txt",
default_field: "file.filename"
}
}
};

//QUERY C

var c = {
query: {
query_string: {
query: "fichero1.txt"
}
}
};
//QUERY D

var d = {
query: {
simple_query_string: {
all_fields: true,
query: "fichero1.txt"
}
}
};

//QUERY E

var e = {
query: {
query_string: { query: "fichero1.txt", fields: ["file.filename"] }
}
};

//QUERY F

var f = {
query: {
match: {
"file.filename": "fichero1.txt"
}
}
};

var problematic = {
query: { query_string: { query: "fichero1.txt" } }
};

[a, b, c, d, e, f, problematic].map(function (query) {
$.ajax({
url,
type: "post",
data: JSON.stringify(query),
contentType: "application/json",
dataType: "json",
success: function (response) {
console.log({ query, response });
},
error: function (err) {
console.error({ query, err });
}
});
});
});

FYI http://localhost:9200几乎肯定不会在生产中访问。

关于elasticsearch - 使用$ .getJSON的Elasticsearch查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61367757/

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