2015-04-30 148 views
5

我正在尋找ElasticSearch嵌套查詢將提供精確匹配的字符串中使用C#中的空格。彈性搜索搜索字符串中使用C#空格和特殊字符#

例如 - 我想搜索一個詞,如'XYZ公司解決方案'。我試過查詢字符串查詢,但它給我所有的記錄,不論搜索結果如何。另外我在帖子上閱讀,發現我們必須爲該領域添加一些映射。我在該領域嘗試了'Not_Analyzed'分析儀,但仍然無效。

這裏是我的C#

var indexDefinition = new RootObjectMapping 
{ 
    Properties = new Dictionary<PropertyNameMarker, IElasticType>(), 
    Name = elastic_newindexname 
}; 
var notAnalyzedField = new StringMapping 
{ 
    Index = FieldIndexOption.NotAnalyzed 
}; 
indexDefinition.Properties.Add("Name", notAnalyzedField); 
objElasticClient.DeleteIndex(d => d.Index(elastic_newindexname)); 
var reindex = objElasticClient.Reindex<dynamic>(r => r.FromIndex(elastic_oldindexname).ToIndex(elastic_newindexname).Query(q => q.MatchAll()).Scroll("10s").CreateIndex(i => i.AddMapping<dynamic>(m => m.InitializeUsing(indexDefinition)))); 
ReindexObserver<dynamic> o = new ReindexObserver<dynamic>(onError: e => { }); 
reindex.Subscribe(o);** 

**ISearchResponse<dynamic> ivals = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(q => q.Term("Name","XYZ Company Solutions")));** //this gives 0 records 

**ISearchResponse<dynamic> ivals1 = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(q => q.Term(u => u.OnField("Name").Value("XYZ Company Solutions"))));** //this gives 0 records 

**ISearchResponse<dynamic> ivals = objElasticClient.Search<dynamic>(s => s.Index(elastic_newindexname).AllTypes().Query(@"Name = 'XYZ Company Solutions'"));** //this gives all records having fields value starting with "XYZ" 

代碼如果有人有完整的例子或在C#中的步驟,然後可以請您與我分享?

+1

還可以提供越做越elasticsearch創建了索引架構? curl -XGET localhost:9200/name_of_the_index?pretty = true 基本上,爲了匹配確切的字符串,您需要確保在索引時不分析它。 –

回答

2

請參考下面的代碼,我認爲這會符合喲你的要求。 在這裏,我已經創建並映射索引與動態模板,然後做了XDCR。 現在所有的字符串字段都不會被分析。

IIndicesOperationResponse result = null; 
        if (!objElasticClient.IndexExists(elastic_indexname).Exists) 
        { 
         result = objElasticClient.CreateIndex(elastic_indexname, c => c.AddMapping<dynamic>(m => m.Type("_default_").DynamicTemplates(t => t 
                .Add(f => f.Name("string_fields").Match("*").MatchMappingType("string").Mapping(ma => ma 
                 .String(s => s.Index(FieldIndexOption.NotAnalyzed))))))); 
       } 

感謝

穆克什Raghuwanshi

+0

謝謝Mukesh,這正是我所需要的。:-) – Diboliya

0

看起來你只需要在重新索引操作之後刷新新索引。

使用你的代碼示例(和你的第一個術語查詢),我看到了相同的結果 - 0命中。

添加以下Refresh調用返回後,在一個單一的命中reindex.Subscribe()通話效果:

objElasticClient.Refresh(new RefreshRequest() { }); 
+0

感謝您的回覆裏克,嘗試這個,但仍然得到相同的結果,即0點擊。 – Diboliya