[英]How to fetch particular documents in elasticsearch index
I want to fetch all the data of the corresponding particular field, and have a response of the elastic search.我想获取相应特定字段的所有数据,并获得弹性搜索的响应。
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 35,
"relation": "eq"
},
"max_score": 0.44183275,
"hits": [
{
"_index": "allevents",
"_type": "_doc",
"_id": "jQPDaG0BcOh3oggcguoV",
"_score": 0.44183275,
"_source": {
"category": "sessions",
"contentid": "KqRLj2lWZ3",
"clientname": "omkarpathlab",
------------------
}]
I tried search function it returning an error.我尝试搜索 function它返回错误。
var elasticsearch = require('elasticsearch');
var client = new elasticsearch.Client({
host: 'aaa',
log: 'trace',
apiVersion: '7.1'
});
client.search({
"size": 20,
"query": {
"query_string": {
"default_field": "clientname",
"query": "omkarlab"
}
}
}).then((res) => {
console.log("resultData", res);
}, (err) => {
console.log("err", err);
});
enter code here
Error showing:错误显示:
{ Error: [illegal_argument_exception] request [/_search] contains unrecognized parameter: [query] { 错误:[illegal_argument_exception] 请求 [/_search] 包含无法识别的参数:[query]
Please suggest me how to solve this kind of problem.请建议我如何解决此类问题。
You should specify your field under default_field
, not the value you are looking for.您应该在default_field
下指定您的字段,而不是您要查找的值。 The field you are trying to query is clientname
in your case, and the value you are looking for is omkarpathlab
.在您的情况下,您尝试查询的字段是clientname
,而您要查找的值是omkarpathlab
。 So your query should be as follows:所以你的查询应该如下:
"query": {
"query_string": {
"default_field": "clientname",
"query": "omkarpathlab"
}
}
edit.编辑。 But your query inside of the body property:但是您在 body 属性中的查询:
client.search({
"size": 20,
"body": {
"query": {
"query_string": {
"default_field": "clientname",
"query": "omkarlab"
}
}
}
}).then((res) => {
console.log("resultData", res);
}, (err) => {
console.log("err", err);
});
You can use below code to connect to elasticsearch.您可以使用以下代码连接到 elasticsearch。 I have tested it on 5.6 version我已经在 5.6 版本上测试过
'use strict'
const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://XXX:9200' })
async function run () {
// Let's search!
const { body } = await client.search({
index: 'XXX',
type : 'XXX',
body: {
query: {
match_all: {}
}
}
})
console.log(body.hits.hits)
}
run().catch(console.log)
Code is a sample from https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html site.代码是来自https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html站点的示例。
for search documentation check below link https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_search对于搜索文档,请查看以下链接https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_search
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.