elasticsearch 如何使用elasticsearch筛选查询结果,elasticsearch,nest,elasticsearch,Nest" /> elasticsearch 如何使用elasticsearch筛选查询结果,elasticsearch,nest,elasticsearch,Nest" />

elasticsearch 如何使用elasticsearch筛选查询结果

elasticsearch 如何使用elasticsearch筛选查询结果,elasticsearch,nest,elasticsearch,Nest,我正在尝试创建一个组件,为用户和组提供typeahead自动完成建议。我使用的是elasticsearch 6.5.3。我创建了一个索引,其中包含我要搜索的字段和另外3个要筛选的字段(isGroup、isUser、organizationId)。在某些情况下,我希望使用此组件搜索所有用户和组,有时仅搜索用户或组,或者仅搜索属于特定组织的用户。我计划根据具体的用例提供一个过滤器和搜索词。我正在用nest做搜索,但我不知道怎么做。有可能做到这一点吗?如果有,如何做到?我走错这条路了吗?我主要是按照这

我正在尝试创建一个组件,为用户和组提供typeahead自动完成建议。我使用的是elasticsearch 6.5.3。我创建了一个索引,其中包含我要搜索的字段和另外3个要筛选的字段(isGroup、isUser、organizationId)。在某些情况下,我希望使用此组件搜索所有用户和组,有时仅搜索用户或组,或者仅搜索属于特定组织的用户。我计划根据具体的用例提供一个过滤器和搜索词。我正在用nest做搜索,但我不知道怎么做。有可能做到这一点吗?如果有,如何做到?我走错这条路了吗?我主要是按照这个来创建analyzer之类的东西。如果有帮助的话,我可以发布我的索引,但是有点长

这里是一个搜索,返回两个项目

 return client.Search<UserGroupDocument>(s => s
            .Query(q=>q
                .QueryString(qs=>qs.Query("adm"))                    
            )
        );

    {
    "_index": "users_and_groups_autocomplete_index",
    "_type": "usergroupdocument",
    "_id": "c54956ab-c50e-481c-b093-f9855cc74480",
    "_score": 2.2962174,
    "_source": {
        "id": "c54956ab-c50e-481c-b093-f9855cc74480",
        "isUser": true,
        "isGroup": false,
        "name": "admin",
        "email": "admin@snapshotdesign.com",
        "organizationId": 2
    }
},
    {
        "_index": "users_and_groups_autocomplete_index",
        "_type": "usergroupdocument",
        "_id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
        "_score": 0.8630463,
        "_source": {
        "id": "80f98d24-39e3-475d-9cb6-8f16ca472525",
        "isUser": false,
        "isGroup": true,
        "name": "new Group",
        "users": [
            {
                "name": "Test User 1",
                "email": "test@example.com"
            },
            {
                "name": "admin",
                "email": "admin@snapshotdesign.com"
            }
        ],
        "organizationId": 0
    }
},
根据Russ Cam下面的回答,我修改了必须声明,如下所示。这给了我想要的过滤功能,但没有提供typeahead功能。在我开始匹配之前,我仍然需要输入整个单词

.Must(mu => mu
    .QueryString(mmp => mmp
        .Query(searchTerms)
        .Fields(f => f
            .Field(ff => ff.Name)
            .Field(ff => ff.Users.Suffix("name"))
        )
    )
)
这是我正在使用的索引

{
"users_and_groups_autocomplete_index": {
    "aliases": {},
    "mappings": {
        "usergroupdocument": {
            "properties": {
                "email": {
                    "type": "text",
                    "fields": {
                        "autocomplete": {
                            "type": "text",
                            "analyzer": "autocomplete"
                        }
                    }
                },
                "id": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "isGroup": {
                    "type": "boolean"   
                },
                "isUser": {
                    "type": "boolean"
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "autocomplete": {
                            "type": "text",
                            "analyzer": "autocomplete"
                        }
                    }
                },
                "organizationId": {
                    "type": "long"
                },
                "users": {
                    "properties": {
                        "email": {
                            "type": "text",
                            "fields": {
                                "autocomplete": {
                                    "type": "text",
                                    "analyzer": "autocomplete"
                                }
                            }
                        },
                        "name": {
                            "type": "text",
                            "fields": {
                                "autocomplete": {
                                    "type": "text",
                                    "analyzer": "autocomplete"
                                }
                            }
                        }
                    }
                }
            }
        }
    },
    "settings": {
        "index": {
            "number_of_shards": "5",
            "provided_name": "users_and_groups_autocomplete_index",
            "creation_date": "1548363729311",
            "analysis": {
                "analyzer": {
                    "autocomplete": {
                        "filter": [
                            "lowercase"
                        ],
                        "type": "custom",
                        "tokenizer": "autocomplete"
                    }
                },
                "tokenizer": {
                    "autocomplete": {
                        "token_chars": [
                            "digit",
                            "letter"
                        ],
                        "min_gram": "1",
                        "type": "edge_ngram",
                        "max_gram": "20"
                    }
                }
            },
            "number_of_replicas": "1",
            "uuid": "Vxv-y58qQTG8Uh76Doi_dA",
            "version": {
                "created": "6050399"
            }
        }
    }
}
}

