2016-08-24 112 views
0

我怎麼能創造身體elasticsearch這樣Elasticsearch搜索多領域

select * from table where full_name like '%q%' or address like '%q%' or description like '%q%' order by full_name , description , address 
+1

你有沒有試着自己寫點東西? – Dekel

+0

是的,但在multi_match只有短語前綴,但我需要像通配符 – feeling

+0

我想你應該多讀一點關於全文搜索。 – Dekel

回答

2

通配符查詢可以是非常昂貴的,特別是如果你在幾個領域進行搜索。正確的做法是在要搜索的部分字段上使用nGram token filter

首先創建像下面的指數與自定義分析,將切片和切塊你的領域爲可搜索的標記:

curl -XPUT localhost:9200/tests -d '{ 
    "settings": { 
    "analysis": { 
     "analyzer": { 
     "substring_analyzer": { 
      "tokenizer": "standard", 
      "filter": ["lowercase", "substring"] 
     } 
     }, 
     "filter": { 
     "substring": { 
      "type": "nGram", 
      "min_gram": 1, 
      "max_gram": 15 
     } 
     } 
    } 
    }, 
    "mappings": { 
    "test": { 
     "properties": { 
     "full_name": { 
      "type": "string", 
      "analyzer": "substring_analyzer" 
     }, 
     "address": { 
      "type": "string", 
      "analyzer": "substring_analyzer" 
     }, 
     "description": { 
      "type": "string", 
      "analyzer": "substring_analyzer" 
     } 
     } 
    } 
    } 
}' 

然後你可以索引一些文檔:

curl -XPUT localhost:9200/tests/test/_bulk -d ' 
{"index":{"_id": 1}} 
{"full_name": "Doe", "address": "1234 Quinn Street", "description": "Lovely guy"} 
{"index":{"_id": 2}} 
{"full_name": "Brennan", "address": "4567 Main Street", "description": "Not qualified"} 
{"index":{"_id": 3}} 
{"full_name": "Quantic", "address": "1234 Quinn Street", "description": "New friend"} 
' 

最後,您可以使用與上面的SQL查詢等效的查詢進行搜索,並且所有三個測試文檔都將匹配:

curl -XPUT localhost:9200/tests/test/_search -d '{ 
    "query": { 
    "bool": { 
     "should": [ 
     { 
      "match": { 
      "full_name": "q" 
      } 
     }, 
     { 
      "match": { 
      "address": "q" 
      } 
     }, 
     { 
      "match": { 
      "description": "q" 
      } 
     } 
     ] 
    } 
    } 
}'