首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ElasticSearch中按切分键进行搜索

如何在ElasticSearch中按切分键进行搜索
EN

Stack Overflow用户
提问于 2022-04-21 20:59:53
回答 1查看 313关注 0票数 -1

我是elasticsearch的新手,我需要编写下面描述的查询

我有一个elasticsearch索引,其中有类似于这个的文档

数字是指书中单词的条目。

代码语言:javascript
复制
{
  "words": {        # words listed below are different in every document
    "cat": 7,
    "monkey": 4,
    "mouse": 10,
    ...
}

我想用“猫”(不是“猫”)、“猴子”和“猫”等词来搜索文档。如何编写查询,在文档中找到“猫”、“猴子”和“鼠标”,并在计算分数时考虑它们的值?

upd。我有文件

代码语言:javascript
复制
{                             # 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)

有什么想法吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-04-21 22:36:51

我不知道我是否正确理解,但您想要搜索“猫”,并返回“猫”,“猴子”和“鼠标”等词的文档单词。

运行我创建的这个示例,看看它是否适用于您。

代码语言:javascript
复制
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"
      }
    }
  }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71960812

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档