您正在寻找一种将多个查询组合在一起的方法:

  • 查询搜索词
  • 查询
    isGroup
  • 查询
    isUser
  • 查询
    组织ID
  • 并使用这些组合执行搜索。这就是使用类似的复合查询的地方。鉴于以下POCO

    public class UserGroupDocument 
    {
        public string Name { get; set; }
        public bool IsGroup { get; set; }
        public bool IsUser { get; set; }
        public string OrganizationId { get; set; }
    }
    
    我们可以从

    private static void Main()
    {
    var defaultIndex=“默认索引”;
    变量池=新的SingleNodeConnectionPool(新Uri(“http://localhost:9200")); 
    var设置=新连接设置(池)
    .DefaultIndex(默认索引);
    var客户端=新的ElasticClient(设置);
    var isUser=true;
    var isGroup=true;
    var organizationId=“organizationId”;
    var searchResponse=client.Search(x=>x
    .Index(默认索引)
    .Query(q=>q
    .Bool(b=>b
    .Must(mu=>mu
    .QueryString(mmp=>mmp
    .Query(“某些管理员”)
    .Fields(f=>f
    .Field(ff=>ff.Name)
    )
    )
    )
    .Filter(fi=>
    {
    if(isUser)
    {
    返回fi
    .Term(f=>f.IsUser,true);
    }
    返回null;
    },fi=>
    {
    if(isGroup)
    {
    返回fi
    .Term(f=>f.IsGroup,真);
    }
    返回null;
    },fi=>fi
    .Term(f=>f.OrganizationId,OrganizationId)
    )
    )
    )
    );
    }
    
    这将产生以下查询

    POST http://localhost:9200/default-index/usergroupdocument/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isUser": {
                  "value": true
                }
              }
            },
            {
              "term": {
                "isGroup": {
                  "value": true
                }
              }
            },
            {
              "term": {
                "organizationId": {
                  "value": "organizationId"
                }
              }
            }
          ],
          "must": [
            {
              "query_string": {
                "fields": [
                  "name"
                ],
                "query": "some admin"
              }
            }
          ]
        }
      }
    }
    
    如果

    • isUser
      false
      ,搜索查询中将不包括
      isUser
      字段上的
      term
      查询筛选器
    • isGroup
      false
      ,搜索查询中将不包括
      isGroup
      字段上的
      term
      查询筛选器
    • organizationId
      null
      或空字符串时,
      organizationId
      上的
      术语
      查询筛选器将不包括在搜索查询中
    现在,我们可以进一步制作
    isGroup
    isUser
    可空布尔值(
    bool?
    )。然后,当任一项的值为
    null
    时,相应的
    术语
    查询过滤器将不包括在发送到Elasticsearch的搜索查询中。这利用了Nest中称为无条件查询的功能,该功能旨在使编写更复杂的查询更容易。此外,我们还可以使用来简化编写
    bool
    查询的过程。这一切都意味着我们可以将查询细化到

    bool?isUser=true;
    布尔?isGroup=true;
    var organizationId=“organizationId”;
    var searchResponse=client.Search(x=>x
    .Index(默认索引)
    .Query(q=>q
    .QueryString(mmp=>mmp
    .Query(“某些管理员”)
    .Fields(f=>f
    .Field(ff=>ff.Name)
    )
    )&&+q
    .Term(f=>f.IsUser,IsUser)&&+q
    .Term(f=>f.IsGroup,IsGroup)&&+q
    .Term(f=>f.OrganizationId,OrganizationId)
    )
    );
    
    您要寻找的是一种将多个查询组合在一起的方法:

  • 查询搜索词
  • 查询
    isGroup
  • 查询
    isUser
  • 查询
    组织ID
  • 并使用这些组合执行搜索。这就是使用类似的复合查询的地方。鉴于以下POCO

    public class UserGroupDocument 
    {
        public string Name { get; set; }
        public bool IsGroup { get; set; }
        public bool IsUser { get; set; }
        public string OrganizationId { get; set; }
    }
    
    我们可以从

    private static void Main()
    {
    var defaultIndex=“默认索引”;
    变量池=新的SingleNodeConnectionPool(新Uri(“http://localhost:9200")); 
    var设置=新连接设置(池)
    .DefaultIndex(默认索引);
    var客户端=新的ElasticClient(设置);
    var isUser=true;
    var isGroup=true;
    var organizationId=“organizationId”;
    var searchResponse=client.Search(x=>x
    .Index(默认索引)
    .Query(q=>q
    .Bool(b=>b
    .Must(mu=>mu
    .QueryString(mmp=>mmp
    .Query(“某些管理员”)
    .Fields(f=>f
    .Field(ff=>ff.Name)
    )
    )
    )
    .Filter(fi=>
    {
    if(isUser)
    {
    返回fi
    
    POST http://localhost:9200/default-index/usergroupdocument/_search
    {
      "query": {
        "bool": {
          "filter": [
            {
              "term": {
                "isUser": {
                  "value": true
                }
              }
            },
            {
              "term": {
                "isGroup": {
                  "value": true
                }
              }
            },
            {
              "term": {
                "organizationId": {
                  "value": "organizationId"
                }
              }
            }
          ],
          "must": [
            {
              "query_string": {
                "fields": [
                  "name"
                ],
                "query": "some admin"
              }
            }
          ]
        }
      }
    }