2013-09-24 13 views
12

Mam pole website dokumentu indeksowanego w wyszukiwaniu elastycznym. Przykładowa wartość: http://example.com. Problem polega na tym, że podczas wyszukiwania example dokument nie jest uwzględniony. Jak poprawnie odwzorować pole strony/adresu URL?Strona indeksowania/URL w Elastic Search

I stworzył indeks poniżej:

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

Odpowiedz

22

Chyba używasz standard analizatora, który dzieli się na dwie http://example.dom tokenów - http i example.com. Możesz spojrzeć na http://localhost:9200/_analyze?text=http://example.com&analyzer=standard.

Jeśli chcesz podzielić url, musisz użyć innej analyzer lub podać własną custom analyzer.

Możesz sprawdzić, jak zostałby zaindeksowany url indeksowany przez simple analyzer - http://localhost:9200/_analyze?text=http://example.com&analyzer=simple. Jak widać, teraz jest url indeksowany jako trzy tokeny ['http', 'example', 'com']. Jeśli nie chcesz indeksować żetonów takich jak ['http', 'www'] itd., Możesz określić swój analizator za pomocą lowercase tokenizer (jest to ten używany w prostym analizatorze) i stop filter. Na przykład coś takiego:

# 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" 
    } 
    } 
}' 

Uwaga: Jeśli nie chcesz używać stopwords Oto ciekawy artykuł stop stopping stop words: a look at common terms query

+0

dzięki @vhyza. Zaktualizowałem pytanie w/jak stworzyłem indeks. Mam zagnieżdżoną właściwość i chcę usunąć html. –

+0

Nie ma za co. Zagnieżdżone właściwości powinny być w porządku. Możesz dodać 'char_filter' w' lowercase_with_stopwords ', aby usunąć HTML, jeśli chcesz. – vhyza

Powiązane problemy