【问题标题】:ES suggest, search all words in index item (not only the first word)ES 建议,搜索索引项中的所有单词(不仅是第一个单词)
【发布时间】:2020-10-06 10:09:00
【问题描述】:

基于this 答案(第一个选项)我创建了这个索引:

    'settings' => array(
        'analysis' => array(
            'analyzer' => array(
                'stop_analyzer' => array( 
                    'type' => 'custom',
                    'tokenizer' => 'standard',
                    'filter' => array(
                        'lowercase',
                        'english_stop'
                    )
                )
            ),
            "filter" => array(
                "english_stop" => array(
                    "type" => "stop",
                    "stopwords" => "_english_"
                )
            )
        )
    ),
    'mappings' => array(
        'properties' => array(
            'texts' => array(
                'type' => 'completion',
                "analyzer" => "stop_analyzer",
                "search_analyzer" => "stop_analyzer", 
                'preserve_position_increments' => false
            ),
        ),
    )

当我开始使用或不使用停用词的建议搜索时,这非常有效。但是,例如,当我的索引中有这个:This is the text,并且我搜索text 时,我不会得到任何结果,那么正确的方法是什么?我宁愿不使用 N-gram。

我的搜索查询:

'suggest' => array(
    'suggestion' => array(
        'prefix'=> 'text',
        'completion' => array(
            'field' => 'texts'
        )
    )
)

【问题讨论】:

  • 您有机会浏览我的回答吗,期待您的反馈????
  • @Bhavya 是的,谢谢您的宝贵时间。目前我正试图弄清楚我将如何做到这一点。您提供的解决方案看起来很昂贵,所以我不确定我是否可以使用它,因为它必须很快。我会再等一会儿,如果没有其他解决方案,我会接受你的回答。
  • 感谢您的回复????是的,你是对的,它非常昂贵,但如果你愿意,我也可以提供使用 n-gram 的解决方案????
  • @Bhavya 哦,这会很有帮助,因为这可能是我唯一的选择,所以请接受!

标签: elasticsearch elasticsearch-query


【解决方案1】:

根据用户给出的评论,添加另一个答案,用于使用 n-gram 搜索所有单词。以前的方法效果很好,但是使用正则表达式非常昂贵。

添加一个包含索引映射、索引数据、搜索查询和搜索结果的工作示例

索引映射:

{
  "settings": {
    "analysis": {
      "filter": {
        "my_custom_stop_words_filter": {
          "type": "stop",
          "ignore_case": true,
          "stopwords": [
            "and",
            "is",
            "the"
          ]
        },
        "ngram_filter": {
          "type": "ngram",
          "min_gram": 4,
          "max_gram": 20
        }
      },
      "analyzer": {
        "ngram_analyzer": {
          "type": "custom",
          "tokenizer": "standard",
          "filter": [
            "lowercase",
            "ngram_filter",
            "my_custom_stop_words_filter"
          ]
        }
      }
    },
    "max_ngram_diff": 50
  },
  "mappings": {
    "properties": {
      "title": {
        "type": "text",
        "analyzer": "ngram_analyzer",
        "search_analyzer": "standard"
      }
    }
  }
}

分析 API

POST/_analyze
{
  "analyzer" : "ngram_analyzer",
  "text" : "This is the text"
}

生成以下令牌:

{
    "tokens": [
        {
            "token": "this",
            "start_offset": 0,
            "end_offset": 4,
            "type": "<ALPHANUM>",
            "position": 0
        },
        {
            "token": "text",
            "start_offset": 12,
            "end_offset": 16,
            "type": "<ALPHANUM>",
            "position": 3
        }
    ]
}

索引数据:

{
  "title": [
    "This is the text"
  ]
}

搜索查询:

{
    "query": {
        "match": {
           "title": "text"
        }
    }
}

搜索结果:

"hits": [
            {
                "_index": "stof_29753971",
                "_type": "_doc",
                "_id": "1",
                "_score": 0.41978103,
                "_source": {
                    "title": [
                        "This is the text"
                    ]
                }
            }
        ]

【讨论】:

    【解决方案2】:

    最好的补全提示器,可以匹配中间 fields 是 n-gram 过滤器。

    但由于您不想使用 n-gram,您可以尝试以下方法:

    您可以使用多个建议,其中一个建议基于前缀,并且您可以使用正则表达式在字段中间进行匹配。

    添加一个包含索引映射、数据、搜索查询和搜索结果的工作示例

    索引映射:

    {
      "settings": {
        "analysis": {
          "filter": {
            "my_custom_stop_words_filter": {
              "type": "stop",
              "ignore_case": true,
              "stopwords": [ "and", "is", "the" ]
            }
          },
          "analyzer": {
            "autocomplete": {
              "type": "custom",
              "tokenizer": "whitespace",
              "filter": [
                "lowercase",
                "my_custom_stop_words_filter"
              ]
            }
          }
        }
      },
      "mappings": {
        "properties": {
          "title": {
            "type": "keyword"
          },
          "suggest": {
            "type": "completion",
            "analyzer": "autocomplete",
            "search_analyzer": "standard"
          }
        }
      }
    }
    

    索引数据:

    {
      "suggest": [
        {
          "input": "This is the text"
        }
      ]
    }
    {
      "suggest": [
        {
          "input": "Software Manager"
        }
      ]
    }
    

    搜索查询:

    {
        "suggest": {
            "suggest-exact": {
                "prefix": "text",
                "completion": {
                    "field": "suggest",
                    "skip_duplicates": true
                }
            },
            "suggest-regex": {
                "regex": ".*text.*",
                "completion": {
                    "field": "suggest",
                    "skip_duplicates": true
                }
            }
        }
    }
    

    搜索结果:

    "suggest": {
        "suggest-exact": [
          {
            "text": "text",
            "offset": 0,
            "length": 4,
            "options": []
          }
        ],
        "suggest-regex": [
          {
            "text": ".*text.*",
            "offset": 0,
            "length": 8,
            "options": [
              {
                "text": "This is the text",
                "_index": "test",
                "_type": "_doc",
                "_id": "1",
                "_score": 1.0,
                "_source": {
                  "suggest": [
                    {
                      "input": "This is the text"
                    }
                  ]
                }
              }
            ]
          }
        ]
      }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-17
      • 2017-08-07
      • 1970-01-01
      • 2011-04-04
      • 2018-07-21
      • 1970-01-01
      相关资源
      最近更新 更多