如何将Elasticsearch Analyzer与搜索查询连接(Typeahead搜索,组合搜索)?

发布于 01-24 10:50 字数 1438 浏览 4 评论 0 原文

我将映射分析仪访问了该领域。

我的优先级是基于搜索(我想基于 给定的文字) 1必须从左侧匹配(1优先级), 2给定文本中的任何单词(2个优先级), 3给定文本中的任何字符(3个优先级)

**1. I created field mapping with the custom analyzer.**
    `
    "properties" => [
                "name" => [
                    "type"              => "search_as_you_type",
                    "analyzer"          => "my_tokenizer",
                ],
        ];
    `
**2. created a custom analyzer**
    
    {
        "settings": { 
          "analysis": {
              "tokenizer": { 
                   "my_tokenizer": {
                        "type": "edge_ngram", 
                        "min_gram": 1, 
                         "max_gram": 10, 
                         "token_chars": ["letter", "digit", "symbol"]
                    }
                },
               "analyzer": { 
                "my_analyzer":{
                       "tokenizer": "my_tokenizer"
                }
               }
             }
          }
       }
    
**3. Search query**
  
    'query' => [
               'query_string' => [
                      'default_field' => "name",
                      'query' => "test",
                      'default_operator' => "AND",
                    ],
         ],

**The result I am getting:**

TEST
ABC TEST
NAKT TZY TEST
TEST POST
TEST VN

**What I want :**
TEST
TEST VN
TEST POST
ABC TEST
NAKT TZY TEST

I have an issue with the mapping analyzer to the field.

My priority is based on search (i want to autocomplete search based on
the given text)
1 must match from the left side (1 priority),
2 any word in the given text (2 priority),
3 any character like the combination in the given text (3 priority)

**1. I created field mapping with the custom analyzer.**
    `
    "properties" => [
                "name" => [
                    "type"              => "search_as_you_type",
                    "analyzer"          => "my_tokenizer",
                ],
        ];
    `
**2. created a custom analyzer**
    
    {
        "settings": { 
          "analysis": {
              "tokenizer": { 
                   "my_tokenizer": {
                        "type": "edge_ngram", 
                        "min_gram": 1, 
                         "max_gram": 10, 
                         "token_chars": ["letter", "digit", "symbol"]
                    }
                },
               "analyzer": { 
                "my_analyzer":{
                       "tokenizer": "my_tokenizer"
                }
               }
             }
          }
       }
    
**3. Search query**
  
    'query' => [
               'query_string' => [
                      'default_field' => "name",
                      'query' => "test",
                      'default_operator' => "AND",
                    ],
         ],

**The result I am getting:**

TEST
ABC TEST
NAKT TZY TEST
TEST POST
TEST VN

**What I want :**
TEST
TEST VN
TEST POST
ABC TEST
NAKT TZY TEST

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

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

发布评论

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

评论(1

泅渡 2025-01-31 10:50:23

我不明白为什么您要将分析器用于搜索_AS_YOU_TYPE字段,该字段要用作自动完成。
search_as_you_type已经使用ngram创建了子字段。

我像这样映射了:

"mappings": {
     "properties": {
       "name": {
         "type": "search_as_you_type"
       }
     }
   }

下面的查询按照您需要运行。

{
  "query": {
    "multi_match": {
      "query": "test",
      "type": "bool_prefix",
      "fields": [
        "name",
        "name._2gram",
        "name._3gram"
      ]
    }
  }
}

查看更多: <="" a="">

I don't understand why you use an analyzer for a search_as_you_type field that you want to use as an autocomplete.
The search_as_you_type already creates the subfields with ngram.

I mapped like this:

"mappings": {
     "properties": {
       "name": {
         "type": "search_as_you_type"
       }
     }
   }

And the query below ran as you want.

{
  "query": {
    "multi_match": {
      "query": "test",
      "type": "bool_prefix",
      "fields": [
        "name",
        "name._2gram",
        "name._3gram"
      ]
    }
  }
}

See more: https://www.elastic.co/guide/en/elasticsearch/reference/current/search-as-you-type.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文