简体   繁体   English

Elasticsearch:如何使用不同的分析器进行搜索?

[英]Elasticsearch: How to search with different analyzers?

I'm using my custom analyzer autocomplete_analyzer with filter edgeNGram . 我正在使用带有过滤器edgeNGram自定义分析器autocomplete_analyzer So mapping looks like: 因此映射看起来像:

  "acts_as_taggable_on_tags" : {
    "acts_as_taggable_on/tag" : {
      "properties" : {
        "name" : {
          "type" : "string",
          "boost" : 10.0,
          "analyzer" : "autocomplete_analyzer"
        }
      }
    }
  }

When I search using query_string , it works like autocomplete. 当我使用query_string搜索时,它就像自动完成一样。 For example, query "lon" returns ["lon", "long", "london",...]. 例如,查询“ lon”返回[“ lon”,“ long”,“ london”,...]。

But sometimes I need exact matching. 但是有时候我需要精确匹配。 How can I get just one exactly matching word "lon"? 我怎样才能得到一个完全匹配的单词“ lon”? Can I use another analyzers (eg simple or standard ) when I making a search query? 进行搜索查询时可以使用其他分析仪(例如, simplestandard )吗?

I think you will need to store the data in 2 separate fields. 我认为您需要将数据存储在2个单独的字段中。 One would contain the tokens necessary for doing autocomplete queries, the other for the full search queries. 一个将包含执行自动完成查询所需的令牌,另一个将包含完整搜索查询。

If you have only one field with the tokens [lon, lond, londo, london] then if you search against this field you cannot say "please only match the token london as this is the full word/longest token". 如果只有一个带标记的字段[lon, lond, londo, london]则如果您搜索该字段,则不能说“请只匹配标记london因为这是完整的单词/最长的标记”。

You can have the 2 fields done nicely for you with the multi-field. 通过多字段,您可以很好地完成这两个字段。 Take a look at the elasticsearch docs on multi-field . 看一下multi-field上的elasticsearch文档 The 'official' documentation is pretty good on this section, please check it out! “官方”文档在本节中相当不错,请查看!

I would probably do this: 我可能会这样做:

Mapping 制图

"acts_as_taggable_on_tags" : {
  "acts_as_taggable_on/tag" : {
    "properties" : {
      "name" : {
        "type" : "multi_field",           
        "fields" : {
          "name" : {
            "type" : "string",
            "boost" : 10.0
          },
          "autocomplete" : {
            "type" : "string",
            "analyzer" : "autocomplete_analyzer",
            "boost" : 10.0
          }
        }
      }
    }
  }
}

Querying 查询

for autocomplete queries: 对于自动完成查询:

"query": {
  "query_string": {
    "query" : "lon",
    "default_field": "name.autocomplete"
  }
}

for normal queries: 对于普通查询:

"query": {
  "query_string": {
    "query" : "lon",
    "default_field": "name"
  }
}

Note the difference in "default_field". 注意“ default_field”中的区别。

The other answer given would not work; 给出的其他答案将无效。 the different search_analyzer would mean that a search for 'london' would not get tokenized into lon, lond, londo, london . 不同的search_analyzer意味着对“ london”的搜索不会被标记为lon, lond, londo, london But this would not stop a search for 'lon' from matching documents with a name of 'london' which I think is what you want. 但这不会阻止您从匹配文件中搜索名称为“ london”的“ lon”(我认为这是您想要的)。

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

相关问题 Elasticsearch - 如何使用不同的分析器为同一个字段编制索引 - Elasticsearch - How to index the same field with different analyzers Elasticsearch-为一个字段指定不同的索引和搜索分析器 - Elasticsearch - specify different index and search analyzers for one field ElasticSearch索引和搜索分析器一起 - ElasticSearch index and search analyzers together 如何创建一个Elasticsearch节点以指定默认搜索分析器进行索引和搜索 - How to create a Elasticsearch node specifying default search analyzers for indexing and searching Elasticsearch:使用分析仪组合时搜索如何工作? - Elasticsearch: How does search work when using combination of analyzers? 如何根据要插入的文档在 Elasticsearch 中使用不同的分析器? - How to use different analyzers in Elasticsearch depending on document to insert? 如何在ES中使用不同的分析器创建索引以进行搜索和索引编制? - How to createin ES an index with different analyzers for search and indexing? Elasticsearch搜索无结果,可能是分析器 - Elasticsearch search yields no results, analyzers might be the issue Elasticsearch 中用于索引和搜索的不同自定义分析器为用户查询提供 NULL 结果 - The different custom analyzers for index and search in Elasticsearch giving NULL results for user queries elasticsearch 中的分析器 - Analyzers in elasticsearch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM