【问题标题】:Indexing website/url in Elastic Search在 Elastic Search 中索引网站/网址
【发布时间】:2013-09-29 10:42:41
【问题描述】:

我有一个在弹性搜索中索引的文档的 website 字段。示例值:http://example.com。问题是当我搜索example 时,没有包含该文档。如何正确映射网站/网址字段?

我在下面创建了索引:

{
  "settings":{
    "index":{
        "analysis":{
        "analyzer":{
            "analyzer_html":{
                  "type":"custom",
                  "tokenizer": "standard",
                "filter":"standard",
                "char_filter": "html_strip"
            }
        }
        }
    }
  },
  "mapping":{
    "blogshops": {
        "properties": {
            "category": {
                "properties": {
                    "name": {
                        "type": "string"
                    }
                }
            },
            "reviews": {
                "properties": {
                    "user": {
                        "properties": {
                            "_id": {
                                "type": "string"
                            }
                        }
                    }
                }
            }
        }
    }
  }
}

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我猜你正在使用standard 分析器,它将http://example.dom 分成两个标记-httpexample.com。你可以看看http://localhost:9200/_analyze?text=http://example.com&analyzer=standard

    如果要拆分url,需要使用不同的analyzer或者指定我们自己的custom analyzer

    您可以查看如何将urlsimple analyzer - http://localhost:9200/_analyze?text=http://example.com&analyzer=simple 编入索引。如您所见,现在url 被索引为三个标记['http', 'example', 'com']。如果您不想索引['http', 'www'] 等标记,您可以使用lowercase tokenizer(这是简单分析器中使用的)和stop filter 指定您的分析器。例如这样的:

    # Delete index
    #
    curl -s -XDELETE 'http://localhost:9200/url-test/' ; echo
     
    # Create index with mapping and custom index
    #
    curl -s -XPUT 'http://localhost:9200/url-test/' -d '{
      "mappings": {
        "document": {
          "properties": {
            "content": {
              "type": "string",
              "analyzer" : "lowercase_with_stopwords"
            }
          }
        }
      },
      "settings" : {
        "index" : {
          "number_of_shards" : 1,
          "number_of_replicas" : 0
        },
        "analysis": {
          "filter" : {
            "stopwords_filter" : {
              "type" : "stop",
              "stopwords" : ["http", "https", "ftp", "www"]
            }
          },
          "analyzer": {
            "lowercase_with_stopwords": {
              "type": "custom",
              "tokenizer": "lowercase",
              "filter": [ "stopwords_filter" ]
            }
          }
        }
      }
    }' ; echo
    
    curl -s -XGET 'http://localhost:9200/url-test/_analyze?text=http://example.com&analyzer=lowercase_with_stopwords&pretty'
    
    # Index document
    #
    curl -s -XPUT 'http://localhost:9200/url-test/document/1?pretty=true' -d '{
      "content" : "Small content with URL http://example.com."
    }'
    
    # Refresh index
    #
    curl -s -XPOST 'http://localhost:9200/url-test/_refresh'
    
    # Try to search document
    #
    curl -s -XGET 'http://localhost:9200/url-test/_search?pretty' -d '{
      "query" : {
        "query_string" : {
            "query" : "content:example"
        }
      }
    }'
    

    注意:如果你不喜欢使用停用词,这里是有趣的文章stop stopping stop words: a look at common terms query

    【讨论】:

    • 感谢@vhyza。我更新了我如何创建索引的问题。我有一个嵌套的属性,想剥离 html。
    • 不客气。嵌套属性应该没问题。如果需要,您可以在 lowercase_with_stopwords 中添加 char_filter 以剥离 html。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-04
    相关资源
    最近更新 更多