2014-03-30 37 views
0

如何设置一个弹性搜索,使我可以搜索单词的一部分或单词的开头。如何用elasticsearch搜索单词的开头?

当我运行下面的脚本时,除非使它成为'inte *',否则它找不到前缀'inte'。

我不明白这里有什么问题!

echo "--- DELETING OLD INDEX ---" 

curl -XDELETE 'http://localhost:9200/searchtest?pretty' 

echo "--- CREATING INDEX ---" 

curl -XPUT 'http://127.0.0.1:9200/searchtest/?pretty=1' -d '{ 
    "mappings" : { 
     "mytype" : { 
     "properties" : { 
      "description": { 
       "type": "string", 
       "index_analyzer": "my_analyzer", 
       "search_analyzer": "standard" 
      } 
     } 
     } 
    }, 
    "settings" : { 
     "analysis" : { 
     "analyzer" : { 
      "my_analyzer" : { 
       "filter" : [ "standard", "lowercase", "stop" ], 
       "type" : "custom", 
       "tokenizer" : "my_tokenizer" 
      } 
     }, 
     "tokenizer" : { 
      "my_tokenizer" : { 
       "side" : "front", 
       "max_gram" : 200, 
       "type" : "edgeNGram" 
      } 
     } 
     } 
    } 
}' 

echo "--- INSERTING RECORDS ---" 

curl -XPOST 'http://localhost:9200/searchtest/mytype?pretty=1' -d '{ 
    "description": "Programming International!" 
}' 

curl -XPOST 'http://localhost:9200/searchtest/mytype?pretty=1' -d '{ 
    "description": "i18n is internationalizatin" 
}' 

echo "--- REFRESH ---" 

curl -XPOST 'http://localhost:9200/_refresh?pretty=1' 

echo "--- RUNNING QUERY WITH * ---" 

curl -XGET 'http://localhost:9200/searchtest/mytype/_search?pretty' -d ' 
{ 
    "query" : { 
     "query_string" : { 
     "query" : "inte*" 
     } 
    } 
}' 

echo "--- RUNNING QUERY ---" 

curl -XGET 'http://localhost:9200/searchtest/mytype/_search?pretty' -d ' 
{ 
    "query" : { 
     "query_string" : { 
     "query" : "inte" 
     } 
    } 
}' 

[我使用elasticsearch 0.90.9]

回答

2

没有什么错,在QUERY_STRING使用支持通配符查询分析器,你可以看到here

也许你正在寻找一个前缀查询,匹配开始以给定前缀而言这,看看这个link

curl -XGET 'http://localhost:9200/searchtest/mytype/_search?pretty' -d ' 
{ 
    "query" : { 
    "prefix" : { 
     "description" : { 
      "value" : "inte" 
     } 
    } 
    } 
}' 
+0

下面建议的比通配符查询更具扩展性。 – Nate

0

我认为你正在寻找通配符查询...

curl -XPOST "http://_search" -d' 
{ 
"query": { 
    "wildcard": { 
     "description": { 
      "value": "inte*" 
         } 
       } 
     } 
}' 

尝试使用通配符和正则表达式查询以上查询..!

HOpe它帮助..!