如何在 ElasticSearch 中通过字典键进行搜索
How to search by dict keys in ElasticSearch
我是 elasticsearch 的新手,我需要编写如下所述的查询
我有一个像这样的文档的 elasticsearch 索引
数字表示书中单词的条目
{
"words": { # words listed below are different in every document
"cat": 7,
"monkey": 4,
"mouse": 10,
...
}
我想按单词“cats”(不是“cat”)、“monkey”和例如“mous”来搜索文档。
如何编写查询以在文档中找到“cat”、“monkey”和“mouse”并在计算分数时考虑它们的值?
更新。示例:我有文件
{ # doc1
"words": {
"cat": 7,
"monkey": 4,
"mouse": 10,
}
}
{ # doc2
"words": {
"Cat": 20,
"monkeys": 40,
"mouse": 10,
}
}
{ # doc3
"words": {
"Dog": 10,
"monkey": 2,
"human": 10,
}
}
# I am searching by words "cats", "monkey", "mouse"
# I want to get doc2 as the most relevant result (because of words matches and entries quantity).
# doc1 should be the second result (words do match, but there are less entries).
# doc3 is the third result (too few words matches)
有什么想法吗?
我不知道我是否理解正确,但你想搜索“cats”和return包含“cat”、“monkey”和“mouse”的文档词。
运行我做的这个例子,看看它是否适合你。
PUT my-index-000001
{
"mappings" : {
"properties" : {
"words" : {
"properties" : {
"count" : {
"type" : "text"
},
"key" : {
"type" : "text",
"analyzer": "english"
}
}
}
}
}
}
POST my-index-000001/_doc/1
{
"words": [
{
"key": "cat",
"count": "7"
},
{
"key": "monkey",
"count": "4"
},
{
"key": "mouse",
"count": "10"
}
]
}
GET my-index-000001/_search
{
"query": {
"match": {
"words.key":{
"query": "cats"
}
}
}
}
我是 elasticsearch 的新手,我需要编写如下所述的查询
我有一个像这样的文档的 elasticsearch 索引
数字表示书中单词的条目
{
"words": { # words listed below are different in every document
"cat": 7,
"monkey": 4,
"mouse": 10,
...
}
我想按单词“cats”(不是“cat”)、“monkey”和例如“mous”来搜索文档。 如何编写查询以在文档中找到“cat”、“monkey”和“mouse”并在计算分数时考虑它们的值?
更新。示例:我有文件
{ # doc1
"words": {
"cat": 7,
"monkey": 4,
"mouse": 10,
}
}
{ # doc2
"words": {
"Cat": 20,
"monkeys": 40,
"mouse": 10,
}
}
{ # doc3
"words": {
"Dog": 10,
"monkey": 2,
"human": 10,
}
}
# I am searching by words "cats", "monkey", "mouse"
# I want to get doc2 as the most relevant result (because of words matches and entries quantity).
# doc1 should be the second result (words do match, but there are less entries).
# doc3 is the third result (too few words matches)
有什么想法吗?
我不知道我是否理解正确,但你想搜索“cats”和return包含“cat”、“monkey”和“mouse”的文档词。
运行我做的这个例子,看看它是否适合你。
PUT my-index-000001
{
"mappings" : {
"properties" : {
"words" : {
"properties" : {
"count" : {
"type" : "text"
},
"key" : {
"type" : "text",
"analyzer": "english"
}
}
}
}
}
}
POST my-index-000001/_doc/1
{
"words": [
{
"key": "cat",
"count": "7"
},
{
"key": "monkey",
"count": "4"
},
{
"key": "mouse",
"count": "10"
}
]
}
GET my-index-000001/_search
{
"query": {
"match": {
"words.key":{
"query": "cats"
}
}
}
}