根据我的映射和使用关键字,我应该如何查询 Elastic Search?

发布于 2024-11-25 01:09:57 字数 1077 浏览 1 评论 0原文

我有一个非常简单的映射,如下所示(我稍微简化了示例):

{
    "location" : {
        "properties": {
            "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" },
            "description": { "type": "string", "analyzer": "snowball" }
        }
    }
}

现在我使用一些基于真实英语单词的随机值来索引很多位置。

我希望能够搜索与名称或描述字段中的任何给定关键字匹配的位置(名称更重要,因此我给予了它提升)。我尝试了一些不同的查询,但它们没有返回任何结果。

{
    "fields" : ["name", "description"],
    "query" : {
        "terms" : {
            "name" : ["savage"],
            "description" : ["savage"]
        },
        "from" : 0,
        "size" : 500
    }
}

考虑到描述中有些位置有“savages”一词,它应该会给我一些结果(“savage”是“savages”的词干)。使用上述查询会产生 0 个结果。我一直在使用curl来查询ES:

curl -XGET -d @query.json http://localhost:9200/myindex/locations/_search

如果我使用查询字符串:

curl -XGET http://localhost:9200/fieldtripfinder/locations/_search?q=description:savage

我实际上得到一个结果(当然现在它只会搜索描述字段)。

基本上,我正在寻找一个查询,该查询将使用多个关键字进行 OR 类型的搜索,并将它们与名称和描述字段中的值进行比较。

I have a very simple mapping which looks like this (I streamlined the example a bit):

{
    "location" : {
        "properties": {
            "name": { "type": "string", "boost": 2.0, "analyzer": "snowball" },
            "description": { "type": "string", "analyzer": "snowball" }
        }
    }
}

Now I index a lot of locations using some random values which are based on real English words.

I'd like to be able to search for locations that match any of the given keywords in either the name or the description field (name is more important, hence the boost I gave it). I tried a few different queries and they don't return any results.

{
    "fields" : ["name", "description"],
    "query" : {
        "terms" : {
            "name" : ["savage"],
            "description" : ["savage"]
        },
        "from" : 0,
        "size" : 500
    }
}

Considering there are locations which have the word savaged in the description it should get me some results (savage is the stem of savaged). It yields 0 results using the above query. I've been using curl to query ES:

curl -XGET -d @query.json http://localhost:9200/myindex/locations/_search

If I use query string instead:

curl -XGET http://localhost:9200/fieldtripfinder/locations/_search?q=description:savage

I actually get one result (of course now it would be searching the description field only).

Basically I am looking for a query that will do a OR kind of search using multiple keywords and compare them to the values in both the name and the description field.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2024-12-02 01:09:57

Snowball 将“savage”词干为“savag”,这就是术语“savage”没有返回任何结果的原因。但是,当您在 URL 上指定“savage”时,系统会对其进行分析并获得结果。根据您的意图,您可以使用正确的词干(“savag”)或使用“match”查询而不是“terms”来分析您的术语:

{
  "fields" : ["name", "description"],
  "query" : {
    "bool" : {
      "should" : [
        {"match" : {"name" : "savage"}},
        {"match" : {"description" : "savage"}}
      ]
    },
    "from" : 0,
    "size" : 500
  }
}

Snowball stems "savage" into "savag" that’s why term "savage" didn't return any results. However, when you specify "savage" on URL, it’s getting analyzed and you get results. Depending on what your intention is, you can either use correct stem ("savag") or analyze your terms by using "match" query instead of "terms":

{
  "fields" : ["name", "description"],
  "query" : {
    "bool" : {
      "should" : [
        {"match" : {"name" : "savage"}},
        {"match" : {"description" : "savage"}}
      ]
    },
    "from" : 0,
    "size" : 500
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文