简体   繁体   English

如何在 Elastic Search 中使用标准分析器搜索文本

[英]How to search on Text with Standard Analyser in Elastic Search

I have a key value pair indexed as text in ES Mapping.我有一个键值对索引为 ES 映射中的文本。

{key:myKey, value:MY_VALUE}

Whatever I do, let it be term, match, terms it doesn't hit in ES when I try to query for value无论我做什么,让它成为术语,匹配,当我尝试查询值时它不会在 ES 中遇到的术语

So: MY_VALUE will convert to terms of [my, value]所以: MY_VALUE 将转换为 [my, value] 的条款

If you use a match query without any modification then it uses the same analyzer which was used to index the field, ie generates the same types of token for your search term and should produce you the search result.如果您使用匹配查询而不进行任何修改,那么它会使用用于索引该字段的相同分析器,即为您的搜索词生成相同类型的标记,并且应该为您生成搜索结果。

As per Elasticsearch Match query Doc:根据 Elasticsearch 匹配查询文档:

The provided text is analyzed before matching.在匹配之前分析提供的文本。

The match query is the standard query for performing a full-text search匹配查询是执行全文搜索的标准查询

As you have not provided any sample, let me show it using my sample data:由于您没有提供任何示例,让我使用我的示例数据来展示它:

Index def索引定义

{
  "mappings" :{
    "properties" :{
        "title" :{
            "type" : "text" --> default standard analyzer would be used.
        }
    }
  }
}

Index sample docs索引示例文档

{
    "title": "foo bar"
}

{
    "title": "hello world"
}

search for foo搜索 foo

{
    "query" :{
        "match" :{
            "title" :"foo"
        }
    }
}

Result结果

 "hits": [
            {
                "_index": "standard",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.4700036,
                "_source": {
                    "title": "foo bar"
                }
            }
        ]

Search for foo bar搜索 foo bar

{
    "query" :{
        "match" :{
            "title" :"foo bar"
        }
    }
}

Result结果

"hits": [
            {
                "_index": "standard",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.9400072,
                "_source": {
                    "title": "foo bar"
                }
            }
        ]

Note: Searching for foo and foo bar both matches as standard token split the tokens on whitespace and you can use analyze API to check that as shown below:注意:搜索foofoo bar都匹配,因为标准标记拆分了空格上的标记,您可以使用分析 API进行检查,如下所示:

{
  "text" : "foo bar",
  "analyzer" : "standard"
}

{
    "tokens": [
        {
            "token": "foo",
            "start_offset": 0,
            "end_offset": 3,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "bar",
            "start_offset": 4,
            "end_offset": 7,
            "type": "<ALPHANUM>",
            "position": 1
        }
    ]
}

Edit: Based on the comment from OP(@AKASH GUPTA)编辑:基于 OP(@AKASH GUPTA) 的评论

Now Elastic provides a default keyword field for text field but that's only for dynamic mapping and not for the static mapping and that too in the latest versions.现在 Elastic 为文本字段提供了一个默认的关键字字段,但这仅适用于动态映射,而不适用于 static 映射,并且在最新版本中也是如此。 Please refer this blog post and if you are using static mapping you can define your own corresponding keyword field.请参考 这篇博文,如果您使用 static 映射,您可以定义自己的相应keyword字段。

Moving comment to answer: "It seems Elastic Search by default provides keyword for text fields. In this case it can be used as "title.keyword".移动评论回答:“似乎弹性搜索默认为文本字段提供关键字。在这种情况下,它可以用作“title.keyword”。

Refer: https://www.elastic.co/blog/strings-are-dead-long-live-strings (Section: New Defaults.)请参阅: https://www.elastic.co/blog/strings-are-dead-long-live-strings (部分:新默认值。)

If you are having issues with "analyzed" text, and want a non analyzed string, you can append ".keyword" to your key.如果您遇到“已分析”文本的问题,并且想要一个未分析的字符串,您可以将 append ".keyword" 设置为您的密钥。 before that do check if the mapping is present similar to:在此之前检查映射是否存在类似于:

"yourKey": {
     "type": "text",
     "fields": {
           "keyword": {
                "type": "keyword",
                "ignore_above": 256
           }
      }
}

If its not present, update the mapping similar as above.如果它不存在,请更新与上述类似的映射。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM