[英]How to fetch particular documents in elasticsearch index
我想獲取相應特定字段的所有數據,並獲得彈性搜索的響應。
{
"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",
------------------
}]
我嘗試搜索 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
錯誤顯示:
{ 錯誤:[illegal_argument_exception] 請求 [/_search] 包含無法識別的參數:[query]
請建議我如何解決此類問題。
您應該在default_field
下指定您的字段,而不是您要查找的值。 在您的情況下,您嘗試查詢的字段是clientname
,而您要查找的值是omkarpathlab
。 所以你的查詢應該如下:
"query": {
"query_string": {
"default_field": "clientname",
"query": "omkarpathlab"
}
}
編輯。 但是您在 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);
});
您可以使用以下代碼連接到 elasticsearch。 我已經在 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)
代碼是來自https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/search_examples.html站點的示例。
對於搜索文檔,請查看以下鏈接https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/api-reference.html#_search
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.