2013-09-24 178 views
12

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

我创建了以下指标:我猜你正在使用standard分析仪,它分裂成http://example.dom两个记号

{ 
    "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" 
          } 
         } 
        } 
       } 
      } 
     } 
    } 
    } 
} 

回答

22

- httpexample.com。你可以看看http://localhost:9200/_analyze?text=http://example.com&analyzer=standard

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

你可以看看url如何编制simple 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

+0

感谢@vhyza。我更新了如何创建索引的问题。我有一个嵌套的属性,并希望剥离HTML。 –

+0

不客气。嵌套属性应该没问题。如果需要,您可以在'lowercase_with_stopwords'中添加'char_filter'来去除html。 – vhyza